fix(admin): 添加管理操作错误处理及更新模板样式
- 管理后台帖子与用户审核操作中添加失败错误重定向处理 - 管理后台帖子、用户、评论删除操作中添加错误检查及提示 - 用户角色更新操作失败时添加错误重定向 - 用户封禁通知失败时添加相应错误提示 - 登录登出时session保存加入错误处理 - 讨论区上传目录创建失败时显示错误提示 - 移除admin_dashboard.html多余样式及修正侧边栏当前页高亮 - admin_posts.html和admin_users.html添加状态样式动态使用的隐藏span元素 - admin_users.html为角色选择添加label以提升无障碍性 - 升级项目依赖版本,包含gin、gorm、validator等核心库版本更新
This commit is contained in:
@@ -4,9 +4,10 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"lv8girl/internal/middleware"
|
||||
"lv8girl/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AdminController struct {
|
||||
@@ -50,10 +51,16 @@ func (c *AdminController) ApprovePost(ctx *gin.Context) {
|
||||
action := ctx.Param("action")
|
||||
|
||||
if action == "approve" {
|
||||
c.adminSvc.ApprovePost(postID)
|
||||
if err := c.adminSvc.ApprovePost(postID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_posts?msg=审核失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_posts?msg=帖子已通过审核")
|
||||
} else if action == "reject" {
|
||||
c.adminSvc.RejectPost(postID)
|
||||
if err := c.adminSvc.RejectPost(postID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_posts?msg=拒绝失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_posts?msg=帖子已拒绝")
|
||||
} else {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_posts")
|
||||
@@ -78,12 +85,24 @@ func (c *AdminController) ApproveUser(ctx *gin.Context) {
|
||||
adminID, _ := ctx.Get("user_id")
|
||||
|
||||
if action == "approve" {
|
||||
c.adminSvc.ApproveUser(userID)
|
||||
c.messageSvc.NotifyUserApproved(adminID.(uint), userID)
|
||||
if err := c.adminSvc.ApproveUser(userID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=审核失败")
|
||||
return
|
||||
}
|
||||
if err := c.messageSvc.NotifyUserApproved(adminID.(uint), userID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=审核通过但通知失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=用户已通过审核")
|
||||
} else if action == "reject" {
|
||||
c.adminSvc.RejectUser(userID)
|
||||
c.messageSvc.NotifyUserRejected(adminID.(uint), userID)
|
||||
if err := c.adminSvc.RejectUser(userID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=拒绝失败")
|
||||
return
|
||||
}
|
||||
if err := c.messageSvc.NotifyUserRejected(adminID.(uint), userID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=拒绝成功但通知失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_users?msg=用户已拒绝")
|
||||
} else {
|
||||
ctx.Redirect(http.StatusFound, "/admin/pending_users")
|
||||
@@ -104,7 +123,10 @@ func (c *AdminController) Posts(ctx *gin.Context) {
|
||||
|
||||
func (c *AdminController) DeletePost(ctx *gin.Context) {
|
||||
postID := parseUint(ctx.Param("id"))
|
||||
c.adminSvc.DeletePost(postID)
|
||||
if err := c.adminSvc.DeletePost(postID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/posts?msg=删除失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/admin/posts?msg=帖子已删除")
|
||||
}
|
||||
|
||||
@@ -134,10 +156,16 @@ func (c *AdminController) UpdateUserRole(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.adminSvc.UpdateUserRole(uint(userID), newRole)
|
||||
if err := c.adminSvc.UpdateUserRole(uint(userID), newRole); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/users?msg=更新失败")
|
||||
return
|
||||
}
|
||||
|
||||
if newRole == "banned" {
|
||||
c.messageSvc.NotifyUserBanned(currentUserID.(uint), uint(userID))
|
||||
if err := c.messageSvc.NotifyUserBanned(currentUserID.(uint), uint(userID)); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/users?msg=封禁成功但通知失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Redirect(http.StatusFound, "/admin/users?msg=用户角色已更新")
|
||||
@@ -152,7 +180,10 @@ func (c *AdminController) DeleteUser(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.adminSvc.DeleteUser(userID)
|
||||
if err := c.adminSvc.DeleteUser(userID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/users?msg=删除失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/admin/users?msg=用户已删除")
|
||||
}
|
||||
|
||||
@@ -170,7 +201,10 @@ func (c *AdminController) Comments(ctx *gin.Context) {
|
||||
|
||||
func (c *AdminController) DeleteComment(ctx *gin.Context) {
|
||||
commentID := parseUint(ctx.Param("id"))
|
||||
c.adminSvc.DeleteComment(commentID)
|
||||
if err := c.adminSvc.DeleteComment(commentID); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/admin/comments?msg=删除失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/admin/comments?msg=评论已删除")
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,10 @@ package controllers
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"lv8girl/internal/services"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
"lv8girl/internal/services"
|
||||
)
|
||||
|
||||
type AuthController struct {
|
||||
@@ -51,7 +52,10 @@ func (c *AuthController) Login(ctx *gin.Context) {
|
||||
session.Set("user_id", result.User.ID)
|
||||
session.Set("username", result.User.Username)
|
||||
session.Set("user_role", result.User.Role)
|
||||
session.Save()
|
||||
if err := session.Save(); err != nil {
|
||||
ctx.HTML(http.StatusOK, "login.html", gin.H{"Error": "登录失败,请重试"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
@@ -94,6 +98,9 @@ func (c *AuthController) Register(ctx *gin.Context) {
|
||||
func (c *AuthController) Logout(ctx *gin.Context) {
|
||||
session := sessions.Default(ctx)
|
||||
session.Clear()
|
||||
session.Save()
|
||||
if err := session.Save(); err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "退出失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
@@ -7,9 +7,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"lv8girl/internal/middleware"
|
||||
"lv8girl/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DiscussionController struct {
|
||||
@@ -72,7 +73,17 @@ func (c *DiscussionController) CreatePost(ctx *gin.Context) {
|
||||
uploadDir := "uploads/posts"
|
||||
|
||||
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
|
||||
os.MkdirAll(uploadDir, 0755)
|
||||
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)
|
||||
|
||||
@@ -4,11 +4,12 @@ import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
"lv8girl/internal/middleware"
|
||||
"lv8girl/internal/repositories"
|
||||
"lv8girl/internal/services"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type HomeController struct {
|
||||
@@ -36,7 +37,7 @@ func (c *HomeController) Index(ctx *gin.Context) {
|
||||
|
||||
if isLoggedIn {
|
||||
userRepo := repositories.NewUserRepository()
|
||||
userRepo.UpdateLastActive(userID)
|
||||
_ = userRepo.UpdateLastActive(userID)
|
||||
}
|
||||
|
||||
posts, _ := c.discussionSvc.GetApprovedPosts(30)
|
||||
@@ -86,10 +87,10 @@ func (c *HomeController) ShowPost(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
if !viewedMap[postID] {
|
||||
c.discussionSvc.IncrementViews(postID)
|
||||
_ = c.discussionSvc.IncrementViews(postID)
|
||||
viewedMap[postID] = true
|
||||
session.Set(viewedKey, viewedMap)
|
||||
session.Save()
|
||||
_ = session.Save()
|
||||
}
|
||||
|
||||
detail, err := c.discussionSvc.GetPostDetail(postID, userID)
|
||||
@@ -129,7 +130,7 @@ func (c *HomeController) LikePost(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
postID := parseUint(ctx.Param("id"))
|
||||
c.discussionSvc.AddLike(postID, userID)
|
||||
_ = c.discussionSvc.AddLike(postID, userID)
|
||||
ctx.Redirect(http.StatusFound, "/post/"+ctx.Param("id"))
|
||||
}
|
||||
|
||||
@@ -144,7 +145,7 @@ func (c *HomeController) AddComment(ctx *gin.Context) {
|
||||
content := ctx.PostForm("content")
|
||||
|
||||
if content != "" {
|
||||
c.discussionSvc.AddComment(postID, userID, content)
|
||||
_ = c.discussionSvc.AddComment(postID, userID, content)
|
||||
}
|
||||
|
||||
ctx.Redirect(http.StatusFound, "/post/"+ctx.Param("id"))
|
||||
|
||||
@@ -4,10 +4,11 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"lv8girl/internal/middleware"
|
||||
"lv8girl/internal/repositories"
|
||||
"lv8girl/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MessageController struct {
|
||||
@@ -79,7 +80,10 @@ func (c *MessageController) SendMessage(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.messageSvc.SendMessage(currentUserID, toUserID, content)
|
||||
err := c.messageSvc.SendMessage(currentUserID, toUserID, content)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.HTML(http.StatusOK, "send_message.html", gin.H{
|
||||
"Receiver": receiver,
|
||||
|
||||
@@ -8,9 +8,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"lv8girl/internal/middleware"
|
||||
"lv8girl/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserController struct {
|
||||
@@ -86,7 +87,10 @@ func (c *UserController) UploadAvatar(ctx *gin.Context) {
|
||||
uploadDir := "uploads/avatars"
|
||||
|
||||
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
|
||||
os.MkdirAll(uploadDir, 0755)
|
||||
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/profile?error=创建上传目录失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
imagePath := filepath.Join(uploadDir, filename)
|
||||
@@ -97,6 +101,9 @@ func (c *UserController) UploadAvatar(ctx *gin.Context) {
|
||||
|
||||
// 将路径转换为 URL 友好的格式(使用正斜杠)
|
||||
avatarPath := filepath.ToSlash(imagePath)
|
||||
c.userSvc.UpdateAvatar(userID, avatarPath)
|
||||
if err := c.userSvc.UpdateAvatar(userID, avatarPath); err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/profile?error=头像更新失败")
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/profile?success=头像更新成功")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user