163 lines
4.2 KiB
JavaScript
163 lines
4.2 KiB
JavaScript
/**
|
||
* 系统配置管理控制器(客户端接口)
|
||
* 提供客户端调用的配置相关接口
|
||
*/
|
||
|
||
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: 配置键(单个,如:wx_num)
|
||
* - in: query
|
||
* name: configKeys
|
||
* required: false
|
||
* schema:
|
||
* type: string
|
||
* description: 配置键(多个,逗号分隔,如:wx_num,wx_img)
|
||
* 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('配置键不能为空');
|
||
}
|
||
|
||
const result = await sys_parameter.findAll({
|
||
where: {
|
||
key: {
|
||
[op.in]: keys
|
||
}
|
||
}
|
||
});
|
||
|
||
|
||
const tempData = {};
|
||
result.forEach(item => {
|
||
tempData[item.key] = item.value;
|
||
});
|
||
|
||
return ctx.success(tempData);
|
||
} catch (error) {
|
||
console.error('获取配置失败:', error);
|
||
return ctx.fail('获取配置失败: ' + error.message);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* @swagger
|
||
* /api/config/pricing-plans:
|
||
* get:
|
||
* summary: 获取价格套餐列表
|
||
* description: 获取所有可用的价格套餐
|
||
* tags: [前端-系统配置]
|
||
* responses:
|
||
* 200:
|
||
* description: 获取成功
|
||
*/
|
||
'GET /config/pricing-plans': async (ctx) => {
|
||
|
||
const models = Framework.getModels();
|
||
const { pricing_plans } = models;
|
||
|
||
// 查询所有启用且未删除的套餐,按排序顺序返回
|
||
const plans = await pricing_plans.findAll({
|
||
where: {
|
||
is_active: 1,
|
||
is_delete: 0
|
||
},
|
||
order: [['sort_order', 'ASC'], ['id', 'ASC']],
|
||
attributes: [
|
||
'id', 'name', 'duration', 'days', 'price',
|
||
'original_price', 'unit', 'discount', 'features', 'featured'
|
||
]
|
||
});
|
||
|
||
// 转换数据格式以匹配前端期望
|
||
const pricingPlans = plans.map(plan => {
|
||
const planData = plan.toJSON();
|
||
|
||
// 重命名字段以匹配前端期望(camelCase)
|
||
if (planData.original_price !== null) {
|
||
planData.originalPrice = planData.original_price;
|
||
}
|
||
delete planData.original_price;
|
||
|
||
if( planData.features)
|
||
{
|
||
planData.features = JSON.parse(planData.features);
|
||
}
|
||
|
||
// 转换 featured 为布尔值
|
||
planData.featured = planData.featured === 1;
|
||
|
||
return planData;
|
||
});
|
||
|
||
|
||
|
||
return ctx.success(pricingPlans);
|
||
|
||
},
|
||
|
||
/**
|
||
* @swagger
|
||
* /api/config/remote-code/version:
|
||
* get:
|
||
* summary: 获取远程代码版本
|
||
* description: 获取远程代码版本
|
||
* tags: [前端-系统配置]
|
||
* responses:
|
||
* 200:
|
||
* description: 获取成功
|
||
*/
|
||
'GET /config/remote-code/version': async (ctx) => {
|
||
try {
|
||
|
||
const remoteCode =
|
||
{
|
||
version: "1.0.9",
|
||
download_url: "https://work.light120.com/app/dist-modules/remote.zip",
|
||
info_url: "https://work.light120.com/app/dist-modules/remote-version.json",
|
||
}
|
||
return ctx.success(remoteCode);
|
||
} catch (error) {
|
||
console.error('获取远程代码失败:', error);
|
||
return ctx.fail('获取远程代码失败: ' + error.message);
|
||
}
|
||
}
|
||
};
|
||
|