'use strict'; const ManagerController = require('./manager.js'); /** * [exports 配置管理控制骂] * @type {[type]} */ module.exports = class ConfigsController extends ManagerController { /** * [actionModel 当前操作模型 必填] * @author szjcomo * @date 2020-10-23 * @return {[type]} [description] */ get useModel() { return this.app.model.Configs; } /** * [clearConfigCache 清除缓存配置] * @return {[type]} [description] */ async clearConfigCache() { let that = this; await that.service.configs.clear(); } /** * [createValidate 添加数据验证提示 必填] * @author szjcomo * @date 2020-10-23 * @return {[type]} [description] */ get createValidate() { let that = this; return { field_index:that.ctx.rules.name('索引字段').required().min_length(1).max_length(100).extend(async (field,value) => { let result = await that.ctx.model.Configs.findOne({where:{field_index:value},attributes:['config_id']}); if(result) throw new Error('索引字段已经存在,请不要重复添加'); }), field_label:that.ctx.rules.name('字段label').required().min_length(1).max_length(100), field_type:that.ctx.rules.default('input').required(), field_group:that.ctx.rules.default('system').required(), field_options:that.ctx.rules.default('').required(), field_desc:that.ctx.rules.default('').required(), field_status:that.ctx.rules.default(1).required(), field_value:that.ctx.rules.default('').required(), field_sort:that.ctx.rules.default(1).required().number(), admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).required().number(), create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required() }; } /** * [settingValidate 配置设置] * @author szjcomo * @date 2020-10-31 * @return {[type]} [description] */ get settingValidate() { let that = this; return { setting:that.ctx.rules.name('设置参数').required().is_array() }; } /** * [select 重写查询] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async select() { let that = this; try { let data = await that.ctx.validate(that.selectValidate,await that.ctx.getParse()); let selectBean = that.app.comoBean.instance(data,{}); selectBean.addCall(that.selectBeanBefore); selectBean.addCall(that.selectBeanAfter,'after'); let result = await that.service.manager.select(selectBean,that.useModel,'配置查询失败,请稍候重试',true,true); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [selectBeanBefore 查询前置操作] * @author szjcomo * @date 2020-11-02 * @param {[type]} app [description] * @return {[type]} [description] */ async selectBeanBefore(app) { let that = this; let data = that.getData(); if(data.config_id == 0) { let seq = app.Sequelize; let options = {}; options.include = [ {model:app.model.AdminUser,as:'admin_user',attributes:[]} ]; options.order = [['field_sort','asc'],['config_id','asc']]; let exclude = ['admin_id','create_time']; if(data.field_status) exclude = ['field_sort','admin_id','create_time','update_time']; options.attributes = { include:[[seq.col('admin_user.username'),'username']], exclude:exclude }; options.raw = true; if(data.field_status) options.where = {field_status:1}; that.setOptions(options); } } /** * [selectBeanAfter 查询后置操作] * @author szjcomo * @date 2020-11-02 * @param {[type]} app [description] * @param {[type]} result [description] * @return {[type]} [description] */ async selectBeanAfter(app,result) { let that = this; let data = that.getData(); if(data.config_id > 0) return result; /** * [groups 分解分组] * @author szjcomo * @date 2020-10-31 * @param {[type]} value [description] * @return {[type]} [description] */ function groups(value) { let arr = value.split('\n'); let res = []; arr.forEach(item => { let tmp = item.split('|'); res.push({config_title:tmp[1],config_name:app.szjcomo.trim(tmp[0]),groups:[]}); }) return res; } let system_groups = await app.model.Configs.findOne({where:{field_index:'system_group'},attributes:['field_value'],raw:true}); if(!system_groups) throw new Error('未设置系统总类分组选项,请检查'); let arr = groups(system_groups.field_value); result.rows.forEach(item => { arr.forEach(childItem => { if(item.field_group == childItem.config_name) { if(item.field_type == 'checkbox' && item.field_value) item.field_value = app.szjcomo.json_decode(item.field_value); let tmparr = ['radio','number','switch','select']; if(app.szjcomo.inArray(tmparr,item.field_type)) item.field_value = Number(item.field_value); childItem.groups.push(item); } }) }) return arr; } /** * [create 重写添加方法] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async create() { let that = this; try { let data = await that.ctx.validate(that.createValidate,await that.ctx.postParse()); let field_value = await that.ctx.postParse('field_value','',(value)=>{return value;}); let field_options = await that.ctx.postParse('field_options','',(value)=>{return value;}); data.field_value = field_value; data.field_options = field_options; let createBean = that.app.comoBean.instance(data); let result = await that.service.manager.create(createBean,that.useModel,'配置添加失败,请稍候重试'); await that.clearConfigCache(); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [update 重写更新方法] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async update() { let that = this; try { let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse()); data.admin_id = that.service.manager.ActionAdminUserId(); if(data.field_value && data.field_value.length > 0) { let field_value = await that.ctx.anyParse('field_value','',(value) => {return value;}); data.field_value = field_value; } if(data.field_options && data.field_options.length > 0) { let field_options = await that.ctx.anyParse('field_options','',(value) => {return value;}); data.field_options = field_options; } let options = {where:{config_id:data.config_id}}; let updateBean = that.app.comoBean.instance(data,options); let result = await that.service.manager.update(updateBean,that.useModel,'配置更新失败,请稍候重试'); await that.clearConfigCache(); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [setting 配置设置] * @author szjcomo * @date 2020-10-31 * @return {[type]} [description] */ async setting() { let that = this; let transaction; try { let data = await that.ctx.validate(that.settingValidate,await that.ctx.postParse()); transaction = await that.app.model.transaction(); let admin_id = that.service.manager.ActionAdminUserId(); let length = data.setting.length; for(let i = 0;i < length;i++) { let item = data.setting[i]; let field_value = (item.field_type == 'checkbox' && item.field_value)?that.app.szjcomo.json_encode(item.field_value):item.field_value; await that.app.model.Configs.update({field_value:field_value,admin_id:admin_id},{ where:{config_id:item.config_id},fields:['field_value','admin_id'],transaction:transaction }); } await transaction.commit(); await that.clearConfigCache(); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',null,false)); } catch(err) { if(transaction) await transaction.rollback(); return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } }