- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权; 已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF), 开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。 单测 91 个通过(不需要本机数据库)。 - frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 / 首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。 - extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。 - 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、 .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
132 lines
4.0 KiB
JavaScript
132 lines
4.0 KiB
JavaScript
'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;
|