feat: apply user settings to effect
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"baihu/internal/constant"
|
||||
"baihu/internal/middleware"
|
||||
"baihu/internal/services"
|
||||
"baihu/internal/utils"
|
||||
@@ -9,11 +12,12 @@ import (
|
||||
)
|
||||
|
||||
type AuthController struct {
|
||||
userService *services.UserService
|
||||
userService *services.UserService
|
||||
settingsService *services.SettingsService
|
||||
}
|
||||
|
||||
func NewAuthController(userService *services.UserService) *AuthController {
|
||||
return &AuthController{userService: userService}
|
||||
func NewAuthController(userService *services.UserService, settingsService *services.SettingsService) *AuthController {
|
||||
return &AuthController{userService: userService, settingsService: settingsService}
|
||||
}
|
||||
|
||||
func (ac *AuthController) Login(c *gin.Context) {
|
||||
@@ -33,15 +37,30 @@ func (ac *AuthController) Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取 cookie 过期天数
|
||||
expireDays := 7
|
||||
if days := ac.settingsService.Get(constant.SectionSite, constant.KeyCookieDays); days != "" {
|
||||
if d, err := strconv.Atoi(days); err == nil && d > 0 {
|
||||
expireDays = d
|
||||
}
|
||||
}
|
||||
|
||||
// 获取 JWT Secret
|
||||
jwtSecret := ac.settingsService.Get(constant.SectionSystem, constant.KeyJWTSecret)
|
||||
if jwtSecret == "" {
|
||||
utils.ServerError(c, "系统配置错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 生成 token
|
||||
token, err := utils.GenerateToken(user.ID, user.Username)
|
||||
token, err := utils.GenerateToken(user.ID, user.Username, expireDays, jwtSecret)
|
||||
if err != nil {
|
||||
utils.ServerError(c, "登录失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 设置 Cookie
|
||||
middleware.SetAuthCookie(c, token)
|
||||
middleware.SetAuthCookie(c, token, expireDays)
|
||||
|
||||
utils.Success(c, gin.H{
|
||||
"user": user.Username,
|
||||
|
||||
@@ -79,18 +79,18 @@ func (sc *SettingsController) CleanLogs(c *gin.Context) {
|
||||
|
||||
// GetSiteSettings 获取站点设置
|
||||
func (sc *SettingsController) GetSiteSettings(c *gin.Context) {
|
||||
settings := sc.settingsService.GetSection("site")
|
||||
settings := sc.settingsService.GetSection(constant.SectionSite)
|
||||
utils.Success(c, settings)
|
||||
}
|
||||
|
||||
// GetPublicSiteSettings 获取公开的站点设置(无需认证)
|
||||
func (sc *SettingsController) GetPublicSiteSettings(c *gin.Context) {
|
||||
settings := sc.settingsService.GetSection("site")
|
||||
settings := sc.settingsService.GetSection(constant.SectionSite)
|
||||
// 只返回公开信息
|
||||
utils.Success(c, gin.H{
|
||||
"title": settings["title"],
|
||||
"subtitle": settings["subtitle"],
|
||||
"icon": settings["icon"],
|
||||
constant.KeyTitle: settings[constant.KeyTitle],
|
||||
constant.KeySubtitle: settings[constant.KeySubtitle],
|
||||
constant.KeyIcon: settings[constant.KeyIcon],
|
||||
})
|
||||
}
|
||||
|
||||
@@ -110,14 +110,14 @@ func (sc *SettingsController) UpdateSiteSettings(c *gin.Context) {
|
||||
}
|
||||
|
||||
values := map[string]string{
|
||||
"title": req.Title,
|
||||
"subtitle": req.Subtitle,
|
||||
"icon": req.Icon,
|
||||
"page_size": req.PageSize,
|
||||
"cookie_days": req.CookieDays,
|
||||
constant.KeyTitle: req.Title,
|
||||
constant.KeySubtitle: req.Subtitle,
|
||||
constant.KeyIcon: req.Icon,
|
||||
constant.KeyPageSize: req.PageSize,
|
||||
constant.KeyCookieDays: req.CookieDays,
|
||||
}
|
||||
|
||||
if err := sc.settingsService.SetSection("site", values); err != nil {
|
||||
if err := sc.settingsService.SetSection(constant.SectionSite, values); err != nil {
|
||||
utils.ServerError(c, "保存失败")
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user