login.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. const ManagerController = require('./manager.js');
  3. /**
  4. * [exports 用户登录退出控制器]
  5. * @type {[type]}
  6. */
  7. module.exports = class LoginController extends ManagerController {
  8. /**
  9. * [dologinValidate 登录验证器]
  10. * @author szjcomo
  11. * @date 2020-11-02
  12. * @return {[type]} [description]
  13. */
  14. get dologinValidate() {
  15. const that = this;
  16. return {
  17. username: that.ctx.rules.name('管理员账号').required().notEmpty()
  18. .trim(),
  19. password: that.ctx.rules.name('管理员密码').required().notEmpty()
  20. .trim(),
  21. mobile: that.ctx.rules.default(0).number(),
  22. verify_code: that.ctx.rules.name('验证码').required().trim()
  23. .notEmpty()
  24. .number(),
  25. };
  26. }
  27. /**
  28. * [dologin 后台管理执行登录功能]
  29. * @author szjcomo
  30. * @date 2020-11-02
  31. * @return {[type]} [description]
  32. */
  33. async dologin() {
  34. const that = this;
  35. try {
  36. const data = await that.ctx.validate(that.dologinValidate, await that.ctx.postParse());
  37. data.login_ip = that.ctx.request.ip;
  38. const verify_code = that.ctx.session.verify_code;
  39. if (process.env.NODE_ENV != 'development') {
  40. if (!data.mobile) {
  41. if (verify_code != data.verify_code) throw new Error('验证码输入错误,请检查...');
  42. }
  43. }
  44. const loginBean = that.app.comoBean.instance(data, { where: { username: data.username } });
  45. loginBean.addCall(that.dologinBeanBefore);
  46. loginBean.addCall(that.dologinBeanAfter, 'after');
  47. const result = await that.service.manager.select(loginBean, that.app.model.AdminUser, '管理员账号查询失败,请检查账号或密码是否正确');
  48. result.mobile = data.mobile;
  49. const token = that.app.jwt.sign(result, that.app.config.jwt.secret, { expiresIn: '10h' });
  50. const menus = await that.service.manager.getRoleAuth(result.role_id, true, data.mobile);
  51. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', {
  52. admin_user: result,
  53. menus: that.app.szjcomo.base64_encode(that.app.szjcomo.json_encode(menus)),
  54. token, admin_config: await that.service.manager.adminConfig(),
  55. }, false));
  56. } catch (err) {
  57. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  58. }
  59. }
  60. /**
  61. * [dologinBeanBefore 登录前数据处理]
  62. * @author szjcomo
  63. * @date 2020-11-02
  64. * @param {[type]} app [description]
  65. * @return {[type]} [description]
  66. */
  67. async dologinBeanBefore(app) {
  68. const that = this;
  69. const options = that.getOptions();
  70. const data = that.getData();
  71. const result = await app.model.AdminUser.findOne({
  72. where: { username: data.username }, raw: true,
  73. attributes: [ 'admin_code', 'admin_id', 'admin_status' ],
  74. });
  75. if (!result) throw new Error('管理员账号是否有误,请检查...');
  76. if (!result.admin_status) throw new Error('您的账号已经被禁用,请联系管理员处理');
  77. const password = app.szjcomo.MD5(`${data.password}${result.admin_code}`);
  78. if (!options.where) options.where = {};
  79. options.where.password = password;
  80. data.admin_id = result.admin_id;
  81. options.attributes = [ 'username', 'admin_id', 'admin_status', 'login_time', 'login_count', 'login_ip', 'role_id' ];
  82. options.raw = true;
  83. that.setOptions(options);
  84. that.setData(data);
  85. }
  86. /**
  87. * [dologinBeanAfter 登录成功后需更新登录信息]
  88. * @author szjcomo
  89. * @date 2020-11-02
  90. * @param {[type]} app [description]
  91. * @param {[type]} result [description]
  92. * @return {[type]} [description]
  93. */
  94. async dologinBeanAfter(app, result) {
  95. const that = this;
  96. const data = that.getData();
  97. const login_time = app.szjcomo.date('Y-m-d H:i:s');
  98. const update_data = { login_ip: data.login_ip, login_time, login_count: app.Sequelize.literal('login_count+1') };
  99. const res = await app.model.AdminUser.update(update_data, { where: { admin_id: data.admin_id } });
  100. if (!res[0]) app.logger.error('管理员登录信息更新失败,需要管理员处理...');
  101. }
  102. /**
  103. * [verify 登录验证码]
  104. * @author szjcomo
  105. * @date 2020-11-02
  106. * @return {[type]} [description]
  107. */
  108. async verify() {
  109. const that = this;
  110. try {
  111. const result = that.service.captcha.get();
  112. that.ctx.session.verify_code = result.text;
  113. that.ctx.response.type = 'image/svg+xml';
  114. return that.ctx.body = result.data;
  115. } catch (err) {
  116. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  117. }
  118. }
  119. /**
  120. * [manager 控制台数据]
  121. * @author szjcomo
  122. * @date 2020-11-02
  123. * @return {[type]} [description]
  124. */
  125. async manager() {
  126. const that = this;
  127. try {
  128. const result = await that.service.manager.console();
  129. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  130. } catch (err) {
  131. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  132. }
  133. }
  134. };