- 管理后台帖子与用户审核操作中添加失败错误重定向处理 - 管理后台帖子、用户、评论删除操作中添加错误检查及提示 - 用户角色更新操作失败时添加错误重定向 - 用户封禁通知失败时添加相应错误提示 - 登录登出时session保存加入错误处理 - 讨论区上传目录创建失败时显示错误提示 - 移除admin_dashboard.html多余样式及修正侧边栏当前页高亮 - admin_posts.html和admin_users.html添加状态样式动态使用的隐藏span元素 - admin_users.html为角色选择添加label以提升无障碍性 - 升级项目依赖版本,包含gin、gorm、validator等核心库版本更新
135 lines
3.4 KiB
Go
135 lines
3.4 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"lv8girl/internal/middleware"
|
|
"lv8girl/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type DiscussionController struct {
|
|
discussionSvc *services.DiscussionService
|
|
}
|
|
|
|
func NewDiscussionController() *DiscussionController {
|
|
return &DiscussionController{
|
|
discussionSvc: services.NewDiscussionService(),
|
|
}
|
|
}
|
|
|
|
func (c *DiscussionController) ShowNewPost(ctx *gin.Context) {
|
|
userID, username, userRole, _ := middleware.GetCurrentUser(ctx)
|
|
ctx.HTML(http.StatusOK, "post_discussion.html", gin.H{
|
|
"IsLoggedIn": true,
|
|
"UserID": userID,
|
|
"Username": username,
|
|
"UserRole": userRole,
|
|
"Error": "",
|
|
"Success": "",
|
|
})
|
|
}
|
|
|
|
func (c *DiscussionController) CreatePost(ctx *gin.Context) {
|
|
userID, username, userRole, _ := middleware.GetCurrentUser(ctx)
|
|
|
|
title := strings.TrimSpace(ctx.PostForm("title"))
|
|
content := strings.TrimSpace(ctx.PostForm("content"))
|
|
|
|
if title == "" || content == "" {
|
|
ctx.HTML(http.StatusOK, "post_discussion.html", gin.H{
|
|
"IsLoggedIn": true,
|
|
"UserID": userID,
|
|
"Username": username,
|
|
"UserRole": userRole,
|
|
"Error": "标题和内容不能为空",
|
|
"Success": "",
|
|
})
|
|
return
|
|
}
|
|
|
|
var imagePath string
|
|
file, err := ctx.FormFile("image")
|
|
if err == nil {
|
|
if file.Size > 2*1024*1024 {
|
|
ctx.HTML(http.StatusOK, "post_discussion.html", gin.H{
|
|
"IsLoggedIn": true,
|
|
"UserID": userID,
|
|
"Username": username,
|
|
"UserRole": userRole,
|
|
"Error": "图片大小不能超过2MB",
|
|
"Success": "",
|
|
})
|
|
return
|
|
}
|
|
|
|
ext := strings.ToLower(filepath.Ext(file.Filename))
|
|
filename := "post_" + time.Now().Format("20060102150405") + "_" + randomString(8) + ext
|
|
uploadDir := "uploads/posts"
|
|
|
|
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
|
|
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
|
ctx.HTML(http.StatusOK, "post_discussion.html", gin.H{
|
|
"IsLoggedIn": true,
|
|
"UserID": userID,
|
|
"Username": username,
|
|
"UserRole": userRole,
|
|
"Error": "创建上传目录失败",
|
|
"Success": "",
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
imagePath = filepath.Join(uploadDir, filename)
|
|
if err := ctx.SaveUploadedFile(file, imagePath); err != nil {
|
|
ctx.HTML(http.StatusOK, "post_discussion.html", gin.H{
|
|
"IsLoggedIn": true,
|
|
"UserID": userID,
|
|
"Username": username,
|
|
"UserRole": userRole,
|
|
"Error": "图片保存失败",
|
|
"Success": "",
|
|
})
|
|
return
|
|
}
|
|
// 将路径转换为 URL 友好的格式
|
|
imagePath = filepath.ToSlash(imagePath)
|
|
}
|
|
|
|
if err := c.discussionSvc.CreatePost(userID, title, content, imagePath); err != nil {
|
|
ctx.HTML(http.StatusOK, "post_discussion.html", gin.H{
|
|
"IsLoggedIn": true,
|
|
"UserID": userID,
|
|
"Username": username,
|
|
"UserRole": userRole,
|
|
"Error": "发帖失败,请稍后重试",
|
|
"Success": "",
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx.HTML(http.StatusOK, "post_discussion.html", gin.H{
|
|
"IsLoggedIn": true,
|
|
"UserID": userID,
|
|
"Username": username,
|
|
"UserRole": userRole,
|
|
"Error": "",
|
|
"Success": "帖子已提交,等待管理员审核。",
|
|
})
|
|
}
|
|
|
|
func randomString(n int) string {
|
|
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = letters[time.Now().UnixNano()%int64(len(letters))]
|
|
}
|
|
return string(b)
|
|
}
|