This commit is contained in:
张成
2025-12-16 16:48:52 +08:00
parent 5252add88a
commit 40cfbadb78
2 changed files with 115 additions and 1 deletions

View File

@@ -90,7 +90,7 @@ module.exports = {
order: [ order: [
['category', 'ASC'], ['category', 'ASC'],
['sortOrder', 'ASC'], ['sortOrder', 'ASC'],
['configId', 'ASC'] ['id', 'ASC']
] ]
}); });

View File

@@ -0,0 +1,114 @@
/**
* 系统配置管理控制器(客户端接口)
* 提供客户端调用的配置相关接口
*/
const Framework = require("../../framework/node-core-framework.js");
const normalizeConfigValue = (config) => {
if (!config) {
return config;
}
const normalized = { ...config };
if (normalized.configType === 'json' && normalized.configValue) {
try {
normalized.configValue = JSON.parse(normalized.configValue);
} catch (e) {
// 解析失败,保持原值
}
} else if (normalized.configType === 'boolean') {
normalized.configValue = normalized.configValue === 'true' || normalized.configValue === '1';
} else if (normalized.configType === 'number') {
normalized.configValue = parseFloat(normalized.configValue) || 0;
}
return normalized;
};
module.exports = {
/**
* @swagger
* /api/config/get:
* get:
* summary: 获取配置
* description: 根据配置键获取配置值,支持单个或多个配置键
* tags: [前端-系统配置]
* parameters:
* - in: query
* name: configKey
* required: false
* schema:
* type: string
* description: 配置键单个wechat_number
* - in: query
* name: configKeys
* required: false
* schema:
* type: string
* description: 配置键多个逗号分隔wechat_number,wechat_qrcode
* responses:
* 200:
* description: 获取成功
*/
'GET /config/get': async (ctx) => {
try {
const models = await Framework.getModels();
const { system_config, op } = models;
const { configKey, configKeys } = ctx.query;
// 如果没有提供配置键,返回失败
if (!configKey && !configKeys) {
return ctx.fail('请提供配置键');
}
// 处理多个配置键
let keys = [];
if (configKeys) {
keys = configKeys.split(',').map(k => k.trim()).filter(k => k);
} else if (configKey) {
keys = [configKey];
}
if (keys.length === 0) {
return ctx.fail('配置键不能为空');
}
// 查询配置
const configs = await system_config.findAll({
where: {
configKey: {
[op.in]: keys
},
is_delete: 0
}
});
if (configs.length === 0) {
return ctx.success({});
}
// 如果只有一个配置键,返回单个配置值
if (keys.length === 1) {
const config = normalizeConfigValue(configs[0].toJSON());
return ctx.success({
[config.configKey]: config.configValue
});
}
// 多个配置键,返回对象
const result = {};
configs.forEach(config => {
const normalized = normalizeConfigValue(config.toJSON());
result[normalized.configKey] = normalized.configValue;
});
return ctx.success(result);
} catch (error) {
console.error('获取配置失败:', error);
return ctx.fail('获取配置失败: ' + error.message);
}
}
};