login.js 3.5 KB

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