login.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const jwt = require('jsonwebtoken');
  8. const uuid = require('uuid');
  9. class LoginService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'login');
  12. this.model = this.ctx.model.User;
  13. this.rmodel = this.ctx.model.Role;
  14. this.umodel = this.ctx.model.Roomuser;
  15. }
  16. // 用户登录
  17. async login(data) {
  18. const { phone, passwd } = data;
  19. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  20. const user = await this.model.findOne({ phone });
  21. // 如果用户不存在抛出异常
  22. if (!user) {
  23. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  24. }
  25. const _user = await this.model.findOne({ phone }, '+passwd');
  26. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  27. const pas = await this.createJwtPwd(passwd);
  28. // 如果两个密码不一致抛出异常
  29. if (pas !== _user.passwd.secret) {
  30. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  31. }
  32. if (_user.role === '4') {
  33. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  34. }
  35. // 取出用户的类型,根据用户类型返回相应信息
  36. const state = uuid();
  37. const key = `free:auth:state:${state}`;
  38. const _menus = [];
  39. for (const elm of user.menus) {
  40. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  41. if (_menu) {
  42. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  43. }
  44. }
  45. user.menus = JSON.stringify(_menus);
  46. const token = await this.createJwt(user);
  47. await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
  48. return { key };
  49. }
  50. // 创建登录Token
  51. async createJwtPwd(password) {
  52. const { secret, expiresIn, issuer } = this.config.jwt;
  53. const token = await jwt.sign(password, secret);
  54. return token;
  55. }
  56. // 创建登录Token
  57. async createJwt({ id, name, uid, phone, role, menus, remark, openid, deptname }) {
  58. const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
  59. const subject = phone;
  60. const res = { uid: id, userid: uid, name, phone, role, menus, openid, remark, deptname };
  61. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  62. return token;
  63. }
  64. // 取得redis内token信息
  65. async token({ key }) {
  66. assert(key, 'key不能为空');
  67. const token = await this.app.redis.get(key);
  68. if (!token) {
  69. throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
  70. }
  71. return { token };
  72. }
  73. // 删除操作
  74. async destroy({ key }) {
  75. const res = await this.app.redis.del(key);
  76. console.log(res);
  77. return res;
  78. }
  79. }
  80. module.exports = LoginService;