login.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. this.roommodel = this.ctx.model.Room;
  16. }
  17. // 用户登录
  18. async login(data) {
  19. const { phone, passwd } = data;
  20. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  21. const user = await this.model.findOne({ phone });
  22. // 如果用户不存在抛出异常
  23. if (!user) {
  24. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  25. }
  26. const _user = await this.model.findOne({ phone }, '+passwd');
  27. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  28. const pas = await this.createJwtPwd(passwd);
  29. // 如果两个密码不一致抛出异常
  30. if (pas !== _user.passwd.secret) {
  31. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  32. }
  33. if (_user.role === '4') {
  34. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  35. }
  36. // 取出用户的类型,根据用户类型返回相应信息
  37. const state = uuid();
  38. const key = `free:auth:state:${state}`;
  39. const _menus = [];
  40. for (const elm of user.menus) {
  41. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  42. if (_menu) {
  43. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  44. }
  45. }
  46. user.menus = JSON.stringify(_menus);
  47. const token = await this.createJwt(user);
  48. await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
  49. return { key };
  50. }
  51. // 创建登录Token
  52. async createJwtPwd(password) {
  53. const { secret, expiresIn, issuer } = this.config.jwt;
  54. const token = await jwt.sign(password, secret);
  55. return token;
  56. }
  57. // 创建登录Token
  58. async createJwt({ id, name, uid, phone, role, menus, remark, openid, deptname }) {
  59. const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
  60. const subject = phone;
  61. const res = { id, uid, name, phone, role, menus, openid, remark, deptname };
  62. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  63. return token;
  64. }
  65. // 取得redis内token信息
  66. async token({ key }) {
  67. assert(key, 'key不能为空');
  68. const token = await this.app.redis.get(key);
  69. if (!token) {
  70. throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
  71. }
  72. return { token };
  73. }
  74. // 删除操作
  75. async destroy({ key }) {
  76. const res = await this.app.redis.del(key);
  77. console.log(res);
  78. return res;
  79. }
  80. // 程序用户登录
  81. async trtclogin(data) {
  82. const { roomId, phone, passwd } = data;
  83. const room = await this.roommodel.findOne({ name: roomId });
  84. if (!room) {
  85. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  86. }
  87. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  88. const user = await this.model.findOne({ phone });
  89. // 如果用户不存在抛出异常
  90. if (!user) {
  91. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  92. }
  93. const _user = await this.model.findOne({ phone }, '+passwd');
  94. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  95. const pas = await this.createJwtPwd(passwd);
  96. // 如果两个密码不一致抛出异常
  97. if (pas !== _user.passwd.secret) {
  98. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  99. }
  100. if (_user.role === '4') {
  101. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  102. }
  103. // 取出用户的类型,根据用户类型返回相应信息
  104. const user_ = _.find(room.zjr, function(o) { return o === _user.uid; });
  105. if (!user_) {
  106. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  107. }
  108. return { uid: _user.uid };
  109. }
  110. }
  111. module.exports = LoginService;