262 lines
6.2 KiB
JavaScript
262 lines
6.2 KiB
JavaScript
/**
|
|
* 系统配置模型
|
|
* @file SystemConfig.js
|
|
* @description 系统参数配置数据模型
|
|
*/
|
|
const { DataTypes } = require('sequelize');
|
|
const BaseModel = require('./BaseModel');
|
|
const { sequelize } = require('../config/database-simple');
|
|
|
|
class SystemConfig extends BaseModel {
|
|
/**
|
|
* 获取配置值并自动转换类型
|
|
* @param {string} key 配置键名
|
|
* @returns {*} 转换后的配置值
|
|
*/
|
|
static async getValue(key) {
|
|
try {
|
|
const config = await this.findOne({
|
|
where: { config_key: key }
|
|
});
|
|
|
|
if (!config) {
|
|
return null;
|
|
}
|
|
|
|
return this.parseValue(config.config_value, config.config_type);
|
|
} catch (error) {
|
|
console.error(`获取配置 ${key} 失败:`, error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置配置值
|
|
* @param {string} key 配置键名
|
|
* @param {*} value 配置值
|
|
* @param {number} userId 操作用户ID
|
|
* @returns {Object} 配置对象
|
|
*/
|
|
static async setValue(key, value, userId = null) {
|
|
try {
|
|
const existingConfig = await this.findOne({
|
|
where: { config_key: key }
|
|
});
|
|
|
|
const stringValue = this.stringifyValue(value);
|
|
const configType = this.detectType(value);
|
|
|
|
if (existingConfig) {
|
|
await existingConfig.update({
|
|
config_value: stringValue,
|
|
config_type: configType,
|
|
updated_by: userId
|
|
});
|
|
return existingConfig;
|
|
} else {
|
|
return await this.create({
|
|
config_key: key,
|
|
config_value: stringValue,
|
|
config_type: configType,
|
|
updated_by: userId
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error(`设置配置 ${key} 失败:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取公开配置(前端可访问)
|
|
* @returns {Object} 公开配置对象
|
|
*/
|
|
static async getPublicConfigs() {
|
|
try {
|
|
const configs = await this.findAll({
|
|
where: { is_public: true },
|
|
order: [['category', 'ASC'], ['sort_order', 'ASC']]
|
|
});
|
|
|
|
const result = {};
|
|
configs.forEach(config => {
|
|
result[config.config_key] = this.parseValue(config.config_value, config.config_type);
|
|
});
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error('获取公开配置失败:', error);
|
|
return {};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取分类配置
|
|
* @param {string} category 配置分类
|
|
* @returns {Array} 配置列表
|
|
*/
|
|
static async getByCategory(category) {
|
|
try {
|
|
const configs = await this.findAll({
|
|
where: { category },
|
|
order: [['sort_order', 'ASC']]
|
|
});
|
|
|
|
return configs.map(config => ({
|
|
...config.dataValues,
|
|
parsed_value: this.parseValue(config.config_value, config.config_type)
|
|
}));
|
|
} catch (error) {
|
|
console.error(`获取分类 ${category} 配置失败:`, error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 解析配置值
|
|
* @param {string} value 字符串值
|
|
* @param {string} type 数据类型
|
|
* @returns {*} 解析后的值
|
|
*/
|
|
static parseValue(value, type) {
|
|
if (value === null || value === undefined) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
switch (type) {
|
|
case 'number':
|
|
return Number(value);
|
|
case 'boolean':
|
|
return value === 'true' || value === '1' || value === 1;
|
|
case 'json':
|
|
return JSON.parse(value);
|
|
case 'array':
|
|
return JSON.parse(value);
|
|
default:
|
|
return value;
|
|
}
|
|
} catch (error) {
|
|
console.error(`解析配置值失败: ${value}, type: ${type}`, error);
|
|
return value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 转换值为字符串
|
|
* @param {*} value 任意类型的值
|
|
* @returns {string} 字符串值
|
|
*/
|
|
static stringifyValue(value) {
|
|
if (value === null || value === undefined) {
|
|
return null;
|
|
}
|
|
|
|
if (typeof value === 'object') {
|
|
return JSON.stringify(value);
|
|
}
|
|
|
|
return String(value);
|
|
}
|
|
|
|
/**
|
|
* 检测值的类型
|
|
* @param {*} value 任意类型的值
|
|
* @returns {string} 数据类型
|
|
*/
|
|
static detectType(value) {
|
|
if (typeof value === 'number') {
|
|
return 'number';
|
|
}
|
|
if (typeof value === 'boolean') {
|
|
return 'boolean';
|
|
}
|
|
if (Array.isArray(value)) {
|
|
return 'array';
|
|
}
|
|
if (typeof value === 'object' && value !== null) {
|
|
return 'json';
|
|
}
|
|
return 'string';
|
|
}
|
|
}
|
|
|
|
// 初始化SystemConfig模型
|
|
SystemConfig.init({
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
primaryKey: true,
|
|
autoIncrement: true,
|
|
comment: '配置ID'
|
|
},
|
|
config_key: {
|
|
type: DataTypes.STRING(100),
|
|
allowNull: false,
|
|
unique: true,
|
|
comment: '配置键名'
|
|
},
|
|
config_value: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: true,
|
|
comment: '配置值'
|
|
},
|
|
config_type: {
|
|
type: DataTypes.ENUM('string', 'number', 'boolean', 'json', 'array'),
|
|
allowNull: false,
|
|
defaultValue: 'string',
|
|
comment: '配置类型'
|
|
},
|
|
category: {
|
|
type: DataTypes.STRING(50),
|
|
allowNull: false,
|
|
defaultValue: 'general',
|
|
comment: '配置分类'
|
|
},
|
|
description: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
comment: '配置描述'
|
|
},
|
|
is_public: {
|
|
type: DataTypes.BOOLEAN,
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
comment: '是否公开(前端可访问)'
|
|
},
|
|
is_editable: {
|
|
type: DataTypes.BOOLEAN,
|
|
allowNull: false,
|
|
defaultValue: true,
|
|
comment: '是否可编辑'
|
|
},
|
|
sort_order: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false,
|
|
defaultValue: 0,
|
|
comment: '排序顺序'
|
|
},
|
|
updated_by: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: true,
|
|
comment: '最后更新人ID'
|
|
}
|
|
}, {
|
|
sequelize,
|
|
tableName: 'system_configs',
|
|
modelName: 'SystemConfig',
|
|
comment: '系统配置表',
|
|
indexes: [
|
|
{
|
|
fields: ['config_key'],
|
|
unique: true
|
|
},
|
|
{
|
|
fields: ['category']
|
|
},
|
|
{
|
|
fields: ['is_public']
|
|
}
|
|
]
|
|
});
|
|
|
|
module.exports = SystemConfig; |