Files
autoAiWorkSys/api/controller_front/config.js
张成 9784175618 1
2025-12-16 17:01:57 +08:00

95 lines
2.5 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");
// sys_parameter 表直接存储字符串值,不需要类型转换
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 { sys_parameter, 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('配置键不能为空');
}
// 查询配置(使用 sys_parameter 表)
const configs = await sys_parameter.findAll({
where: {
param_key: {
[op.in]: keys
},
is_delete: 0
}
});
if (configs.length === 0) {
return ctx.success({});
}
// 如果只有一个配置键,返回单个配置值
if (keys.length === 1) {
const config = configs[0].toJSON();
return ctx.success({
[config.param_key]: config.param_value
});
}
// 多个配置键,返回对象
const result = {};
configs.forEach(config => {
const configData = config.toJSON();
result[configData.param_key] = configData.param_value;
});
return ctx.success(result);
} catch (error) {
console.error('获取配置失败:', error);
return ctx.fail('获取配置失败: ' + error.message);
}
}
};