package controller import ( "bbs-backend/api/reply" "bbs-backend/api/request" "bbs-backend/common/errcode" "bbs-backend/common/response" "bbs-backend/logic/appservice" "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "net/http" "strings" ) // GetBBSInfo 获取论坛信息 func GetBBSInfo(c *gin.Context) { forumInfo, err := appservice.GetBBSInfo() if err != nil { response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError) return } response.Success(c, reply.BBSInfoReply{ ForumName: forumInfo["forum_name"], ForumLogo: forumInfo["forum_logo"], ForumDescription: forumInfo["forum_description"], }) } // UpdateBBSInfo 更新论坛信息 func UpdateBBSInfo(c *gin.Context) { // 验证管理员权限 if !isAdmin(c) { response.Error(c, http.StatusForbidden, errcode.ErrForbidden) return } var req request.UpdateBBSInfoRequest if err := c.ShouldBindJSON(&req); err != nil { response.Error(c, http.StatusBadRequest, errcode.ErrBadRequest) return } err := appservice.UpdateBBSInfo(req) if err != nil { response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError) return } response.Success(c, gin.H{"message": "Forum info updated successfully"}) } // isAdmin 验证用户是否为管理员 func isAdmin(c *gin.Context) bool { authHeader := c.GetHeader("Authorization") if authHeader == "" { return false } tokenString := strings.Replace(authHeader, "Bearer ", "", 1) token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { return []byte("your_secret_key"), nil }) if err != nil { return false } claims, ok := token.Claims.(jwt.MapClaims) if !ok || !token.Valid { return false } userID, ok := claims["user_id"].(float64) if !ok { return false } // 假设管理员用户的ID为1 return uint(userID) == 1 }