login.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. let user = await this.model.findOne({ phone, role });
  20. user = JSON.parse(JSON.stringify(user));
  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. // console.log('-------------------------------_user.role');
  33. // console.log(_user.role);
  34. if (_user.role === '4' || _user.role === '5') {
  35. const url = 'http://127.0.0.1:9004/api/market/user/' + _user.uid;
  36. const marketuser = await this.ctx.curl(url, {
  37. method: 'get',
  38. headers: {
  39. 'content-type': 'application/json',
  40. },
  41. dataType: 'json',
  42. });
  43. if (marketuser.data.data.status !== '1') {
  44. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  45. }
  46. } else if (_user.role === '6') {
  47. const url = 'http://127.0.0.1:9004/api/market/expertsuser/' + _user.uid;
  48. const expertsuser = await this.ctx.curl(url, {
  49. method: 'get',
  50. headers: {
  51. 'content-type': 'application/json',
  52. },
  53. dataType: 'json',
  54. });
  55. if (expertsuser.data.data.status !== '1') {
  56. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  57. }
  58. } else if (_user.role === '8') {
  59. const url = 'http://127.0.0.1:9008/api/live/dock/getdock/' + _user.id;
  60. const vipuser = await this.ctx.curl(url, {
  61. method: 'post',
  62. headers: {
  63. 'content-type': 'application/json',
  64. },
  65. dataType: 'json',
  66. });
  67. // console.log('------------------------------------>>>>>>>>>>>>>>vipuser<<<<<<<<<<<*-----------------');
  68. if (vipuser.status === 200) {
  69. if (vipuser.data.errcode === 0) {
  70. const vd = vipuser.data.res;
  71. if (vd.length > 0) {
  72. const f = _.head(vd);
  73. const fid = _.get(f, 'id');
  74. user = { ...user, remark: fid };
  75. }
  76. }
  77. }
  78. }
  79. // 取出用户的类型,根据用户类型返回相应信息
  80. const state = uuid();
  81. const key = `free:auth:state:${state}`;
  82. const _menus = [];
  83. for (const elm of user.menus) {
  84. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  85. if (_menu) {
  86. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  87. }
  88. }
  89. user.menus = JSON.stringify(_menus);
  90. const token = await this.createJwt(user);
  91. await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
  92. const operationlogdata = { dockid: user.remark, login_id: user.id, login_name: user.name, login_role: user.role, type: '0', operation_edit: '登录' };
  93. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  94. const operationlog = await this.ctx.curl(url, {
  95. method: 'post',
  96. headers: {
  97. 'content-type': 'application/json',
  98. },
  99. dataType: 'json',
  100. data: JSON.stringify(operationlogdata),
  101. });
  102. if (operationlog.data.errcode === 0) {
  103. console.log('1234');
  104. }
  105. return { key };
  106. }
  107. // 创建登录Token
  108. async createJwtPwd(password) {
  109. const { secret, expiresIn, issuer } = this.config.jwt;
  110. const token = await jwt.sign(password, secret);
  111. return token;
  112. }
  113. // 创建登录Token
  114. async createJwt({ id, name, uid, phone, role, menus, remark, openid, deptid, deptname, pid, code }) {
  115. try {
  116. const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
  117. const subject = phone;
  118. const res = { uid: id, userid: uid, name, phone, role, menus, openid, remark, deptid, deptname, pid, code };
  119. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  120. return token;
  121. } catch (error) {
  122. console.error('token error');
  123. }
  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;