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=评论已删除")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user