'use strict'; const { Service } = require('egg'); /** 根据 value_type 把数据库里的字符串还原成对应类型 */ function parseValue(configValue, valueType) { if (configValue === null || configValue === undefined) return null; switch (valueType) { case 'number': return Number(configValue); case 'boolean': return configValue === 'true' || configValue === '1'; case 'json': try { return JSON.parse(configValue); } catch (err) { this.ctx.logger.warn('[config] %s 不是合法 JSON,按字符串返回', configValue); return configValue; } default: return configValue; } } /** 写入前把值统一序列化成字符串 */ function stringifyValue(configValue, valueType) { if (configValue === null || configValue === undefined) return null; if (valueType === 'json') { return typeof configValue === 'string' ? configValue : JSON.stringify(configValue); } if (typeof configValue === 'object') return JSON.stringify(configValue); return String(configValue); } class ConfigService extends Service { get SysConfig() { return this.app.model.SysConfig; } /** 缓存 key:配置读多写少,走 Redis 缓存,写入/删除时清除 */ cacheKey(groupName, configKey) { return `config:${groupName}:${configKey}`; } /** 按 group + key 查询记录(不过滤 status,内部用) */ async findOne(groupName, configKey) { return this.SysConfig.findOne({ where: { groupName, configKey } }); } /** * 读取单个配置值,带 Redis 缓存;不存在或已禁用时返回 defaultValue */ async get(groupName, configKey, defaultValue = null) { const row = await this.ctx.service.cache.remember( this.cacheKey(groupName, configKey), this.config.cache.ttl, async () => { const found = await this.SysConfig.findOne({ where: { groupName, configKey, status: 1 } }); return found ? found.toJSON() : null; }, ); if (!row) return defaultValue; return parseValue.call(this, row.configValue, row.valueType); } /** 读取一个分组下所有启用的配置,返回 { key: value } */ async getGroup(groupName) { const rows = await this.SysConfig.findAll({ where: { groupName, status: 1 }, order: [[ 'configKey', 'ASC' ]], }); const result = {}; for (const row of rows) { result[row.configKey] = parseValue.call(this, row.configValue, row.valueType); } return result; } /** 列表(管理用,含禁用项) */ async list({ groupName } = {}) { const where = {}; if (groupName) where.groupName = groupName; const rows = await this.SysConfig.findAll({ where, order: [[ 'groupName', 'ASC' ], [ 'configKey', 'ASC' ]], }); return rows.map(row => row.toJSON()); } /** 新增或更新(幂等 upsert),返回写入后的记录 */ async set({ groupName = 'default', configKey, configValue, valueType = 'string', remark, status = 1 }) { if (!configKey) { this.ctx.throw(422, 'configKey 不能为空'); } const value = stringifyValue(configValue, valueType); const existing = await this.findOne(groupName, configKey); let record; if (existing) { await existing.update({ configValue: value, valueType, remark, status }); record = existing; } else { record = await this.SysConfig.create({ groupName, configKey, configValue: value, valueType, remark, status }); } await this.clearCache(groupName, configKey); return record.toJSON(); } /** 软删除配置,返回是否删除成功 */ async remove(groupName, configKey) { const affected = await this.SysConfig.destroy({ where: { groupName, configKey } }); await this.clearCache(groupName, configKey); return affected > 0; } async clearCache(groupName, configKey) { await this.ctx.service.cache.del(this.cacheKey(groupName, configKey)); } /** 总数(首页统计用) */ async count(where = {}) { return this.SysConfig.count({ where }); } } module.exports = ConfigService;