- 用户名校验改用白名单正则(中英文/数字/下划线/连字符),字符数计法统一 - 简介→个性签名,新增 textarea 可编辑,上限 128 字符 - 用户名+签名各增加实时字符计数器(x/16、x/128) - API 合并为 PUT /api/settings/profile,一次提交两字段,按变更写入 - Toast 移至 NAV 下方,独立样式,5 秒自动消失 - 移除用户 ID 灰色底色,提示改为「个人资料已更新」 - 纯文本存储 + Go 模板自动转义防 XSS
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package controller
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"metazone.cc/metalab/internal/common"
|
||
"metazone.cc/metalab/internal/model"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// profileProvider SettingsController 对 Service 层的最小依赖(ISP:2 个方法)
|
||
type profileProvider interface {
|
||
GetProfile(userID uint) (*model.User, error)
|
||
UpdateProfile(userID uint, username, bio string) error
|
||
}
|
||
|
||
// SettingsController 个人设置控制器
|
||
type SettingsController struct {
|
||
authService profileProvider
|
||
}
|
||
|
||
// NewSettingsController 构造函数
|
||
func NewSettingsController(authService profileProvider) *SettingsController {
|
||
return &SettingsController{authService: authService}
|
||
}
|
||
|
||
// 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
|
||
}
|
||
|
||
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],
|
||
}))
|
||
}
|
||
|
||
// UpdateProfile 修改个人资料(用户名 + 个性签名,需登录)
|
||
func (sc *SettingsController) UpdateProfile(c *gin.Context) {
|
||
uid, exists := c.Get("uid")
|
||
if !exists {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
var req struct {
|
||
Username string `json:"username"`
|
||
Bio string `json:"bio"`
|
||
}
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
common.Error(c, http.StatusBadRequest, "参数错误")
|
||
return
|
||
}
|
||
|
||
if err := sc.authService.UpdateProfile(uid.(uint), req.Username, req.Bio); err != nil {
|
||
handleSettingsError(c, err)
|
||
return
|
||
}
|
||
|
||
common.OkMessage(c, "个人资料已更新")
|
||
}
|
||
|
||
// handleSettingsError 统一处理 settings 接口的 service 层错误
|
||
func handleSettingsError(c *gin.Context, err error) {
|
||
switch err {
|
||
case common.ErrUsernameTaken:
|
||
common.Error(c, http.StatusConflict, err.Error())
|
||
case common.ErrUsernameInvalid:
|
||
common.Error(c, http.StatusBadRequest, err.Error())
|
||
case common.ErrBioTooLong:
|
||
common.Error(c, http.StatusBadRequest, err.Error())
|
||
default:
|
||
common.Error(c, http.StatusInternalServerError, "操作失败")
|
||
}
|
||
}
|