86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"videoweb/app/service/authser"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/useragent"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Setting 展示账号设置,字段来自最新的用户信息接口。
|
|
func Setting(c *gin.Context) {
|
|
data := gin.H{"title": "账号设置 - 黄果短剧", "navActive": "setting", "accountTab": "setting"}
|
|
loadAccountSummary(c, data, gin.H{})
|
|
HTMLOK(c, "setting.html", data)
|
|
}
|
|
|
|
// UpdateProfile 代理账号资料修改。可选字段必须显式传入,避免覆盖未修改内容。
|
|
func UpdateProfile(c *gin.Context) {
|
|
if useragent.TokenFromRequest(c.Request) == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "tip": "请先登录"})
|
|
return
|
|
}
|
|
var body authser.UpdateProfileReq
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "tip": "参数错误"})
|
|
return
|
|
}
|
|
if body.NickName != nil {
|
|
v := strings.TrimSpace(*body.NickName)
|
|
if v == "" || len([]rune(v)) > 40 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "tip": "昵称须为 1 至 40 个字符"})
|
|
return
|
|
}
|
|
body.NickName = &v
|
|
}
|
|
if body.Introduction != nil {
|
|
v := strings.TrimSpace(*body.Introduction)
|
|
if len([]rune(v)) > 300 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "tip": "简介不能超过 300 个字符"})
|
|
return
|
|
}
|
|
body.Introduction = &v
|
|
}
|
|
if body.Avatar != nil {
|
|
v := strings.TrimSpace(*body.Avatar)
|
|
if safeProfileImage(v) == "" || strings.HasPrefix(v, "/static/") {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "tip": "头像地址无效"})
|
|
return
|
|
}
|
|
body.Avatar = &v
|
|
}
|
|
if (body.OldPassword == nil) != (body.NewPassword == nil) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "tip": "请输入旧密码和新密码"})
|
|
return
|
|
}
|
|
if body.OldPassword != nil && (*body.OldPassword == "" || *body.NewPassword == "") {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "tip": "密码不能为空"})
|
|
return
|
|
}
|
|
if body.NickName == nil && body.Introduction == nil && body.Avatar == nil && body.OldPassword == nil && body.PrivacyMode == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "tip": "没有需要保存的修改"})
|
|
return
|
|
}
|
|
env, err := authser.UpdateProfile(c, body)
|
|
if err != nil {
|
|
log.Error("update profile failed", log.E(err))
|
|
c.JSON(http.StatusBadGateway, gin.H{"code": 500, "tip": "保存失败,请稍后重试"})
|
|
return
|
|
}
|
|
if env == nil || env.Code != 200 {
|
|
if env == nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"code": 500, "tip": "保存失败,请稍后重试"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": env.Code, "tip": firstNonEmptyAuth(env.Tip, env.Msg, "保存失败")})
|
|
return
|
|
}
|
|
if infoEnv, user, err := authser.GetInfo(c); err == nil && infoEnv != nil && infoEnv.Code == 200 && user != nil {
|
|
authser.SetAuthCookies(c, user)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 200, "tip": firstNonEmptyAuth(env.Tip, "保存成功")})
|
|
}
|