verify.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import BaseService from '../../base';
  2. import * as svgCaptcha from 'svg-captcha';
  3. import * as _ from 'lodash';
  4. /**
  5. * 通用功能Servce
  6. */
  7. export default class VerifyService extends BaseService {
  8. /**
  9. * 生成图片验证码
  10. */
  11. async getImgCaptcha(params) {
  12. const svg = svgCaptcha.create({
  13. size: 4,
  14. color: true,
  15. noise: 4,
  16. width: params.width ?? 100,
  17. height: params.height ?? 50,
  18. });
  19. const result = {
  20. img: `data:image/svg+xml;base64,${new Buffer(svg.data).toString('base64')}`,
  21. id: this.getHelper().generateUUID(),
  22. };
  23. // 10分钟过期时间
  24. await this.getAdminRedis().set(`admin:captcha:img:${result.id}`, svg.text, 'EX', 60 * 10);
  25. return result;
  26. }
  27. /**
  28. * 校验验证码
  29. */
  30. async checkImgCaptcha(id: string, code: string) {
  31. const result = await this.getAdminRedis().get(`admin:captcha:img:${id}`);
  32. if (_.isEmpty(result)) {
  33. return false;
  34. }
  35. if (code.toLowerCase() !== result!.toLowerCase()) {
  36. return false;
  37. }
  38. // 校验成功后移除验证码
  39. await this.getAdminRedis().del(`admin:captcha:img:${id}`);
  40. return true;
  41. }
  42. /**
  43. * 获取登录JWT
  44. * 返回null则账号密码有误,不存在该用户
  45. */
  46. async getLoginSign(username: string, password: string) {
  47. // const decodeUserName = this.getHelper().aesDecrypt(username, this.config.aesSecret.front);
  48. const decodeUserName = username;
  49. // const decodePassword = this.getHelper().aesDecrypt(password, this.config.aesSecret.front);
  50. const decodePassword = password;
  51. const user = await this.getRepo().admin.sys.User.findOne({ username: decodeUserName, status: 1 });
  52. if (_.isEmpty(user)) {
  53. return null;
  54. }
  55. if (this.getHelper().aesDecrypt(user!.password, this.config.aesSecret.admin) !== decodePassword) {
  56. return null;
  57. }
  58. const perms = await this.service.admin.sys.menu.getPerms(user!.id);
  59. const jwtSign = this.getHelper().jwtSign({
  60. uid: parseInt(user!.id.toString()),
  61. pv: 1,
  62. }, {
  63. expiresIn: '24h',
  64. });
  65. await this.getAdminRedis().set(`admin:passwordVersion:${user!.id}`, 1);
  66. await this.getAdminRedis().set(`admin:token:${user!.id}`, jwtSign);
  67. await this.getAdminRedis().set(`admin:perms:${user!.id}`, JSON.stringify(perms));
  68. // 保存登录日志
  69. await this.service.admin.sys.loginLog.save(user!.id);
  70. return jwtSign;
  71. }
  72. /**
  73. * 清除登录状态信息
  74. */
  75. async clearLoginStatus(uid: number) {
  76. await this.service.admin.sys.user.forbidden(uid);
  77. }
  78. /**
  79. * 获取权限菜单
  80. */
  81. async getPermMenu(uid: number) {
  82. const menus = await this.service.admin.sys.menu.getMenus(uid);
  83. const perms = await this.service.admin.sys.menu.getPerms(uid);
  84. return { menus, perms };
  85. }
  86. async getRedisPasswordVersionById(id: number) {
  87. return this.getAdminRedis().get(`admin:passwordVersion:${id}`);
  88. }
  89. async getRedisTokenById(id: number) {
  90. return this.getAdminRedis().get(`admin:token:${id}`);
  91. }
  92. async getRedisPermsById(id: number) {
  93. return this.getAdminRedis().get(`admin:perms:${id}`);
  94. }
  95. }