accesss.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict';
  2. const ManagerController = require('./manager.js');
  3. /**
  4. * [exports 权限节点控制骂]
  5. * @type {[type]}
  6. */
  7. module.exports = class AccesssController 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.Accesss;
  16. }
  17. /**
  18. * [createValidate 添加数据验证提示 必填]
  19. * @author szjcomo
  20. * @date 2020-10-23
  21. * @return {[type]} [description]
  22. */
  23. get createValidate() {
  24. let that = this;
  25. return {
  26. access_name:that.ctx.rules.name('节点名称').required().min_length(2).max_length(20),
  27. router_name:that.ctx.rules.name('路由名称').required().min_length(2).max_length(100),
  28. pid:that.ctx.rules.default(0).number(),
  29. router_path:that.ctx.rules.default('').required(),
  30. admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number(),
  31. access_icon:that.ctx.rules.default('el-icon-menu').required(),
  32. is_nav:that.ctx.rules.default(0).number(),
  33. access_sort:that.ctx.rules.default(1).number(),
  34. access_status:that.ctx.rules.default(1).number(),
  35. create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required()
  36. };
  37. }
  38. /**
  39. * [select 重写查询]
  40. * @author szjcomo
  41. * @date 2020-11-02
  42. * @return {[type]} [description]
  43. */
  44. async select() {
  45. let that = this;
  46. try {
  47. let data = await that.ctx.validate(that.selectValidate,await that.ctx.getParse());
  48. let selectBean = that.app.comoBean.instance(data,{
  49. where:{},offset:(data.page - 1) * data.limit,limit:data.limit
  50. });
  51. selectBean.addCall(that.selectBeanBefore);
  52. let result = await that.service.manager.select(selectBean,that.useModel,'权限节点查询失败,请稍候重试',true,true);
  53. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  54. } catch(err) {
  55. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  56. }
  57. }
  58. /**
  59. * [selectBeanBefore 查询前置操作]
  60. * @author szjcomo
  61. * @date 2020-11-02
  62. * @param {[type]} app [description]
  63. * @return {[type]} [description]
  64. */
  65. async selectBeanBefore(app) {
  66. let that = this;
  67. let seq = app.Sequelize;
  68. let options = that.getOptions();
  69. let data = that.getData();
  70. options.raw = true;
  71. if(data.access_id == 0) options.order = [['access_sort','asc'],['access_id','asc']];
  72. options.include = [
  73. {model:app.model.AdminUser,as:'admin_user',attributes:[]}
  74. ];
  75. options.attributes = {
  76. include:[
  77. [seq.col('admin_user.username'),'username']
  78. ],
  79. exclude:['admin_id']
  80. };
  81. that.setOptions(options);
  82. }
  83. /**
  84. * [create 重写添加]
  85. * @author szjcomo
  86. * @date 2020-11-02
  87. * @return {[type]} [description]
  88. */
  89. async create() {
  90. let that = this;
  91. try {
  92. let data = await that.ctx.validate(that.createValidate,await that.ctx.postParse());
  93. let level = await that.service.manager.getLevel(data.pid,that.useModel,{
  94. where:{access_id:data.pid},attributes:['level']
  95. });
  96. data.level = level;
  97. if(that.app.szjcomo.empty(data.access_icon)) data.access_icon = 'el-icon-menu';
  98. let createBean = that.app.comoBean.instance(data);
  99. let result = await that.service.manager.create(createBean,that.useModel,'权限节点添加失败,请稍候重试');
  100. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  101. } catch(err) {
  102. console.log(err)
  103. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  104. }
  105. }
  106. /**
  107. * [update 重写更新]
  108. * @author szjcomo
  109. * @date 2020-11-02
  110. * @return {[type]} [description]
  111. */
  112. async update() {
  113. let that = this;
  114. try {
  115. let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
  116. data.admin_id = that.service.manager.ActionAdminUserId();
  117. if(data.hasOwnProperty('pid')) {
  118. let level = await that.service.manager.getLevel(data.pid,that.useModel,{
  119. where:{access_id:data.pid},attributes:['level']
  120. });
  121. data.level = level;
  122. }
  123. if(data.hasOwnProperty('access_icon')) {
  124. data.access_icon = await that.ctx.anyParse('access_icon','el-icon-menu',(value)=>{return value;});
  125. }
  126. let updateBean = that.app.comoBean.instance(data,{where:{access_id:data.access_id}});
  127. let result = await that.service.manager.update(updateBean,that.useModel,'权限节点更新失败,请稍候重试');
  128. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  129. } catch(err) {
  130. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  131. }
  132. }
  133. /**
  134. * [delete 重写删除]
  135. * @author szjcomo
  136. * @date 2020-11-02
  137. * @return {[type]} [description]
  138. */
  139. async delete() {
  140. let that = this;
  141. try {
  142. let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
  143. let deleteBean = that.app.comoBean.instance(data,{where:{access_id:data.access_id}});
  144. deleteBean.addCall(that.deleteBeanBefore);
  145. let result = await that.service.manager.delete(deleteBean,that.useModel,'权限节点删除失败,请稍候重试');
  146. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  147. } catch(err) {
  148. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  149. }
  150. }
  151. /**
  152. * [deleteBeanBefore 删除节点前检查]
  153. * @author szjcomo
  154. * @date 2020-11-02
  155. * @param {[type]} app [description]
  156. * @return {[type]} [description]
  157. */
  158. async deleteBeanBefore(app) {
  159. let that = this;
  160. let data = that.getData();
  161. let info = await app.model.Accesss.findOne({where:{pid:data.access_id},attributes:['access_id']});
  162. if(info) throw new Error('当前节点下还有子节点,无法删除,如果需要删除,请先删除子节点后再删除本节点数据');
  163. }
  164. }