fix: 🐛 修复系统配置页保存配置的bug

This commit is contained in:
Stev_Wang
2025-12-23 10:44:39 +08:00
parent 18b65d4635
commit a670270d87
5 changed files with 41 additions and 19 deletions

View File

@@ -69,11 +69,25 @@ export class ConfigService {
*/
async update(key: string, value: string, description?: string) {
try {
// 更新数据库
await this.configRepository.upsert(
{ key, value, description },
['key']
)
// 先查找记录是否存在
const existingConfig = await this.configRepository.findOne({ where: { key } })
if (existingConfig) {
// 记录存在,更新它
existingConfig.value = value
if (description !== undefined) {
existingConfig.description = description
}
await this.configRepository.save(existingConfig)
} else {
// 记录不存在,创建新记录
const newConfig = this.configRepository.create({
key,
value,
description
})
await this.configRepository.save(newConfig)
}
// 更新缓存
this.configCache[key] = value

View File

@@ -19,7 +19,7 @@ export const useConfigStore = defineStore('config', {
this.error = null
try {
const response = await configApi.getAllConfigs()
this.configs = response.data
this.configs = response.data || {}
return response
} catch (error) {
this.error = error

View File

@@ -14,7 +14,7 @@
label-width="80px"
class="login-form-inner"
>
<el-form-item label="管理员账号" prop="username">
<el-form-item label="账号" prop="username">
<el-input
v-model="loginForm.username"
placeholder="请输入管理员账号"

View File

@@ -254,6 +254,12 @@ const loadSystemConfig = async () => {
// 将获取到的配置映射到表单中
if (response && response.success && response.data) {
const configData = response.data
// 处理布尔值转换
if (configData.maintenance_mode !== undefined) {
configData.maintenance_mode = configData.maintenance_mode === 'true' || configData.maintenance_mode === true
}
Object.assign(configForm, configData)
}
} catch (error) {
@@ -267,7 +273,9 @@ const handleSaveConfig = async () => {
try {
// 遍历配置表单,逐个保存配置项
for (const [key, value] of Object.entries(configForm)) {
await configStore.updateConfig({ key, value })
// 处理布尔值转换为字符串
const saveValue = typeof value === 'boolean' ? value.toString() : value
await configStore.updateConfig({ key, value: saveValue })
}
ElMessage.success('配置保存成功')

View File

@@ -47,17 +47,17 @@ ON DUPLICATE KEY UPDATE id = id;
-- 插入系统配置
INSERT INTO config (`key`, `value`, description) VALUES
('website_name', '', '网站名称'),
('admin_domain', '', '运营后台域名'),
('player_domain', '', '玩家中心域名'),
('server_host', '', '后端服务器主机地址'),
('server_port', '', '后端服务器端口'),
('game_api_url', '', '游戏服务API地址'),
('game_psk', '', '游戏服务端的PSK'),
('jwt_secret', '', 'JWT密钥'),
('jwt_expires_in', '', 'JWT过期时间'),
('jwt_refresh_secret', '', 'JWT刷新令牌密钥'),
('jwt_refresh_expires_in', '', 'JWT刷新令牌过期时间'),
('website_name', '梦幻西游WEB版', '网站名称'),
('admin_domain', 'http://localhost:5173', '运营后台域名'),
('player_domain', 'http://localhost:5173', '玩家中心域名'),
('server_host', 'localhost', '后端服务器主机地址'),
('server_port', '3000', '后端服务器端口'),
('game_api_url', 'http://127.0.0.1:8080/tool/http', '游戏服务API地址'),
('game_psk', 'THIS_IS_A_32_BYTE_FIXED_PSK!!!!!', '游戏服务端的PSK'),
('jwt_secret', 'your_jwt_secret_key_here_change_in_production', 'JWT密钥'),
('jwt_expires_in', '24h', 'JWT过期时间'),
('jwt_refresh_secret', 'your_jwt_refresh_secret_key_here_change_in_production', 'JWT刷新令牌密钥'),
('jwt_refresh_expires_in', '7d', 'JWT刷新令牌过期时间'),
('service_status', 'running', '服务运行状态'),
('maintenance_mode', 'false', '维护模式开关')
ON DUPLICATE KEY UPDATE id = id;