238 lines
7.3 KiB
Go
238 lines
7.3 KiB
Go
package services
|
|
|
|
import (
|
|
"cmbone/internal/models"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
settingTypeSelect = "select"
|
|
settingTypeBoolean = "boolean"
|
|
settingTypeNumber = "number"
|
|
settingTypeText = "text"
|
|
settingTypePath = "path"
|
|
settingTypeHotkey = "hotkey"
|
|
)
|
|
|
|
var settingGroups = []models.AppSettingGroup{
|
|
{Category: "appearance", Label: "外观与语言", Description: "主题和界面语言设置"},
|
|
{Category: "window", Label: "窗口行为", Description: "主窗口默认尺寸、启动状态和恢复策略"},
|
|
{Category: "shortcuts", Label: "快捷键", Description: "全局快捷键默认值和入口说明"},
|
|
{Category: "data", Label: "数据目录", Description: "本地数据库、输出文件和缓存目录策略"},
|
|
{Category: "logs", Label: "日志策略", Description: "审计日志和运行日志的级别与保留周期"},
|
|
{Category: "auth", Label: "登录", Description: "登录 provider 和本地演示账号配置"},
|
|
}
|
|
|
|
func settingsDefinitions() []models.AppSettingDefinition {
|
|
return []models.AppSettingDefinition{
|
|
{
|
|
Key: "theme.mode",
|
|
Category: "appearance",
|
|
Label: "主题模式",
|
|
Type: settingTypeSelect,
|
|
DefaultValue: "light",
|
|
Description: "主窗口和组件库主题,systemdefault 表示跟随系统。",
|
|
Options: []models.AppSettingOption{
|
|
{Value: "light", Label: "亮色模式"},
|
|
{Value: "dark", Label: "暗色模式"},
|
|
{Value: "systemdefault", Label: "跟随系统"},
|
|
},
|
|
},
|
|
{
|
|
Key: "language",
|
|
Category: "appearance",
|
|
Label: "界面语言",
|
|
Type: settingTypeSelect,
|
|
DefaultValue: "zh",
|
|
Description: "应用界面语言,同时影响托盘菜单语言。",
|
|
Options: []models.AppSettingOption{
|
|
{Value: "zh", Label: "简体中文"},
|
|
{Value: "en", Label: "English"},
|
|
{Value: "zh-HK", Label: "繁體中文"},
|
|
},
|
|
},
|
|
{
|
|
Key: "window.start_state",
|
|
Category: "window",
|
|
Label: "启动窗口状态",
|
|
Type: settingTypeSelect,
|
|
DefaultValue: "normal",
|
|
Description: "主窗口启动时恢复的窗口状态。",
|
|
Options: []models.AppSettingOption{
|
|
{Value: "normal", Label: "普通窗口"},
|
|
{Value: "maximized", Label: "最大化"},
|
|
},
|
|
},
|
|
{
|
|
Key: "window.width",
|
|
Category: "window",
|
|
Label: "默认宽度",
|
|
Type: settingTypeNumber,
|
|
DefaultValue: "1280",
|
|
Description: "主窗口普通状态下保存的宽度。",
|
|
},
|
|
{
|
|
Key: "window.height",
|
|
Category: "window",
|
|
Label: "默认高度",
|
|
Type: settingTypeNumber,
|
|
DefaultValue: "800",
|
|
Description: "主窗口普通状态下保存的高度。",
|
|
},
|
|
{
|
|
Key: "window.remember_state",
|
|
Category: "window",
|
|
Label: "记住窗口状态",
|
|
Type: settingTypeBoolean,
|
|
DefaultValue: "true",
|
|
Description: "保存并恢复主窗口尺寸和最大化状态。",
|
|
},
|
|
{
|
|
Key: "shortcut.open_second_window",
|
|
Category: "shortcuts",
|
|
Label: "打开第二窗口",
|
|
Type: settingTypeHotkey,
|
|
DefaultValue: "Alt+P",
|
|
Description: "对应 hotkeys 表 target=1,实际注册由 HotkeyService 管理。",
|
|
ReadOnly: true,
|
|
},
|
|
{
|
|
Key: "shortcut.open_main_window",
|
|
Category: "shortcuts",
|
|
Label: "打开主窗口",
|
|
Type: settingTypeHotkey,
|
|
DefaultValue: "Alt+M",
|
|
Description: "对应 hotkeys 表 target=2,实际注册由 HotkeyService 管理。",
|
|
ReadOnly: true,
|
|
},
|
|
{
|
|
Key: "data.root_mode",
|
|
Category: "data",
|
|
Label: "数据目录策略",
|
|
Type: settingTypeSelect,
|
|
DefaultValue: "app_config_dir",
|
|
Description: "默认使用系统用户配置目录下的 cmbone 目录。",
|
|
Options: []models.AppSettingOption{
|
|
{Value: "app_config_dir", Label: "系统配置目录"},
|
|
{Value: "custom", Label: "自定义目录"},
|
|
},
|
|
},
|
|
{
|
|
Key: "data.custom_directory",
|
|
Category: "data",
|
|
Label: "自定义数据目录",
|
|
Type: settingTypePath,
|
|
DefaultValue: "",
|
|
Description: "当数据目录策略为 custom 时使用;为空表示未配置。",
|
|
},
|
|
{
|
|
Key: "logs.level",
|
|
Category: "logs",
|
|
Label: "运行日志级别",
|
|
Type: settingTypeSelect,
|
|
DefaultValue: "info",
|
|
Description: "运行日志默认记录级别。",
|
|
Options: []models.AppSettingOption{
|
|
{Value: "debug", Label: "Debug"},
|
|
{Value: "info", Label: "Info"},
|
|
{Value: "warn", Label: "Warn"},
|
|
{Value: "error", Label: "Error"},
|
|
},
|
|
},
|
|
{
|
|
Key: "logs.audit_retention_days",
|
|
Category: "logs",
|
|
Label: "审计日志保留天数",
|
|
Type: settingTypeNumber,
|
|
DefaultValue: "180",
|
|
Description: "审计日志默认保留周期。",
|
|
},
|
|
{
|
|
Key: "logs.app_retention_days",
|
|
Category: "logs",
|
|
Label: "运行日志保留天数",
|
|
Type: settingTypeNumber,
|
|
DefaultValue: "30",
|
|
Description: "运行日志默认保留周期。",
|
|
},
|
|
{
|
|
Key: "auth.provider",
|
|
Category: "auth",
|
|
Label: "登录 provider",
|
|
Type: settingTypeSelect,
|
|
DefaultValue: "local",
|
|
Description: "local 用于模板演示,remote 是真实后端预留入口。",
|
|
Options: []models.AppSettingOption{
|
|
{Value: "local", Label: "Local"},
|
|
{Value: "remote", Label: "Remote"},
|
|
},
|
|
},
|
|
{
|
|
Key: "auth.local.username",
|
|
Category: "auth",
|
|
Label: "本地演示账号",
|
|
Type: settingTypeText,
|
|
DefaultValue: "admin",
|
|
Description: "本地固定账号 provider 的演示用户名。",
|
|
ReadOnly: true,
|
|
},
|
|
{
|
|
Key: "auth.local.password_salt",
|
|
Category: "auth",
|
|
Label: "本地密码盐",
|
|
Type: settingTypeText,
|
|
DefaultValue: "cmbone-local-auth-v1",
|
|
Description: "本地演示账号密码盐。",
|
|
ReadOnly: true,
|
|
},
|
|
{
|
|
Key: "auth.local.password_hash",
|
|
Category: "auth",
|
|
Label: "本地密码哈希",
|
|
Type: settingTypeText,
|
|
DefaultValue: "3e0bfcda604b19fef1f596bcade7469ff1ab8b427fdc998e9a8216101b002f4a",
|
|
Description: "本地演示账号密码哈希。",
|
|
ReadOnly: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
func settingDefinitionByKey(key string) (models.AppSettingDefinition, bool) {
|
|
for _, definition := range settingsDefinitions() {
|
|
if definition.Key == key {
|
|
return definition, true
|
|
}
|
|
}
|
|
return models.AppSettingDefinition{}, false
|
|
}
|
|
|
|
func defaultAppConfigSQL() string {
|
|
var builder strings.Builder
|
|
for _, definition := range settingsDefinitions() {
|
|
builder.WriteString(fmt.Sprintf(
|
|
"INSERT OR IGNORE INTO appconfig (key, type, value, description) VALUES (%s, %s, %s, %s);\n",
|
|
sqlString(definition.Key),
|
|
sqlString(definition.Type),
|
|
sqlString(definition.DefaultValue),
|
|
sqlString(definition.Description),
|
|
))
|
|
}
|
|
return builder.String()
|
|
}
|
|
|
|
func appConfigDataMigrationSQL() string {
|
|
return `
|
|
UPDATE appconfig
|
|
SET value = 'true',
|
|
description = '保存并恢复主窗口尺寸和最大化状态。'
|
|
WHERE key = 'window.remember_state'
|
|
AND value = 'false'
|
|
AND description = '保存主窗口尺寸和最大化状态,T-015 使用该配置。';
|
|
`
|
|
}
|
|
|
|
func sqlString(value string) string {
|
|
return "'" + strings.ReplaceAll(value, "'", "''") + "'"
|
|
}
|