- auth_controller.go(238→54): 拆出auth_api_login.go(107)+auth_api_register.go(96) - settings_controller.go(289→101): 拆出settings_api_profile(116)+account(59)+error_handlers(41) - message_controller.go(129→85): 拆出message_page_controller.go(55) - 所有拆分后文件均≤120行,符合code-style.md瘦控制器规范
102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package controller
|
||
|
||
import (
|
||
"io"
|
||
"net/http"
|
||
|
||
"metazone.cc/metalab/internal/common"
|
||
"metazone.cc/metalab/internal/model"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// profileProvider SettingsController 对 Service 层的最小依赖(ISP:4 个方法)
|
||
type profileProvider interface {
|
||
GetProfile(userID uint) (*model.User, error)
|
||
UpdateProfile(userID uint, username, bio string) error
|
||
ChangePassword(userID uint, currentPassword, newPassword string) error
|
||
DeleteAccount(userID uint, password, reason string) error
|
||
}
|
||
|
||
// avatarProvider 头像上传对 Service 层的最小依赖(ISP:2 个方法)
|
||
type avatarProvider interface {
|
||
ProcessAvatar(userID uint, file io.Reader, contentType string, cropX, cropY, cropSize int) (string, error)
|
||
ProcessImage(userID uint, file io.Reader, contentType string, cropX, cropY, cropSize int) (string, error)
|
||
}
|
||
|
||
// auditSubmittable 个人设置对审核服务的依赖(ISP:4 个方法)
|
||
type auditSubmittable interface {
|
||
ShouldAudit(userID uint) (bool, error)
|
||
SubmitProfileChanges(userID uint, currentUser *model.User, newUsername, newBio string) error
|
||
Submit(userID uint, auditType, newValue string) error
|
||
GetPendingTypes(userID uint) ([]string, error)
|
||
}
|
||
|
||
// SettingsController 个人设置控制器
|
||
type SettingsController struct {
|
||
authService profileProvider
|
||
avatarService avatarProvider
|
||
auditService auditSubmittable
|
||
}
|
||
|
||
// NewSettingsController 构造函数
|
||
func NewSettingsController(authService profileProvider, avatarService avatarProvider, auditService auditSubmittable) *SettingsController {
|
||
return &SettingsController{authService: authService, avatarService: avatarService, auditService: auditService}
|
||
}
|
||
|
||
// SettingsPage 个人设置页面(需登录)
|
||
func (sc *SettingsController) SettingsPage(c *gin.Context) {
|
||
uidVal, exists := c.Get("uid")
|
||
if !exists {
|
||
c.Redirect(http.StatusFound, "/auth/login")
|
||
c.Abort()
|
||
return
|
||
}
|
||
uid := uidVal.(uint)
|
||
|
||
tab := c.Param("tab")
|
||
if tab != "profile" && tab != "account" {
|
||
c.String(http.StatusNotFound, "页面不存在")
|
||
return
|
||
}
|
||
|
||
user, err := sc.authService.GetProfile(uid)
|
||
if err != nil || user == nil {
|
||
c.String(http.StatusNotFound, "用户不存在")
|
||
return
|
||
}
|
||
|
||
// 查询待审核类型(用于前端显示审核提示)
|
||
pendingTypes, _ := sc.auditService.GetPendingTypes(uid)
|
||
|
||
c.HTML(http.StatusOK, "settings/index.html", common.BuildPageData(c, gin.H{
|
||
"Title": "个人设置",
|
||
"ExtraCSS": "/static/css/settings.css",
|
||
"ActiveTab": tab,
|
||
"User": user,
|
||
"RoleName": model.RoleDisplayNames[user.Role],
|
||
"StatusName": model.StatusDisplayNames[user.Status],
|
||
"PendingTypes": pendingTypes,
|
||
"AuditTypes": model.AuditTypeNames,
|
||
}))
|
||
}
|
||
|
||
// AuditStatus 查询当前用户的待审核类型(需登录)
|
||
func (sc *SettingsController) AuditStatus(c *gin.Context) {
|
||
uid, exists := c.Get("uid")
|
||
if !exists {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
types, err := sc.auditService.GetPendingTypes(uid.(uint))
|
||
if err != nil {
|
||
common.Ok(c, gin.H{"pending_types": []string{}})
|
||
return
|
||
}
|
||
if types == nil {
|
||
types = []string{}
|
||
}
|
||
common.Ok(c, gin.H{"pending_types": types})
|
||
}
|