2023-12-30 17:40:20 +08:00
|
|
|
|
package settings_service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
2023-12-31 21:31:56 +08:00
|
|
|
|
"fmt"
|
2025-10-12 13:48:58 +08:00
|
|
|
|
"strconv"
|
2023-12-31 21:31:56 +08:00
|
|
|
|
"github.com/robfig/cron/v3"
|
2023-12-30 17:40:20 +08:00
|
|
|
|
"message-nest/models"
|
2023-12-31 21:31:56 +08:00
|
|
|
|
"message-nest/pkg/app"
|
|
|
|
|
|
"message-nest/pkg/constant"
|
2025-09-14 16:27:46 +08:00
|
|
|
|
"message-nest/pkg/util"
|
2023-12-31 21:31:56 +08:00
|
|
|
|
"message-nest/service/cron_service"
|
2023-12-30 17:40:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type UserSettings struct {
|
|
|
|
|
|
UserName string
|
|
|
|
|
|
OldPassword string
|
|
|
|
|
|
NewPassword string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// EditUserPasswd 用户设置密码
|
|
|
|
|
|
func (us *UserSettings) EditUserPasswd() error {
|
|
|
|
|
|
ok, _ := models.CheckAuth(us.UserName, us.OldPassword)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return errors.New("旧密码校验失败!")
|
|
|
|
|
|
}
|
2025-08-10 14:22:37 +08:00
|
|
|
|
var user = map[string]interface{}{
|
|
|
|
|
|
"password": us.NewPassword,
|
|
|
|
|
|
}
|
2023-12-30 17:40:20 +08:00
|
|
|
|
return models.EditUser(us.UserName, user)
|
|
|
|
|
|
}
|
2023-12-31 21:31:56 +08:00
|
|
|
|
|
|
|
|
|
|
// GetUserSetting 获取用户设置
|
|
|
|
|
|
func (us *UserSettings) GetUserSetting(section string) (map[string]string, error) {
|
2025-09-20 16:26:18 +08:00
|
|
|
|
// 如果是site_config,优先从缓存获取
|
|
|
|
|
|
if section == constant.SiteSettingSectionName {
|
|
|
|
|
|
if IsSiteConfigCacheValid() {
|
|
|
|
|
|
return GetSiteConfigCache(), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-31 21:31:56 +08:00
|
|
|
|
result := make(map[string]string)
|
|
|
|
|
|
settings, err := models.GetSettingBySection(section)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return result, err
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, setting := range settings {
|
|
|
|
|
|
result[setting.Key] = setting.Value
|
|
|
|
|
|
}
|
2025-09-20 16:26:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果是site_config,更新缓存
|
|
|
|
|
|
if section == constant.SiteSettingSectionName {
|
|
|
|
|
|
SetSiteConfigCache(result)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-31 21:31:56 +08:00
|
|
|
|
// 版本信息单独获取
|
|
|
|
|
|
if section == constant.AboutSectionName {
|
|
|
|
|
|
result = constant.LatestVersion
|
2025-09-14 16:27:46 +08:00
|
|
|
|
// 添加内存使用信息
|
|
|
|
|
|
memoryInfo := util.GetMemoryUsage()
|
|
|
|
|
|
for key, value := range memoryInfo {
|
|
|
|
|
|
result[key] = value
|
|
|
|
|
|
}
|
2023-12-31 21:31:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// EditSettings 便捷自定义设置
|
|
|
|
|
|
func (us *UserSettings) EditSettings(section string, key string, value string, currentUser string) error {
|
|
|
|
|
|
setting, _ := models.GetSettingByKey(section, key)
|
|
|
|
|
|
if setting.ID <= 0 {
|
|
|
|
|
|
err := models.AddOneSetting(models.Settings{
|
|
|
|
|
|
IDModel: models.IDModel{
|
|
|
|
|
|
CreatedBy: currentUser,
|
|
|
|
|
|
ModifiedBy: currentUser,
|
|
|
|
|
|
},
|
|
|
|
|
|
Section: section,
|
|
|
|
|
|
Key: key,
|
|
|
|
|
|
Value: value,
|
|
|
|
|
|
})
|
2025-09-20 16:26:18 +08:00
|
|
|
|
// 如果是site_config,清除缓存
|
|
|
|
|
|
if section == constant.SiteSettingSectionName {
|
|
|
|
|
|
ClearSiteConfigCache()
|
|
|
|
|
|
}
|
2023-12-31 21:31:56 +08:00
|
|
|
|
return err
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if value == "" {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
|
data["section"] = section
|
|
|
|
|
|
data["key"] = key
|
|
|
|
|
|
data["value"] = value
|
|
|
|
|
|
data["modified_by"] = currentUser
|
|
|
|
|
|
err := models.EditSetting(setting.ID, data)
|
2025-12-09 21:43:47 +08:00
|
|
|
|
// 更新日志清理配置
|
|
|
|
|
|
if section == constant.LogsCleanSectionName {
|
|
|
|
|
|
if key == constant.LogsCleanCronKeyName || key == constant.LogsCleanEnabledKeyName {
|
|
|
|
|
|
cronService := cron_service.CronService{}
|
|
|
|
|
|
// 获取最新的cron和enabled值
|
|
|
|
|
|
cronSetting, _ := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanCronKeyName)
|
|
|
|
|
|
enabledSetting, _ := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanEnabledKeyName)
|
|
|
|
|
|
cronService.UpdateLogsCronRun(cronSetting.Value, enabledSetting.Value == "true")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 更新托管消息清理配置
|
|
|
|
|
|
if section == constant.HostedMsgCleanSectionName {
|
|
|
|
|
|
if key == constant.HostedMsgCleanCronKeyName || key == constant.HostedMsgCleanEnabledKeyName {
|
|
|
|
|
|
cronService := cron_service.CronService{}
|
|
|
|
|
|
// 获取最新的cron和enabled值
|
|
|
|
|
|
cronSetting, _ := models.GetSettingByKey(constant.HostedMsgCleanSectionName, constant.HostedMsgCleanCronKeyName)
|
|
|
|
|
|
enabledSetting, _ := models.GetSettingByKey(constant.HostedMsgCleanSectionName, constant.HostedMsgCleanEnabledKeyName)
|
|
|
|
|
|
cronService.UpdateHostedMsgCronRun(cronSetting.Value, enabledSetting.Value == "true")
|
|
|
|
|
|
}
|
2023-12-31 21:31:56 +08:00
|
|
|
|
}
|
2025-09-20 16:26:18 +08:00
|
|
|
|
// 如果是site_config,清除缓存
|
|
|
|
|
|
if section == constant.SiteSettingSectionName {
|
|
|
|
|
|
ClearSiteConfigCache()
|
|
|
|
|
|
}
|
2023-12-31 21:31:56 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 站点自定义的结构
|
|
|
|
|
|
type SiteConfig struct {
|
2025-10-12 13:48:58 +08:00
|
|
|
|
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过期天数"`
|
2023-12-31 21:31:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type LogConfig struct {
|
|
|
|
|
|
Cron string `json:"cron" validate:"required,cron" label:"日志定时表达式"`
|
|
|
|
|
|
KeepNum string `json:"keep_num" validate:"required,min=1,max=50" label:"日志保留数"`
|
2025-12-09 21:43:47 +08:00
|
|
|
|
Enabled string `json:"enabled" validate:"required,oneof=true false" label:"是否启用"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type HostedMsgConfig struct {
|
|
|
|
|
|
Cron string `json:"cron" validate:"required,cron" label:"托管消息定时表达式"`
|
|
|
|
|
|
KeepNum string `json:"keep_num" validate:"required,min=1,max=50" label:"托管消息保留数"`
|
|
|
|
|
|
Enabled string `json:"enabled" validate:"required,oneof=true false" label:"是否启用"`
|
2023-12-31 21:31:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-12 13:48:58 +08:00
|
|
|
|
// 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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-31 21:31:56 +08:00
|
|
|
|
// ValidateDiffSetting 校验不同的设置
|
|
|
|
|
|
func (us *UserSettings) ValidateDiffSetting(section string, data map[string]string) string {
|
|
|
|
|
|
if section == constant.SiteSettingSectionName {
|
|
|
|
|
|
var config SiteConfig
|
|
|
|
|
|
config.Title = data["title"]
|
|
|
|
|
|
config.Slogan = data["slogan"]
|
|
|
|
|
|
config.Logo = data["logo"]
|
2025-10-12 13:48:58 +08:00
|
|
|
|
config.CookieExpDays = data["cookie_exp_days"]
|
2023-12-31 21:31:56 +08:00
|
|
|
|
_, errStr := app.CommonPlaygroundValid(config)
|
|
|
|
|
|
return errStr
|
|
|
|
|
|
}
|
|
|
|
|
|
if section == constant.LogsCleanSectionName {
|
|
|
|
|
|
var config LogConfig
|
|
|
|
|
|
config.Cron = data["cron"]
|
|
|
|
|
|
config.KeepNum = data["keep_num"]
|
2025-12-09 21:43:47 +08:00
|
|
|
|
config.Enabled = data["enabled"]
|
|
|
|
|
|
_, err := cron.ParseStandard(config.Cron)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Sprintf("%s 不是合法的corn表达式", config.Cron)
|
|
|
|
|
|
}
|
|
|
|
|
|
_, errStr := app.CommonPlaygroundValid(config)
|
|
|
|
|
|
return errStr
|
|
|
|
|
|
}
|
|
|
|
|
|
if section == constant.HostedMsgCleanSectionName {
|
|
|
|
|
|
var config HostedMsgConfig
|
|
|
|
|
|
config.Cron = data["cron"]
|
|
|
|
|
|
config.KeepNum = data["keep_num"]
|
|
|
|
|
|
config.Enabled = data["enabled"]
|
2023-12-31 21:31:56 +08:00
|
|
|
|
_, err := cron.ParseStandard(config.Cron)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Sprintf("%s 不是合法的corn表达式", config.Cron)
|
|
|
|
|
|
}
|
|
|
|
|
|
_, errStr := app.CommonPlaygroundValid(config)
|
|
|
|
|
|
return errStr
|
|
|
|
|
|
}
|
|
|
|
|
|
return fmt.Sprintf("未知的section:%s", section)
|
|
|
|
|
|
}
|