roles.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. const ManagerController = require('./manager.js');
  3. /**
  4. * [exports {:this}控制器]
  5. * @type {[type]}
  6. */
  7. module.exports = class RolesController extends ManagerController {
  8. /**
  9. * [actionModel 当前操作模型 必填]
  10. * @author szjcomo
  11. * @date 2020-10-23
  12. * @return {[type]} [description]
  13. */
  14. get useModel() {
  15. return this.ctx.model.Roles;
  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. role_name:that.ctx.rules.name('角色名称').required().max_length(19).min_length(2),
  27. role_desc:that.ctx.rules.default('').max_length(255),
  28. create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required(),
  29. admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number()
  30. };
  31. }
  32. /**
  33. * [roleAuthValidate 角色授权]
  34. * @author szjcomo
  35. * @date 2020-10-23
  36. * @return {[type]} [description]
  37. */
  38. get roleAuthValidate() {
  39. let that = this;
  40. return {
  41. role_id:that.ctx.rules.name('角色ID').required().number(),
  42. access_id:that.ctx.rules.name('节点ID').required()
  43. };
  44. }
  45. /**
  46. * [select 重写查询接口]
  47. * @author szjcomo
  48. * @date 2020-11-02
  49. * @return {[type]} [description]
  50. */
  51. async select() {
  52. let that = this;
  53. try {
  54. let data = await that.ctx.validate(that.selectValidate,await that.ctx.getParse());
  55. let seq = that.app.Sequelize;
  56. let options = {
  57. offset:(data.page - 1) * data.limit,
  58. limit:data.limit,
  59. include:[{model:that.app.model.AdminUser,as:'admin_user',attributes:[]}],
  60. attributes:{
  61. include:[[seq.col('admin_user.username'),'username']],
  62. exclude:['admin_id']
  63. },
  64. order:[['role_id','desc']]
  65. };
  66. let selectBean = that.app.comoBean.instance(data,options);
  67. let result = await that.service.manager.select(selectBean,that.useModel,'角色查询失败,请稍候重试',true,true);
  68. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  69. } catch(err) {
  70. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  71. }
  72. }
  73. /**
  74. * [delete 重写删除接口]
  75. * @author szjcomo
  76. * @date 2020-11-02
  77. * @return {[type]} [description]
  78. */
  79. async delete() {
  80. let that = this;
  81. let transaction;
  82. try {
  83. let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
  84. transaction = await that.app.model.transaction();
  85. let deleteBean = that.app.comoBean.instance(data,{where:{role_id:data.role_id},transaction:transaction});
  86. deleteBean.addCall(that.deleteBeanBefore);
  87. let result = await that.service.manager.delete(deleteBean,that.useModel,'角色删除失败,请稍候重试');
  88. await transaction.commit();
  89. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  90. } catch(err) {
  91. if(transaction) await transaction.rollback();
  92. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  93. }
  94. }
  95. /**
  96. * [deleteBeanBefore 删除管理员前置检查]
  97. * @author szjcomo
  98. * @date 2020-11-02
  99. * @param {[type]} app [description]
  100. * @return {[type]} [description]
  101. */
  102. async deleteBeanBefore(app) {
  103. let that = this;
  104. let data = that.getData();
  105. let options = that.getOptions();
  106. let result = await app.model.AdminUser.findOne({where:{role_id:data.role_id},attributes:['admin_id']});
  107. if(result) throw new Error('当前角色还有管理员在使用,请先处理后再进行删除');
  108. await app.model.AccessRole.destroy({where:{role_id:data.role_id},transaction:options.transaction});
  109. }
  110. /**
  111. * [roleAuth 给角色授权]
  112. * @author szjcomo
  113. * @date 2020-10-23
  114. * @return {[type]} [description]
  115. */
  116. async roleAuth() {
  117. let that = this;
  118. let transaction;
  119. try {
  120. let data = await that.ctx.validate(that.roleAuthValidate,await that.ctx.postParse());
  121. transaction = await that.app.model.transaction();
  122. let modelBean = that.app.comoBean.instance(data,{transaction:transaction});
  123. modelBean.addCall(that.roleAuthBeforeHandle);
  124. let result = await that.app.comoBean.create(modelBean,that.app.model.AccessRole,'角色授权失败,请稍候重试');
  125. await transaction.commit();
  126. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  127. } catch(err) {
  128. if(transaction) await transaction.rollback();
  129. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  130. }
  131. }
  132. /**
  133. * [roleAuthBeforeHandle 角色授权前置检测]
  134. * @author szjcomo
  135. * @date 2020-10-23
  136. * @param {[type]} app [description]
  137. * @return {[type]} [description]
  138. */
  139. async roleAuthBeforeHandle(app) {
  140. let that = this;
  141. let data = that.getData();
  142. let options = that.getOptions();
  143. let info = await app.model.AccessRole.findOne({where:{role_id:data.role_id},attributes:['role_id']});
  144. let deleteRole = true;
  145. if(info) deleteRole = await app.model.AccessRole.destroy({where:{role_id:data.role_id},transaction:options.transaction});
  146. if(!deleteRole) throw new Error('角色授权前删除失败,请联系管理员处理!');
  147. }
  148. /**
  149. * [getRoleAuth 获取角色权限]
  150. * @author szjcomo
  151. * @date 2020-10-23
  152. * @return {[type]} [description]
  153. */
  154. async getRoleAuth() {
  155. let that = this;
  156. try {
  157. let role_id = await that.ctx.getParse('role_id',0,Number);
  158. if(role_id <= 0) throw new Error('角色参数错误,请检查是否正确');
  159. let result = await that.service.manager.getRoleAuth(role_id);
  160. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  161. } catch(err) {
  162. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  163. }
  164. }
  165. }