login.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. console.log(vipuser.data.res[0].id);
  65. const r = _.get(vipuser, 'data.res');
  66. if (r) {
  67. if (_.isArray(r)) {
  68. const rh = _.head(r);
  69. if (rh) user.remark = rh.id;
  70. } else if (_.isObject(r)) {
  71. user.remark = _.get(r, 'id');
  72. }
  73. }
  74. }
  75. // 取出用户的类型,根据用户类型返回相应信息
  76. const state = uuid();
  77. const key = `free:auth:state:${state}`;
  78. const _menus = [];
  79. for (const elm of user.menus) {
  80. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  81. if (_menu) {
  82. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  83. }
  84. }
  85. user.menus = JSON.stringify(_menus);
  86. const token = await this.createJwt(user);
  87. await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
  88. let logFlag = false;
  89. let dockList = [];
  90. // "4568"如果是456的需要去dock里面查有没有这个人(是否在apply),2,8必存
  91. if (
  92. _user.role === '4' ||
  93. _user.role === '5' ||
  94. _user.role === '6' ||
  95. _user.role === '8'
  96. ) {
  97. const applydata = { user_id: user.id };
  98. const url = 'http://127.0.0.1:9008/api/live/getapply';
  99. const applyflag = await this.ctx.curl(url, {
  100. method: 'post',
  101. headers: {
  102. 'content-type': 'application/json',
  103. },
  104. dataType: 'json',
  105. data: JSON.stringify(applydata),
  106. });
  107. // 如果有值true,说明登录者是申请用户,反之,false
  108. if (applyflag.data.res.length > 0) {
  109. logFlag = true;
  110. dockList = applyflag.data.res;
  111. }
  112. } else if (_user.role === '2') {
  113. logFlag = true;
  114. }
  115. if (logFlag) {
  116. if (dockList.length > 0) {
  117. for (const dock of dockList) {
  118. const operationlogdata = {
  119. dockid: dock.id,
  120. login_name: user.name,
  121. login_role: user.role,
  122. type: '0',
  123. operation_edit: '登录',
  124. };
  125. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  126. const operationlog = await this.ctx.curl(url, {
  127. method: 'post',
  128. headers: {
  129. 'content-type': 'application/json',
  130. },
  131. dataType: 'json',
  132. data: JSON.stringify(operationlogdata),
  133. });
  134. }
  135. } else {
  136. const operationlogdata = {
  137. login_id: user.id,
  138. login_name: user.name,
  139. login_role: user.role,
  140. type: '0',
  141. operation_edit: '登录',
  142. };
  143. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  144. const operationlog = await this.ctx.curl(url, {
  145. method: 'post',
  146. headers: {
  147. 'content-type': 'application/json',
  148. },
  149. dataType: 'json',
  150. data: JSON.stringify(operationlogdata),
  151. });
  152. }
  153. }
  154. return { key };
  155. }
  156. // 创建登录Token
  157. async createJwtPwd(password) {
  158. const { secret, expiresIn, issuer } = this.config.jwt;
  159. const token = await jwt.sign(password, secret);
  160. return token;
  161. }
  162. // 创建登录Token
  163. async createJwt({
  164. id,
  165. name,
  166. uid,
  167. phone,
  168. role,
  169. menus,
  170. remark,
  171. openid,
  172. deptid,
  173. deptname,
  174. pid,
  175. code,
  176. }) {
  177. const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
  178. const subject = phone;
  179. const res = {
  180. uid: id,
  181. userid: uid,
  182. name,
  183. phone,
  184. role,
  185. menus,
  186. openid,
  187. remark,
  188. deptid,
  189. deptname,
  190. pid,
  191. code,
  192. };
  193. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  194. return token;
  195. }
  196. // 取得redis内token信息
  197. async token({ key }) {
  198. assert(key, 'key不能为空');
  199. const token = await this.app.redis.get(key);
  200. if (!token) {
  201. throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
  202. }
  203. return { token };
  204. }
  205. // 删除操作
  206. async destroy({ key }) {
  207. const res = await this.app.redis.del(key);
  208. console.log(res);
  209. return res;
  210. }
  211. }
  212. module.exports = LoginService;