admin_user.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. const ManagerController = require('./manager.js');
  3. /**
  4. * [exports 后台管理员控制器]
  5. * @type {[type]}
  6. */
  7. module.exports = class AdminUser extends ManagerController {
  8. /**
  9. * [useModel 使用模型]
  10. * @author szjcomo
  11. * @date 2020-11-02
  12. * @return {[type]} [description]
  13. */
  14. get useModel() {
  15. const that = this;
  16. return that.app.model.AdminUser;
  17. }
  18. /**
  19. * [createValidate 添加管理员验证器]
  20. * @author szjcomo
  21. * @date 2020-11-02
  22. * @return {[type]} [description]
  23. */
  24. get createValidate() {
  25. const that = this;
  26. return {
  27. username: that.ctx.rules.name('管理员名称').required().min_length(2)
  28. .max_length(20)
  29. .extend(async (field, value) => {
  30. const result = await that.ctx.model.AdminUser.findOne({ where: { username: value }, attributes: [ 'admin_id' ] });
  31. if (result) throw new Error('管理员名称已经存在,无法重复添加');
  32. }),
  33. password: that.ctx.rules.name('管理员密码').required().min_length(6)
  34. .max_length(20),
  35. role_id: that.ctx.rules.name('管理员角色').required().number(),
  36. admin_status: that.ctx.rules.default(1).required().number(),
  37. login_time: that.ctx.rules.default('').required(),
  38. admin_code: that.ctx.rules.default(that.app.szjcomo.mt_rand(1000, 9999)).required(),
  39. create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required(),
  40. action_user: that.ctx.rules.default(that.service.manager.ActionAdminUserId()).required().number(),
  41. };
  42. }
  43. /**
  44. * [selectValidate 查询验证器]
  45. * @author szjcomo
  46. * @date 2020-11-02
  47. * @return {[type]} [description]
  48. */
  49. get selectValidate() {
  50. const that = this;
  51. return {
  52. username: that.ctx.rules.default('').required(),
  53. role_id: that.ctx.rules.default('').required(),
  54. limit: that.ctx.rules.default(20).number(),
  55. page: that.ctx.rules.default(1).number(),
  56. admin_id: that.ctx.rules.default(0).number(),
  57. };
  58. }
  59. /**
  60. * [updateValidate 数据更新功能]
  61. * @author szjcomo
  62. * @date 2020-11-02
  63. * @return {[type]} [description]
  64. */
  65. get updateValidate() {
  66. const that = this;
  67. return {
  68. admin_id: that.ctx.rules.name('管理员ID').required().number(),
  69. action_user: that.ctx.rules.default(that.service.manager.ActionAdminUserId()).required(),
  70. update_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required(),
  71. };
  72. }
  73. /**
  74. * [create 添加数据实现]
  75. * @author szjcomo
  76. * @date 2020-11-02
  77. * @return {[type]} [description]
  78. */
  79. async create() {
  80. const that = this;
  81. try {
  82. const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse());
  83. const password = that.app.szjcomo.MD5(`${data.password}${data.admin_code}`);
  84. data.password = password;
  85. const createBean = that.app.comoBean.instance(data);
  86. const result = await that.service.manager.create(createBean, that.useModel, '管理员添加失败,请稍候重试');
  87. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  88. } catch (err) {
  89. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  90. }
  91. }
  92. /**
  93. * [select 管理员查询自定义实现]
  94. * @author szjcomo
  95. * @date 2020-11-02
  96. * @return {[type]} [description]
  97. */
  98. async select() {
  99. const that = this;
  100. try {
  101. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  102. const seq = that.app.Sequelize;
  103. let options = {};
  104. if (data.admin_id > 0) {
  105. options.where = {};
  106. options.where.admin_id = data.admin_id;
  107. } else {
  108. options = {
  109. offset: (data.page - 1) * data.limit,
  110. limit: data.limit,
  111. include: [{ model: that.app.model.Roles, as: 'role', attributes: [] }],
  112. attributes: {
  113. include: [[ seq.col('role.role_name'), 'role_name' ]],
  114. exclude: [ 'password', 'action_user', 'admin_code', 'login_count' ],
  115. },
  116. order: [[ 'admin_id', 'desc' ]],
  117. };
  118. options.where = { role_id: { [seq.Op.gt]: 0 } };
  119. if (data.username) options.where.username = { [seq.Op.regexp]: `${data.username}` };
  120. if (data.role_id) options.where.role_id = Number(data.role_id);
  121. }
  122. const selectBean = that.app.comoBean.instance(data, options);
  123. const count = !(data.admin_id > 0);
  124. const result = await that.service.manager.select(selectBean, that.useModel, '管理员查询失败,请稍候重试', count, count);
  125. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  126. } catch (err) {
  127. console.log(err);
  128. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  129. }
  130. }
  131. /**
  132. * [update 编辑管理员信息]
  133. * @author szjcomo
  134. * @date 2020-11-02
  135. * @return {[type]} [description]
  136. */
  137. async update() {
  138. const that = this;
  139. try {
  140. const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse());
  141. const updateBean = that.app.comoBean.instance(data, { where: { admin_id: data.admin_id } });
  142. updateBean.addCall(that.updateBeanBefore);
  143. const result = await that.service.manager.update(updateBean, that.useModel, '管理员信息更新失败,请稍候重试');
  144. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  145. } catch (err) {
  146. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  147. }
  148. }
  149. /**
  150. * [updateBeanBefore 更新前置操作]
  151. * @author szjcomo
  152. * @date 2020-11-02
  153. * @param {[type]} app [description]
  154. * @return {[type]} [description]
  155. */
  156. async updateBeanBefore(app) {
  157. const that = this;
  158. const data = that.getData();
  159. if (data.hasOwnProperty('role_id') && data.role_id == 0) throw new Error('管理员角色ID不能为空,请检查');
  160. if (data.username && (data.username.length < 2 || data.username.length > 20)) throw new Error('管理员账号名称长度不符号要求,长度最小为2个字符,最大20个字符');
  161. if (data.username) {
  162. const user = await app.model.AdminUser.findOne({ where: { username: data.username }, attributes: [ 'admin_id' ] });
  163. if (user && (data.admin_id != user.admin_id)) throw new Error('管理员账号已经被占用,请重新改一个管理员账号名称吧!');
  164. }
  165. if (data.password) {
  166. if (data.password.length < 6) throw new Error('密码太弱,请重新设置,密码最小长度需要6位,最大20位长度');
  167. const userinfo = await app.model.AdminUser.findOne({
  168. where: { admin_id: data.admin_id }, raw: true,
  169. attributes: [ 'admin_code', 'password' ],
  170. });
  171. if (!userinfo) throw new Error('管理员信息不存在,请检查');
  172. if (userinfo.password != data.password) {
  173. const password = app.szjcomo.MD5(`${data.password}${userinfo.admin_code}`);
  174. data.password = password;
  175. } else {
  176. delete data.password;
  177. }
  178. }
  179. that.setData(data);
  180. }
  181. };