power.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const assert = require('assert');
  4. const crypto = require('crypto');
  5. const menu = require('../public/adminMenu');
  6. const svgCaptcha = require('svg-captcha');
  7. class UserService extends Service {
  8. async login({ acct, password, code }) {
  9. assert(acct, '帐号不存在');
  10. assert(password, '密码不存在');
  11. assert(code, '验证码不存在');
  12. const captcha = this.ctx.session.code;
  13. if (captcha !== code) {
  14. return { errmsg: '验证码错误', errcode: -2003 };
  15. }
  16. const { AdminUser: model } = this.ctx.model;
  17. const hash = crypto.createHmac('sha256', this.app.config.userSecret);
  18. const pwd = hash.update(password).digest('hex');
  19. try {
  20. const res = await model.find({ acct });
  21. if (res.length <= 0) {
  22. return { errmsg: '用户不存在', errcode: -2003 };
  23. }
  24. const userInfo = res[0];
  25. if (userInfo.state !== '0') {
  26. return { errmsg: '用户状态异常', errcode: -2003 };
  27. }
  28. if (userInfo.password !== pwd) {
  29. return { errmsg: '密码错误', errcode: -2003 };
  30. }
  31. const token = this.app.jwt.sign({ acct: userInfo.acct, userName: userInfo.userName, id: userInfo._id }, this.app.config.jwt.secret);
  32. return { errmsg: '', errcode: 0, token, userInfo: { acct: userInfo.acct, userName: userInfo.userName, id: userInfo._id } };
  33. } catch (error) {
  34. throw new Error('登录失败');
  35. }
  36. }
  37. async getUserMenu() {
  38. const { AdminUser: model } = this.ctx.model;
  39. const { Role: rolemodel } = this.ctx.model;
  40. try {
  41. const str = this.ctx.request.header.authorization;
  42. const token = str.substring(7);
  43. const decode = this.ctx.app.jwt.verify(token, this.app.config.jwt.secret);
  44. const menulist = [];
  45. const res = await model.find({ acct: decode.acct });
  46. if (res.length > 0) {
  47. const userRoleList = res[0].roleList;
  48. if (userRoleList && userRoleList.length > 0) {
  49. for (let e = 0; e < userRoleList.length; e++) {
  50. const role = await rolemodel.find({ code: userRoleList[e], state: '0' });
  51. if (role.length > 0) {
  52. const adminMenuList = role[0].adminMenuList;
  53. for (let i = 0; i < adminMenuList.length; i++) {
  54. const item = menu.filter(j => adminMenuList[i] === j.id)[0];
  55. if (!menulist.includes(item) && item) {
  56. menulist.push(item);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. return { errcode: 0, errmsg: '', data: menulist };
  64. } catch (error) {
  65. throw new Error('登录失败');
  66. }
  67. }
  68. async captcha() {
  69. const captcha = svgCaptcha.create({
  70. size: 4,
  71. fontSize: 50,
  72. ignoreChars: 'Ooli',
  73. width: 100,
  74. height: 40,
  75. noise: 3,
  76. color: true,
  77. background: '#cc9966',
  78. });
  79. this.ctx.session.code = captcha.text;
  80. this.ctx.response.type = 'image/svg+xml';
  81. return captcha.data;
  82. }
  83. }
  84. module.exports = UserService;