configs.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict';
  2. const ManagerController = require('./manager.js');
  3. /**
  4. * [exports 配置管理控制骂]
  5. * @type {[type]}
  6. */
  7. module.exports = class ConfigsController extends ManagerController {
  8. /**
  9. * [actionModel 当前操作模型 必填]
  10. * @author szjcomo
  11. * @date 2020-10-23
  12. * @return {[type]} [description]
  13. */
  14. get useModel() {
  15. return this.app.model.Configs;
  16. }
  17. /**
  18. * [clearConfigCache 清除缓存配置]
  19. * @return {[type]} [description]
  20. */
  21. async clearConfigCache() {
  22. let that = this;
  23. await that.service.configs.clear();
  24. }
  25. /**
  26. * [createValidate 添加数据验证提示 必填]
  27. * @author szjcomo
  28. * @date 2020-10-23
  29. * @return {[type]} [description]
  30. */
  31. get createValidate() {
  32. let that = this;
  33. return {
  34. field_index:that.ctx.rules.name('索引字段').required().min_length(1).max_length(100).extend(async (field,value) => {
  35. let result = await that.ctx.model.Configs.findOne({where:{field_index:value},attributes:['config_id']});
  36. if(result) throw new Error('索引字段已经存在,请不要重复添加');
  37. }),
  38. field_label:that.ctx.rules.name('字段label').required().min_length(1).max_length(100),
  39. field_type:that.ctx.rules.default('input').required(),
  40. field_group:that.ctx.rules.default('system').required(),
  41. field_options:that.ctx.rules.default('').required(),
  42. field_desc:that.ctx.rules.default('').required(),
  43. field_status:that.ctx.rules.default(1).required(),
  44. field_value:that.ctx.rules.default('').required(),
  45. field_sort:that.ctx.rules.default(1).required().number(),
  46. admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).required().number(),
  47. create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required()
  48. };
  49. }
  50. /**
  51. * [settingValidate 配置设置]
  52. * @author szjcomo
  53. * @date 2020-10-31
  54. * @return {[type]} [description]
  55. */
  56. get settingValidate() {
  57. let that = this;
  58. return {
  59. setting:that.ctx.rules.name('设置参数').required().is_array()
  60. };
  61. }
  62. /**
  63. * [select 重写查询]
  64. * @author szjcomo
  65. * @date 2020-11-02
  66. * @return {[type]} [description]
  67. */
  68. async select() {
  69. let that = this;
  70. try {
  71. let data = await that.ctx.validate(that.selectValidate,await that.ctx.getParse());
  72. let selectBean = that.app.comoBean.instance(data,{});
  73. selectBean.addCall(that.selectBeanBefore);
  74. selectBean.addCall(that.selectBeanAfter,'after');
  75. let result = await that.service.manager.select(selectBean,that.useModel,'配置查询失败,请稍候重试',true,true);
  76. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  77. } catch(err) {
  78. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  79. }
  80. }
  81. /**
  82. * [selectBeanBefore 查询前置操作]
  83. * @author szjcomo
  84. * @date 2020-11-02
  85. * @param {[type]} app [description]
  86. * @return {[type]} [description]
  87. */
  88. async selectBeanBefore(app) {
  89. let that = this;
  90. let data = that.getData();
  91. if(data.config_id == 0) {
  92. let seq = app.Sequelize;
  93. let options = {};
  94. options.include = [
  95. {model:app.model.AdminUser,as:'admin_user',attributes:[]}
  96. ];
  97. options.order = [['field_sort','asc'],['config_id','asc']];
  98. let exclude = ['admin_id','create_time'];
  99. if(data.field_status) exclude = ['field_sort','admin_id','create_time','update_time'];
  100. options.attributes = {
  101. include:[[seq.col('admin_user.username'),'username']],
  102. exclude:exclude
  103. };
  104. options.raw = true;
  105. if(data.field_status) options.where = {field_status:1};
  106. that.setOptions(options);
  107. }
  108. }
  109. /**
  110. * [selectBeanAfter 查询后置操作]
  111. * @author szjcomo
  112. * @date 2020-11-02
  113. * @param {[type]} app [description]
  114. * @param {[type]} result [description]
  115. * @return {[type]} [description]
  116. */
  117. async selectBeanAfter(app,result) {
  118. let that = this;
  119. let data = that.getData();
  120. if(data.config_id > 0) return result;
  121. /**
  122. * [groups 分解分组]
  123. * @author szjcomo
  124. * @date 2020-10-31
  125. * @param {[type]} value [description]
  126. * @return {[type]} [description]
  127. */
  128. function groups(value) {
  129. let arr = value.split('\n');
  130. let res = [];
  131. arr.forEach(item => {
  132. let tmp = item.split('|');
  133. res.push({config_title:tmp[1],config_name:app.szjcomo.trim(tmp[0]),groups:[]});
  134. })
  135. return res;
  136. }
  137. let system_groups = await app.model.Configs.findOne({where:{field_index:'system_group'},attributes:['field_value'],raw:true});
  138. if(!system_groups) throw new Error('未设置系统总类分组选项,请检查');
  139. let arr = groups(system_groups.field_value);
  140. result.rows.forEach(item => {
  141. arr.forEach(childItem => {
  142. if(item.field_group == childItem.config_name) {
  143. if(item.field_type == 'checkbox' && item.field_value) item.field_value = app.szjcomo.json_decode(item.field_value);
  144. let tmparr = ['radio','number','switch','select'];
  145. if(app.szjcomo.inArray(tmparr,item.field_type)) item.field_value = Number(item.field_value);
  146. childItem.groups.push(item);
  147. }
  148. })
  149. })
  150. return arr;
  151. }
  152. /**
  153. * [create 重写添加方法]
  154. * @author szjcomo
  155. * @date 2020-11-02
  156. * @return {[type]} [description]
  157. */
  158. async create() {
  159. let that = this;
  160. try {
  161. let data = await that.ctx.validate(that.createValidate,await that.ctx.postParse());
  162. let field_value = await that.ctx.postParse('field_value','',(value)=>{return value;});
  163. let field_options = await that.ctx.postParse('field_options','',(value)=>{return value;});
  164. data.field_value = field_value;
  165. data.field_options = field_options;
  166. let createBean = that.app.comoBean.instance(data);
  167. let result = await that.service.manager.create(createBean,that.useModel,'配置添加失败,请稍候重试');
  168. await that.clearConfigCache();
  169. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  170. } catch(err) {
  171. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  172. }
  173. }
  174. /**
  175. * [update 重写更新方法]
  176. * @author szjcomo
  177. * @date 2020-11-02
  178. * @return {[type]} [description]
  179. */
  180. async update() {
  181. let that = this;
  182. try {
  183. let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
  184. data.admin_id = that.service.manager.ActionAdminUserId();
  185. if(data.field_value && data.field_value.length > 0) {
  186. let field_value = await that.ctx.anyParse('field_value','',(value) => {return value;});
  187. data.field_value = field_value;
  188. }
  189. if(data.field_options && data.field_options.length > 0) {
  190. let field_options = await that.ctx.anyParse('field_options','',(value) => {return value;});
  191. data.field_options = field_options;
  192. }
  193. let options = {where:{config_id:data.config_id}};
  194. let updateBean = that.app.comoBean.instance(data,options);
  195. let result = await that.service.manager.update(updateBean,that.useModel,'配置更新失败,请稍候重试');
  196. await that.clearConfigCache();
  197. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  198. } catch(err) {
  199. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  200. }
  201. }
  202. /**
  203. * [setting 配置设置]
  204. * @author szjcomo
  205. * @date 2020-10-31
  206. * @return {[type]} [description]
  207. */
  208. async setting() {
  209. let that = this;
  210. let transaction;
  211. try {
  212. let data = await that.ctx.validate(that.settingValidate,await that.ctx.postParse());
  213. transaction = await that.app.model.transaction();
  214. let admin_id = that.service.manager.ActionAdminUserId();
  215. let length = data.setting.length;
  216. for(let i = 0;i < length;i++) {
  217. let item = data.setting[i];
  218. let field_value = (item.field_type == 'checkbox' && item.field_value)?that.app.szjcomo.json_encode(item.field_value):item.field_value;
  219. await that.app.model.Configs.update({field_value:field_value,admin_id:admin_id},{
  220. where:{config_id:item.config_id},fields:['field_value','admin_id'],transaction:transaction
  221. });
  222. }
  223. await transaction.commit();
  224. await that.clearConfigCache();
  225. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',null,false));
  226. } catch(err) {
  227. if(transaction) await transaction.rollback();
  228. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  229. }
  230. }
  231. }