diff --git a/backend/src/services/ConfigService.ts b/backend/src/services/ConfigService.ts index 89e919a..b12713a 100644 --- a/backend/src/services/ConfigService.ts +++ b/backend/src/services/ConfigService.ts @@ -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 diff --git a/frontend/src/store/config.ts b/frontend/src/store/config.ts index 383f1fd..3de00aa 100644 --- a/frontend/src/store/config.ts +++ b/frontend/src/store/config.ts @@ -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 diff --git a/frontend/src/views/admin/Login.vue b/frontend/src/views/admin/Login.vue index 83d3b5d..cb32a4d 100644 --- a/frontend/src/views/admin/Login.vue +++ b/frontend/src/views/admin/Login.vue @@ -14,7 +14,7 @@ label-width="80px" class="login-form-inner" > - + { // 将获取到的配置映射到表单中 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('配置保存成功') diff --git a/sql/init_mysql.sql b/sql/init_mysql.sql index e852c28..96faf69 100644 --- a/sql/init_mysql.sql +++ b/sql/init_mysql.sql @@ -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;