login.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, role } = data;
  18. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  19. const user = await this.model.findOne({ phone, role });
  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 === '4' || _user.role === '5') {
  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. } else if (_user.role === '8') {
  56. const url = 'http://127.0.0.1:9008/api/live/dock/getdock/' + _user.id;
  57. const vipuser = await this.ctx.curl(url, {
  58. method: 'post',
  59. headers: {
  60. 'content-type': 'application/json',
  61. },
  62. dataType: 'json',
  63. });
  64. user.remark = vipuser.data.id;
  65. }
  66. // 取出用户的类型,根据用户类型返回相应信息
  67. const state = uuid();
  68. const key = `free:auth:state:${state}`;
  69. const _menus = [];
  70. for (const elm of user.menus) {
  71. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  72. if (_menu) {
  73. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  74. }
  75. }
  76. user.menus = JSON.stringify(_menus);
  77. const token = await this.createJwt(user);
  78. await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
  79. let logFlag = false;
  80. // "4568"如果是456的需要去dock里面查有没有这个人(是否在apply),2,8必存
  81. if(_user.role === '4' || _user.role === '5' || _user.role === '6'){
  82. const applydata = { user_id: user.id};
  83. const url = 'http://127.0.0.1:9008/api/live/getapply';
  84. const applyflag = await this.ctx.curl(url, {
  85. method: 'post',
  86. headers: {
  87. 'content-type': 'application/json',
  88. },
  89. dataType: 'json',
  90. data: JSON.stringify(applydata),
  91. });
  92. // 如果有值true,说明登录者是申请用户,反之,false
  93. logFlag = applyflag.data.res
  94. }else if(_user.role === '2' || _user.role === '8'){
  95. logFlag = true;
  96. }
  97. if(logFlag){
  98. const operationlogdata = { login_id: user.id, login_name: user.name, login_role: user.role, type: '0', operation_edit: '登录' };
  99. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  100. const operationlog = await this.ctx.curl(url, {
  101. method: 'post',
  102. headers: {
  103. 'content-type': 'application/json',
  104. },
  105. dataType: 'json',
  106. data: JSON.stringify(operationlogdata),
  107. });
  108. }
  109. return { key };
  110. }
  111. // 创建登录Token
  112. async createJwtPwd(password) {
  113. const { secret, expiresIn, issuer } = this.config.jwt;
  114. const token = await jwt.sign(password, secret);
  115. return token;
  116. }
  117. // 创建登录Token
  118. async createJwt({ id, name, uid, phone, role, menus, remark, openid, deptid, deptname, pid, code }) {
  119. const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
  120. const subject = phone;
  121. const res = { uid: id, userid: uid, name, phone, role, menus, openid, remark, deptid, deptname, pid, code };
  122. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  123. return token;
  124. }
  125. // 取得redis内token信息
  126. async token({ key }) {
  127. assert(key, 'key不能为空');
  128. const token = await this.app.redis.get(key);
  129. if (!token) {
  130. throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
  131. }
  132. return { token };
  133. }
  134. // 删除操作
  135. async destroy({ key }) {
  136. const res = await this.app.redis.del(key);
  137. console.log(res);
  138. return res;
  139. }
  140. }
  141. module.exports = LoginService;