login.js 5.3 KB

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