feat: add cookies expire days settings

This commit is contained in:
engigu
2025-10-12 13:48:58 +08:00
parent 6d51f7824f
commit b8f2d04220
7 changed files with 68 additions and 17 deletions
+1
View File
@@ -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过期天数设置
+3 -1
View File
@@ -23,8 +23,10 @@ Message Nest 是一个灵活而强大的消息推送整合平台,旨在简化
关于日志,考虑到目前多数服务以收集控制台输出为主,暂时不支持写出日志文件。
## 功能更新日志
- **2025.10.12**
- 增加cookies过期天数设置
- **2025.09.30**
- 支持页面的明暗主题切换设置,增加登录日志
- 支持页面的明暗主题切换设置,增加登录日志
- **2025.09.14**
- 支持系统信息展示更多的运行信息
- **2025.08.10**
+5 -4
View File
@@ -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": "<svg t=\"1702547210136\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1861\" width=\"200\" height=\"200\"><path d=\"M970.091313 224.033616a14.201535 14.201535 0 0 0-14.100687-3.631838c-88.419556-13.429657-142.919111-12.953859-248.242424 23.762747-69.454869 24.217859-109.056 79.873293-135.267556 116.712728-6.692202 9.403475-18.949172 26.625293-23.77309 29.509818-5.70699-1.303273-21.929374-16.036202-31.762101-24.965172-28.626747-25.994343-64.252121-58.353778-106.767516-58.757172-0.312889-0.005172-0.627071-0.005172-0.939959-0.005171-29.62101 0-54.798222 13.434828-72.857859 38.89907-13.222788 18.651798-17.914828 37.388929-18.106182 38.176323a14.31402 14.31402 0 0 0 2.893576 12.515556 14.336 14.336 0 0 0 11.782465 5.095434l0.409858-0.024565c19.192242-1.152 59.141172-3.545212 65.807516 81.857939 4.288646 54.899071 29.742545 108.631919 69.833697 147.419798 45.499475 44.025535 106.237414 67.298263 175.650909 67.298263 98.97503 0 166.222869-24.480323 205.207272-45.015919 43.172202-22.742626 62.621737-45.918384 63.429819-46.898425a14.170505 14.170505 0 0 0 1.529535-15.874586 14.191192 14.191192 0 0 0-14.166626-7.337373c-37.924202 4.443798-92.767677 6.09099-138.666667-11.298909-7.580444-2.872889-13.878303-5.893172-18.959515-8.691071 20.273131-9.964606 51.000889-28.383677 83.621495-59.93503 34.186343-33.065374 37.722505-69.908687 40.838464-102.423273 2.297535-23.919192 4.666182-48.651636 18.500526-73.091879 28.807758-50.883232 66.645333-74.489535 74.560646-79.035475a14.201535 14.201535 0 0 0 12.135434-7.776969 14.193778 14.193778 0 0 0-2.59103-16.484849z\" fill=\"\" p-id=\"1862\"></path><path d=\"M674.834101 716.481939c-33.422222 4.90796-74.989899 16.884364-117.22602-8.102787-34.843152-20.613172-53.056646-20.993293-77.783919-18.49794-0.156444 0.015515-0.312889 0.015515-0.469334 0.034909-0.274101 0.033616-0.548202 0.060768-0.822303 0.094384-4.697212 0.487434-9.65301 1.065374-14.995394 1.62004-6.702545 0.649051-13.414141 1.19596-20.133495 1.634263-75.333818 4.102465-136.860444-5.381172-163.012525-10.404202-49.488162-9.872808-86.57196-23.921778-108.946101-33.970424-59.832889-26.868364-87.080081-56.671677-87.080081-72.989738 0-1.873455 0.384-2.222545 0.787394-2.585858 2.196687-1.974303 10.616242-6.520242 41.302626-6.004364 24.793212 0.418909 56.970343 3.858101 94.308849 7.853253 40.722101 4.348121 86.873212 9.283232 136.348444 11.686788 6.913293 0.333576 13.959758 0.625778 20.93899 0.858505 9.510788 0.307717 17.545051-7.182222 17.868283-16.707233 0.319354-9.535354-7.175758-17.550222-16.705939-17.868282a1386.24 1386.24 0 0 1-20.419233-0.839112c-48.521051-2.358303-94.161455-7.236525-134.434909-11.544565-90.868364-9.712485-139.353212-13.818828-162.333737 6.833131-8.02004 7.21196-12.262141 17.004606-12.262141 28.317737 0 0.672323 0.029737 1.348525 0.065939 2.026021l0.005172 0.100848c-0.045253 0.524929-0.071111 1.039515-0.071111 1.539879 0 41.464242 9.561212 81.661414 28.414707 119.484768 17.493333 35.088808 42.251636 66.663434 73.583192 93.927434 9.169455-6.551273 20.155475-13.604202 32.524929-20.050748 49.737697-25.921939 97.838545-29.339152 139.099798-9.884444 101.590626 47.906909 142.060606 47.837091 206.198949-0.364606 5.70699-4.287354 13.814949-3.140525 18.103596 2.567758 4.289939 5.708283 3.140525 13.813657-2.567757 18.103595-38.425859 28.877576-69.968162 41.279354-105.143596 41.280647-0.484848 0-0.969697-0.002586-1.457132-0.007758-32.760242-0.316768-69.311354-11.381657-126.16404-38.190545-51.905939-24.47903-106.215434 1.008485-139.548444 23.465374 64.611556 47.922424 146.341495 74.101657 232.704 74.101656 117.55701 0 226.204444-48.934788 292.761858-131.362909l0.002586-0.002586c18.79402-14.959192 12.028121-41.362101-23.442101-36.152889z\" fill=\"\" p-id=\"1863\"></path></svg>",
"title": "Message Nest",
"pagesize": "8",
"slogan": "A Message Way Hosted Site",
"logo": "<svg t=\"1702547210136\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1861\" width=\"200\" height=\"200\"><path d=\"M970.091313 224.033616a14.201535 14.201535 0 0 0-14.100687-3.631838c-88.419556-13.429657-142.919111-12.953859-248.242424 23.762747-69.454869 24.217859-109.056 79.873293-135.267556 116.712728-6.692202 9.403475-18.949172 26.625293-23.77309 29.509818-5.70699-1.303273-21.929374-16.036202-31.762101-24.965172-28.626747-25.994343-64.252121-58.353778-106.767516-58.757172-0.312889-0.005172-0.627071-0.005172-0.939959-0.005171-29.62101 0-54.798222 13.434828-72.857859 38.89907-13.222788 18.651798-17.914828 37.388929-18.106182 38.176323a14.31402 14.31402 0 0 0 2.893576 12.515556 14.336 14.336 0 0 0 11.782465 5.095434l0.409858-0.024565c19.192242-1.152 59.141172-3.545212 65.807516 81.857939 4.288646 54.899071 29.742545 108.631919 69.833697 147.419798 45.499475 44.025535 106.237414 67.298263 175.650909 67.298263 98.97503 0 166.222869-24.480323 205.207272-45.015919 43.172202-22.742626 62.621737-45.918384 63.429819-46.898425a14.170505 14.170505 0 0 0 1.529535-15.874586 14.191192 14.191192 0 0 0-14.166626-7.337373c-37.924202 4.443798-92.767677 6.09099-138.666667-11.298909-7.580444-2.872889-13.878303-5.893172-18.959515-8.691071 20.273131-9.964606 51.000889-28.383677 83.621495-59.93503 34.186343-33.065374 37.722505-69.908687 40.838464-102.423273 2.297535-23.919192 4.666182-48.651636 18.500526-73.091879 28.807758-50.883232 66.645333-74.489535 74.560646-79.035475a14.201535 14.201535 0 0 0 12.135434-7.776969 14.193778 14.193778 0 0 0-2.59103-16.484849z\" fill=\"\" p-id=\"1862\"></path><path d=\"M674.834101 716.481939c-33.422222 4.90796-74.989899 16.884364-117.22602-8.102787-34.843152-20.613172-53.056646-20.993293-77.783919-18.49794-0.156444 0.015515-0.312889 0.015515-0.469334 0.034909-0.274101 0.033616-0.548202 0.060768-0.822303 0.094384-4.697212 0.487434-9.65301 1.065374-14.995394 1.62004-6.702545 0.649051-13.414141 1.19596-20.133495 1.634263-75.333818 4.102465-136.860444-5.381172-163.012525-10.404202-49.488162-9.872808-86.57196-23.921778-108.946101-33.970424-59.832889-26.868364-87.080081-56.671677-87.080081-72.989738 0-1.873455 0.384-2.222545 0.787394-2.585858 2.196687-1.974303 10.616242-6.520242 41.302626-6.004364 24.793212 0.418909 56.970343 3.858101 94.308849 7.853253 40.722101 4.348121 86.873212 9.283232 136.348444 11.686788 6.913293 0.333576 13.959758 0.625778 20.93899 0.858505 9.510788 0.307717 17.545051-7.182222 17.868283-16.707233 0.319354-9.535354-7.175758-17.550222-16.705939-17.868282a1386.24 1386.24 0 0 1-20.419233-0.839112c-48.521051-2.358303-94.161455-7.236525-134.434909-11.544565-90.868364-9.712485-139.353212-13.818828-162.333737 6.833131-8.02004 7.21196-12.262141 17.004606-12.262141 28.317737 0 0.672323 0.029737 1.348525 0.065939 2.026021l0.005172 0.100848c-0.045253 0.524929-0.071111 1.039515-0.071111 1.539879 0 41.464242 9.561212 81.661414 28.414707 119.484768 17.493333 35.088808 42.251636 66.663434 73.583192 93.927434 9.169455-6.551273 20.155475-13.604202 32.524929-20.050748 49.737697-25.921939 97.838545-29.339152 139.099798-9.884444 101.590626 47.906909 142.060606 47.837091 206.198949-0.364606 5.70699-4.287354 13.814949-3.140525 18.103596 2.567758 4.289939 5.708283 3.140525 13.813657-2.567757 18.103595-38.425859 28.877576-69.968162 41.279354-105.143596 41.280647-0.484848 0-0.969697-0.002586-1.457132-0.007758-32.760242-0.316768-69.311354-11.381657-126.16404-38.190545-51.905939-24.47903-106.215434 1.008485-139.548444 23.465374 64.611556 47.922424 146.341495 74.101657 232.704 74.101656 117.55701 0 226.204444-48.934788 292.761858-131.362909l0.002586-0.002586c18.79402-14.959192 12.028121-41.362101-23.442101-36.152889z\" fill=\"\" p-id=\"1863\"></path></svg>",
"cookie_exp_days": "1",
}
// 日志清理自定义
+6 -3
View File
@@ -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),
+4 -1
View File
@@ -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
+30 -3
View File
@@ -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
}
@@ -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 {
</div>
</div>
<!-- 分页大小 -->
<div class="space-y-2">
<label class="text-sm font-medium text-gray-700">分页大小</label>
<Input v-model="state.pagesize" placeholder="页面分页大小" />
<!-- 分页大小和Cookie过期天数 -->
<div class="grid grid-cols-2 gap-4">
<!-- 分页大小 -->
<div class="space-y-2">
<label class="text-sm font-medium text-gray-700">分页大小</label>
<Input v-model="state.pagesize" placeholder="页面分页大小" />
</div>
<!-- Cookie过期天数 -->
<div class="space-y-2">
<label class="text-sm font-medium text-gray-700">Cookie过期天数[1-365]</label>
<Input v-model="state.cookieExpDays" type="number" min="1" max="365" placeholder="Cookie过期天数(默认1天)" />
</div>
</div>
</div>
<!-- 底部操作区域 -->
@@ -138,6 +151,7 @@ export default {
<div class="text-sm space-y-1">
<p>1. logo请输入svg文本替换后登录页面ico导航栏logo将全部一起更换</p>
<p>2. slogan将在登录页面展示</p>
<p>3. Cookie过期天数设置用户登录后的有效期修改后下次登录时生效</p>
<p>** 将在下一次登录的时候生效如果不生效请在登录页面Ctrl+F5强制刷新</p>
<p>** logo将替换网页ico登录页面logo导航栏logo</p>
</div>