From b8f2d0422009b0fc6cfa67b87fdf204d6a7ebdfd Mon Sep 17 00:00:00 2001 From: engigu Date: Sun, 12 Oct 2025 13:48:58 +0800 Subject: [PATCH] feat: add cookies expire days settings --- .release_log | 1 + README.md | 4 ++- pkg/constant/constant.go | 9 ++--- pkg/util/jwt.go | 9 +++-- routers/api/auth.go | 5 ++- service/settings_service/settings.go | 33 +++++++++++++++++-- .../pages/settings/SiteSettings.vue | 24 +++++++++++--- 7 files changed, 68 insertions(+), 17 deletions(-) diff --git a/.release_log b/.release_log index be7413e..cf6871d 100644 --- a/.release_log +++ b/.release_log @@ -24,3 +24,4 @@ 23. [2025.08.10] UI组件使用shadcn-vue,基于tailwindcss 24. [2025.09.14] 支持系统信息展示更多的运行信息 25. [2025.09.30] 支持页面的明暗主题切换设置,增加登录日志 +25. [2025.10.12] 增加cookies过期天数设置 diff --git a/README.md b/README.md index 98fc7de..8b446cb 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,10 @@ Message Nest 是一个灵活而强大的消息推送整合平台,旨在简化 关于日志,考虑到目前多数服务以收集控制台输出为主,暂时不支持写出日志文件。 ## 功能更新日志 +- **2025.10.12** + - 增加cookies过期天数设置 - **2025.09.30** - - 支持页面的明暗主题切换设置,增加登录日志 + - 支持页面的明暗主题切换设置,增加登录日志 - **2025.09.14** - 支持系统信息展示更多的运行信息 - **2025.08.10** diff --git a/pkg/constant/constant.go b/pkg/constant/constant.go index bc1e5c1..f1d5f25 100644 --- a/pkg/constant/constant.go +++ b/pkg/constant/constant.go @@ -11,10 +11,11 @@ const SiteSettingSectionName = "site_config" // 站点信息默认值 var SiteSiteDefaultValueMap = map[string]string{ - "title": "Message Nest", - "pagesize": "8", - "slogan": "A Message Way Hosted Site", - "logo": "", + "title": "Message Nest", + "pagesize": "8", + "slogan": "A Message Way Hosted Site", + "logo": "", + "cookie_exp_days": "1", } // 日志清理自定义 diff --git a/pkg/util/jwt.go b/pkg/util/jwt.go index 5c386bf..fd3ffec 100644 --- a/pkg/util/jwt.go +++ b/pkg/util/jwt.go @@ -15,9 +15,12 @@ type UserClaims struct { jwt.RegisteredClaims } -func GenerateToken(username, password string) (string, error) { - expHours := 2 * 24 * time.Hour - //expHours := 1 * time.Minute +func GenerateToken(username, password string, expDays int) (string, error) { + // 如果传入的天数小于等于0,使用默认值1天 + if expDays <= 0 { + expDays = 1 + } + expHours := time.Duration(expDays) * 24 * time.Hour SetClaims := UserClaims{ Username: username, Password: EncodeMD5(password), diff --git a/routers/api/auth.go b/routers/api/auth.go index ea13d7d..4fca630 100644 --- a/routers/api/auth.go +++ b/routers/api/auth.go @@ -10,6 +10,7 @@ import ( "message-nest/pkg/e" "message-nest/pkg/util" "message-nest/service/auth_service" + "message-nest/service/settings_service" "message-nest/models" ) @@ -46,7 +47,9 @@ func GetAuth(c *gin.Context) { return } - token, err := util.GenerateToken(req.Username, req.Password) + // 获取配置的 cookie 过期天数 + expDays := settings_service.GetCookieExpDays() + token, err := util.GenerateToken(req.Username, req.Password, expDays) if err != nil { appG.CResponse(http.StatusInternalServerError, fmt.Sprintf("生成token失败:%s", err), nil) return diff --git a/service/settings_service/settings.go b/service/settings_service/settings.go index d4c1c23..fcf1a6c 100644 --- a/service/settings_service/settings.go +++ b/service/settings_service/settings.go @@ -3,6 +3,7 @@ package settings_service import ( "errors" "fmt" + "strconv" "github.com/robfig/cron/v3" "message-nest/models" "message-nest/pkg/app" @@ -106,9 +107,10 @@ func (us *UserSettings) EditSettings(section string, key string, value string, c // 站点自定义的结构 type SiteConfig struct { - Title string `json:"title" validate:"omitempty,min=1,max=50" label:"网站标题"` - Slogan string `json:"slogan" validate:"omitempty,min=1,max=50" label:"网站slogan"` - Logo string `json:"logo" validate:"omitempty,min=1" label:"logo"` + Title string `json:"title" validate:"omitempty,min=1,max=50" label:"网站标题"` + Slogan string `json:"slogan" validate:"omitempty,min=1,max=50" label:"网站slogan"` + Logo string `json:"logo" validate:"omitempty,min=1" label:"logo"` + CookieExpDays string `json:"cookie_exp_days" validate:"omitempty,numeric,min=1,max=365" label:"cookie过期天数"` } type LogConfig struct { @@ -116,6 +118,30 @@ type LogConfig struct { KeepNum string `json:"keep_num" validate:"required,min=1,max=50" label:"日志保留数"` } +// GetCookieExpDays 获取 cookie 过期天数,若无配置则返回默认值 1 +func GetCookieExpDays() int { + // 优先从缓存获取 + if IsSiteConfigCacheValid() { + cache := GetSiteConfigCache() + if expDays, ok := cache["cookie_exp_days"]; ok && expDays != "" { + if days, err := strconv.Atoi(expDays); err == nil && days > 0 { + return days + } + } + } + + // 从数据库获取 + setting, _ := models.GetSettingByKey(constant.SiteSettingSectionName, "cookie_exp_days") + if setting.ID > 0 && setting.Value != "" { + if days, err := strconv.Atoi(setting.Value); err == nil && days > 0 { + return days + } + } + + // 返回默认值 + return 1 +} + // ValidateDiffSetting 校验不同的设置 func (us *UserSettings) ValidateDiffSetting(section string, data map[string]string) string { if section == constant.SiteSettingSectionName { @@ -123,6 +149,7 @@ func (us *UserSettings) ValidateDiffSetting(section string, data map[string]stri config.Title = data["title"] config.Slogan = data["slogan"] config.Logo = data["logo"] + config.CookieExpDays = data["cookie_exp_days"] _, errStr := app.CommonPlaygroundValid(config) return errStr } diff --git a/web/src/components/pages/settings/SiteSettings.vue b/web/src/components/pages/settings/SiteSettings.vue index 0515bf4..d06f5ad 100644 --- a/web/src/components/pages/settings/SiteSettings.vue +++ b/web/src/components/pages/settings/SiteSettings.vue @@ -15,6 +15,7 @@ const state = reactive({ slogan: '', logo: '', pagesize: '', + cookieExpDays: '', section: 'site_config', }) @@ -27,7 +28,8 @@ const handleSubmit = async () => { title: state.title.trim(), slogan: state.slogan.trim(), logo: state.logo.trim(), - pagesize: state.pagesize, + pagesize: state.pagesize.toString(), + cookie_exp_days: state.cookieExpDays.toString(), }, } const response = await request.post('/settings/set', postData) @@ -66,6 +68,7 @@ const getSiteConfig = async () => { state.logo = data.logo || '' state.slogan = data.slogan || '' state.pagesize = data.pagesize || '' + state.cookieExpDays = data.cookie_exp_days || '1' LocalStieConfigUtils.updateLocalConfig(data) } @@ -118,11 +121,21 @@ export default { - -
- - + +
+ +
+ + +
+ + +
+ + +
+
@@ -138,6 +151,7 @@ export default {

1. logo请输入svg文本,替换后登录页面,ico,导航栏logo将全部一起更换

2. slogan将在登录页面展示

+

3. Cookie过期天数设置用户登录后的有效期,修改后下次登录时生效

** 将在下一次登录的时候生效,如果不生效请在登录页面Ctrl+F5强制刷新

** logo将替换网页ico,登录页面logo,导航栏logo