- 管理后台帖子与用户审核操作中添加失败错误重定向处理 - 管理后台帖子、用户、评论删除操作中添加错误检查及提示 - 用户角色更新操作失败时添加错误重定向 - 用户封禁通知失败时添加相应错误提示 - 登录登出时session保存加入错误处理 - 讨论区上传目录创建失败时显示错误提示 - 移除admin_dashboard.html多余样式及修正侧边栏当前页高亮 - admin_posts.html和admin_users.html添加状态样式动态使用的隐藏span元素 - admin_users.html为角色选择添加label以提升无障碍性 - 升级项目依赖版本,包含gin、gorm、validator等核心库版本更新
214 lines
5.9 KiB
Go
214 lines
5.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"lv8girl/internal/middleware"
|
|
"lv8girl/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AdminController struct {
|
|
adminSvc *services.AdminService
|
|
messageSvc *services.MessageService
|
|
}
|
|
|
|
func NewAdminController() *AdminController {
|
|
return &AdminController{
|
|
adminSvc: services.NewAdminService(),
|
|
messageSvc: services.NewMessageService(),
|
|
}
|
|
}
|
|
|
|
func (c *AdminController) Dashboard(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
stats, _ := c.adminSvc.GetStats()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_dashboard.html", gin.H{
|
|
"Username": username,
|
|
"Stats": stats,
|
|
"Page": "dashboard",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) PendingPosts(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
posts, _ := c.adminSvc.GetPendingPosts()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_pending_posts.html", gin.H{
|
|
"Username": username,
|
|
"Posts": posts,
|
|
"Page": "pending_posts",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) ApprovePost(ctx *gin.Context) {
|
|
postID := parseUint(ctx.Param("id"))
|
|
action := ctx.Param("action")
|
|
|
|
if action == "approve" {
|
|
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" {
|
|
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")
|
|
}
|
|
}
|
|
|
|
func (c *AdminController) PendingUsers(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
users, _ := c.adminSvc.GetPendingUsers()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_pending_users.html", gin.H{
|
|
"Username": username,
|
|
"Users": users,
|
|
"Page": "pending_users",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) ApproveUser(ctx *gin.Context) {
|
|
userID := parseUint(ctx.Param("id"))
|
|
action := ctx.Param("action")
|
|
adminID, _ := ctx.Get("user_id")
|
|
|
|
if action == "approve" {
|
|
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" {
|
|
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")
|
|
}
|
|
}
|
|
|
|
func (c *AdminController) Posts(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
posts, _ := c.adminSvc.GetAllPosts()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_posts.html", gin.H{
|
|
"Username": username,
|
|
"Posts": posts,
|
|
"Page": "posts",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) DeletePost(ctx *gin.Context) {
|
|
postID := parseUint(ctx.Param("id"))
|
|
if err := c.adminSvc.DeletePost(postID); err != nil {
|
|
ctx.Redirect(http.StatusFound, "/admin/posts?msg=删除失败")
|
|
return
|
|
}
|
|
ctx.Redirect(http.StatusFound, "/admin/posts?msg=帖子已删除")
|
|
}
|
|
|
|
func (c *AdminController) Users(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
currentUserID, _ := ctx.Get("user_id")
|
|
users, _ := c.adminSvc.GetAllUsers()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_users.html", gin.H{
|
|
"Username": username,
|
|
"Users": users,
|
|
"CurrentUserID": currentUserID,
|
|
"Page": "users",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) UpdateUserRole(ctx *gin.Context) {
|
|
userIDStr := ctx.PostForm("user_id")
|
|
newRole := ctx.PostForm("new_role")
|
|
currentUserID, _ := ctx.Get("user_id")
|
|
|
|
userID, _ := strconv.ParseUint(userIDStr, 10, 32)
|
|
|
|
if uint(userID) == currentUserID.(uint) {
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=不能修改自己的角色")
|
|
return
|
|
}
|
|
|
|
if err := c.adminSvc.UpdateUserRole(uint(userID), newRole); err != nil {
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=更新失败")
|
|
return
|
|
}
|
|
|
|
if newRole == "banned" {
|
|
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=用户角色已更新")
|
|
}
|
|
|
|
func (c *AdminController) DeleteUser(ctx *gin.Context) {
|
|
userID := parseUint(ctx.Param("id"))
|
|
currentUserID, _ := ctx.Get("user_id")
|
|
|
|
if userID == currentUserID.(uint) {
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=不能删除自己")
|
|
return
|
|
}
|
|
|
|
if err := c.adminSvc.DeleteUser(userID); err != nil {
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=删除失败")
|
|
return
|
|
}
|
|
ctx.Redirect(http.StatusFound, "/admin/users?msg=用户已删除")
|
|
}
|
|
|
|
func (c *AdminController) Comments(ctx *gin.Context) {
|
|
username, _ := ctx.Get("username")
|
|
comments, _ := c.adminSvc.GetAllComments()
|
|
|
|
ctx.HTML(http.StatusOK, "admin_comments.html", gin.H{
|
|
"Username": username,
|
|
"Comments": comments,
|
|
"Page": "comments",
|
|
"Message": ctx.Query("msg"),
|
|
})
|
|
}
|
|
|
|
func (c *AdminController) DeleteComment(ctx *gin.Context) {
|
|
commentID := parseUint(ctx.Param("id"))
|
|
if err := c.adminSvc.DeleteComment(commentID); err != nil {
|
|
ctx.Redirect(http.StatusFound, "/admin/comments?msg=删除失败")
|
|
return
|
|
}
|
|
ctx.Redirect(http.StatusFound, "/admin/comments?msg=评论已删除")
|
|
}
|
|
|
|
func init() {
|
|
_ = middleware.GetCurrentUser
|
|
}
|