Files
autoAiWorkSys/api/controller_front/config.js
张成 40cfbadb78 1
2025-12-16 16:48:52 +08:00

115 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 系统配置管理控制器(客户端接口)
* 提供客户端调用的配置相关接口
*/
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);
}
}
};