Files
lv8girl/internal/controllers/home.go
nannanwu 690b4d5961 fix(admin): 添加管理操作错误处理及更新模板样式
- 管理后台帖子与用户审核操作中添加失败错误重定向处理
- 管理后台帖子、用户、评论删除操作中添加错误检查及提示
- 用户角色更新操作失败时添加错误重定向
- 用户封禁通知失败时添加相应错误提示
- 登录登出时session保存加入错误处理
- 讨论区上传目录创建失败时显示错误提示
- 移除admin_dashboard.html多余样式及修正侧边栏当前页高亮
- admin_posts.html和admin_users.html添加状态样式动态使用的隐藏span元素
- admin_users.html为角色选择添加label以提升无障碍性
- 升级项目依赖版本,包含gin、gorm、validator等核心库版本更新
2026-02-24 21:14:55 +08:00

163 lines
4.1 KiB
Go

package controllers
import (
"net/http"
"path/filepath"
"lv8girl/internal/middleware"
"lv8girl/internal/repositories"
"lv8girl/internal/services"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type HomeController struct {
discussionSvc *services.DiscussionService
userSvc *services.UserService
messageSvc *services.MessageService
discussionRepo *repositories.DiscussionRepository
}
func NewHomeController() *HomeController {
return &HomeController{
discussionSvc: services.NewDiscussionService(),
userSvc: services.NewUserService(),
messageSvc: services.NewMessageService(),
discussionRepo: repositories.NewDiscussionRepository(),
}
}
func (c *HomeController) Index(ctx *gin.Context) {
session := sessions.Default(ctx)
userID, _ := session.Get("user_id").(uint)
username, _ := session.Get("username").(string)
userRole, _ := session.Get("user_role").(string)
isLoggedIn := userID != 0
if isLoggedIn {
userRepo := repositories.NewUserRepository()
_ = userRepo.UpdateLastActive(userID)
}
posts, _ := c.discussionSvc.GetApprovedPosts(30)
// 处理头像路径,确保使用正斜杠并添加前导斜杠
for i := range posts {
if posts[i].Avatar != "" {
avatarPath := filepath.ToSlash(posts[i].Avatar)
posts[i].Avatar = "/" + avatarPath
}
}
var postCount, userCount, onlineCount int64
postCount, _ = c.discussionRepo.CountByStatus("approved")
userCount, onlineCount, _ = c.userSvc.GetUserStats()
var unreadCount int64
if isLoggedIn {
unreadCount, _ = c.messageSvc.GetUnreadCount(userID)
}
ctx.HTML(http.StatusOK, "index.html", gin.H{
"IsLoggedIn": isLoggedIn,
"UserID": userID,
"Username": username,
"UserRole": userRole,
"Posts": posts,
"PostCount": postCount,
"UserCount": userCount,
"OnlineCount": onlineCount,
"UnreadCount": unreadCount,
})
}
func (c *HomeController) ShowPost(ctx *gin.Context) {
postID := parseUint(ctx.Param("id"))
userID, username, userRole, isLoggedIn := middleware.GetCurrentUser(ctx)
session := sessions.Default(ctx)
viewedKey := "viewed_posts"
viewedPosts := session.Get(viewedKey)
var viewedMap map[uint]bool
if viewedPosts == nil {
viewedMap = make(map[uint]bool)
} else {
viewedMap = viewedPosts.(map[uint]bool)
}
if !viewedMap[postID] {
_ = c.discussionSvc.IncrementViews(postID)
viewedMap[postID] = true
session.Set(viewedKey, viewedMap)
_ = session.Save()
}
detail, err := c.discussionSvc.GetPostDetail(postID, userID)
if err != nil {
ctx.String(http.StatusNotFound, "帖子不存在")
return
}
comments, _ := c.discussionSvc.GetComments(postID)
avatar := ""
if detail.Post.User.Avatar != "" {
// 确保路径使用正斜杠,并添加前导斜杠
avatarPath := filepath.ToSlash(detail.Post.User.Avatar)
avatar = "/" + avatarPath
}
ctx.HTML(http.StatusOK, "post.html", gin.H{
"IsLoggedIn": isLoggedIn,
"UserID": userID,
"Username": username,
"UserRole": userRole,
"Post": detail.Post,
"PostAvatar": avatar,
"Comments": comments,
"LikeCount": detail.LikeCount,
"UserLiked": detail.UserLiked,
"AuthorPostCount": detail.AuthorPostCount,
})
}
func (c *HomeController) LikePost(ctx *gin.Context) {
userID, _, _, isLoggedIn := middleware.GetCurrentUser(ctx)
if !isLoggedIn {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "请先登录"})
return
}
postID := parseUint(ctx.Param("id"))
_ = c.discussionSvc.AddLike(postID, userID)
ctx.Redirect(http.StatusFound, "/post/"+ctx.Param("id"))
}
func (c *HomeController) AddComment(ctx *gin.Context) {
userID, _, _, isLoggedIn := middleware.GetCurrentUser(ctx)
if !isLoggedIn {
ctx.Redirect(http.StatusFound, "/login")
return
}
postID := parseUint(ctx.Param("id"))
content := ctx.PostForm("content")
if content != "" {
_ = c.discussionSvc.AddComment(postID, userID, content)
}
ctx.Redirect(http.StatusFound, "/post/"+ctx.Param("id"))
}
func parseUint(s string) uint {
var result uint
for _, c := range s {
if c >= '0' && c <= '9' {
result = result*10 + uint(c-'0')
}
}
return result
}