Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
120 lines
2.7 KiB
Go
120 lines
2.7 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"videoweb/app/service/authser"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/useragent"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Setting 账号设置页:SSR 拉取 /api/app/auth/getInfo。
|
|
func Setting(c *gin.Context) {
|
|
if useragent.TokenFromRequest(c.Request) == "" {
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
|
|
env, user, err := authser.GetInfo(c)
|
|
if err != nil {
|
|
log.Error("setting getInfo failed", log.E(err))
|
|
authser.ClearAuthCookies(c)
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
if env != nil && authser.IsSessionExpiredCode(env.Code) {
|
|
retry, needLogin := authser.OnSessionExpired(c, env.Code)
|
|
if needLogin || !retry {
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
env, user, err = authser.GetInfo(c)
|
|
if err != nil || env == nil || env.Code != 200 || user == nil {
|
|
authser.ClearAuthCookies(c)
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
} else if env == nil || env.Code != 200 || user == nil {
|
|
authser.ClearAuthCookies(c)
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
|
|
authser.SetAuthCookies(c, user)
|
|
vm := authser.BuildSettingVM(user)
|
|
data := gin.H{
|
|
"title": "账号设置",
|
|
"setting": vm,
|
|
}
|
|
setNoIndex(data, false)
|
|
HTMLOK(c, "setting.html", data)
|
|
}
|
|
|
|
// EditProfile 编辑资料页:SSR 拉取 getInfo,头像/昵称等回填。
|
|
func EditProfile(c *gin.Context) {
|
|
if useragent.TokenFromRequest(c.Request) == "" {
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
|
|
env, user, err := authser.GetInfo(c)
|
|
if err != nil {
|
|
log.Error("edit-profile getInfo failed", log.E(err))
|
|
authser.ClearAuthCookies(c)
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
if env != nil && authser.IsSessionExpiredCode(env.Code) {
|
|
retry, needLogin := authser.OnSessionExpired(c, env.Code)
|
|
if needLogin || !retry {
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
env, user, err = authser.GetInfo(c)
|
|
if err != nil || env == nil || env.Code != 200 || user == nil {
|
|
authser.ClearAuthCookies(c)
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
} else if env == nil || env.Code != 200 || user == nil {
|
|
authser.ClearAuthCookies(c)
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
|
|
authser.SetAuthCookies(c, user)
|
|
vm := authser.BuildSettingVM(user)
|
|
data := gin.H{
|
|
"title": "编辑个人资料",
|
|
"profile": vm,
|
|
}
|
|
setNoIndex(data, false)
|
|
HTMLOK(c, "editProfile.html", data)
|
|
}
|
|
|
|
// EditPassword 修改密码页:需登录。
|
|
func EditPassword(c *gin.Context) {
|
|
if useragent.TokenFromRequest(c.Request) == "" {
|
|
RedirectToLogin(c)
|
|
return
|
|
}
|
|
data := gin.H{
|
|
"title": "修改登录密码",
|
|
}
|
|
setNoIndex(data, false)
|
|
HTMLOK(c, "editPassword.html", data)
|
|
}
|
|
|
|
// Logout 清除登录 cookie,并标记跳过紧接着的一次自动游客登录。
|
|
func Logout(c *gin.Context) {
|
|
authser.ClearAuthCookies(c)
|
|
authser.MarkGuestSkipAuto(c)
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"tip": "已退出登录",
|
|
"data": nil,
|
|
})
|
|
}
|