login.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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, institution_code } = data;
  18. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  19. let user = await this.model.findOne({ phone, role });
  20. // 增设使用code模式登陆的判断变量
  21. let is_code = false;
  22. // 如果用户不存在抛出异常
  23. if (!user) {
  24. // 添加code作为登录的方式
  25. user = await this.model.findOne({ code: phone, role, institution_code });
  26. if (!user) { throw new BusinessError(ErrorCode.USER_NOT_EXIST); } else is_code = true;
  27. }
  28. const pdata = {};
  29. if (is_code) pdata.code = phone;
  30. else pdata.phone = phone;
  31. const _user = await this.model.findOne(pdata, '+passwd');
  32. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  33. const pas = await this.createJwtPwd(passwd);
  34. // 如果两个密码不一致抛出异常
  35. if (pas !== _user.passwd.secret) {
  36. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  37. }
  38. if (_user.role === '4' || _user.role === '5') {
  39. const url = 'http://127.0.0.1:9004/api/market/user/' + _user.uid;
  40. const marketuser = await this.ctx.curl(url, {
  41. method: 'get',
  42. headers: {
  43. 'content-type': 'application/json',
  44. },
  45. dataType: 'json',
  46. });
  47. if (marketuser.data.data.status !== '1') {
  48. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  49. }
  50. } else if (_user.role === '6') {
  51. const url = 'http://127.0.0.1:9004/api/market/expertsuser/' + _user.uid;
  52. const expertsuser = await this.ctx.curl(url, {
  53. method: 'get',
  54. headers: {
  55. 'content-type': 'application/json',
  56. },
  57. dataType: 'json',
  58. });
  59. if (expertsuser.data.data.status !== '1') {
  60. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  61. }
  62. } else if (_user.role === '8') {
  63. const url = 'http://127.0.0.1:9008/api/live/dock/getdock/' + _user.id;
  64. const vipuser = await this.ctx.curl(url, {
  65. method: 'post',
  66. headers: {
  67. 'content-type': 'application/json',
  68. },
  69. dataType: 'json',
  70. });
  71. const r = _.get(vipuser, 'data.res');
  72. if (r) {
  73. if (_.isArray(r)) {
  74. const rh = _.head(r);
  75. if (rh) user.remark = rh.id;
  76. } else if (_.isObject(r)) {
  77. user.remark = _.get(r, 'id');
  78. }
  79. }
  80. }
  81. // 取出用户的类型,根据用户类型返回相应信息
  82. const state = uuid();
  83. const key = `free:auth:state:${state}`;
  84. const _menus = [];
  85. for (const elm of user.menus) {
  86. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  87. if (_menu) {
  88. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  89. }
  90. }
  91. user.menus = JSON.stringify(_menus);
  92. const token = await this.createJwt(user);
  93. await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
  94. let logFlag = false;
  95. let dockList = [];
  96. // "4568"如果是456的需要去dock里面查有没有这个人(是否在apply),2,8必存
  97. if (
  98. _user.role === '4' ||
  99. _user.role === '5' ||
  100. _user.role === '6' ||
  101. _user.role === '8'
  102. ) {
  103. const applydata = { user_id: user.id };
  104. const url = 'http://127.0.0.1:9008/api/live/getapply';
  105. const applyflag = await this.ctx.curl(url, {
  106. method: 'post',
  107. headers: {
  108. 'content-type': 'application/json',
  109. },
  110. dataType: 'json',
  111. data: JSON.stringify(applydata),
  112. });
  113. // 如果有值true,说明登录者是申请用户,反之,false
  114. if (applyflag.data.res.length > 0) {
  115. logFlag = true;
  116. dockList = applyflag.data.res;
  117. }
  118. } else if (_user.role === '2') {
  119. logFlag = true;
  120. }
  121. if (logFlag) {
  122. if (dockList.length > 0) {
  123. for (const dock of dockList) {
  124. const operationlogdata = {
  125. dockid: dock.id,
  126. login_name: user.name,
  127. login_role: user.role,
  128. type: '0',
  129. operation_edit: '登录',
  130. };
  131. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  132. const operationlog = await this.ctx.curl(url, {
  133. method: 'post',
  134. headers: {
  135. 'content-type': 'application/json',
  136. },
  137. dataType: 'json',
  138. data: JSON.stringify(operationlogdata),
  139. });
  140. }
  141. } else {
  142. const operationlogdata = {
  143. login_id: user.id,
  144. login_name: user.name,
  145. login_role: user.role,
  146. type: '0',
  147. operation_edit: '登录',
  148. };
  149. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  150. const operationlog = await this.ctx.curl(url, {
  151. method: 'post',
  152. headers: {
  153. 'content-type': 'application/json',
  154. },
  155. dataType: 'json',
  156. data: JSON.stringify(operationlogdata),
  157. });
  158. }
  159. }
  160. return { key };
  161. }
  162. // 创建登录Token
  163. async createJwtPwd(password) {
  164. const { secret, expiresIn, issuer } = this.config.jwt;
  165. const token = await jwt.sign(password, secret);
  166. return token;
  167. }
  168. // 创建登录Token
  169. async createJwt({
  170. id,
  171. name,
  172. uid,
  173. phone,
  174. role,
  175. menus,
  176. remark,
  177. openid,
  178. deptid,
  179. deptname,
  180. pid,
  181. code,
  182. }) {
  183. const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
  184. const subject = phone;
  185. const res = {
  186. uid: id,
  187. userid: uid,
  188. name,
  189. phone,
  190. role,
  191. menus,
  192. openid,
  193. remark,
  194. deptid,
  195. deptname,
  196. pid,
  197. code,
  198. };
  199. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  200. return token;
  201. }
  202. // 取得redis内token信息
  203. async token({ key }) {
  204. assert(key, 'key不能为空');
  205. const token = await this.app.redis.get(key);
  206. if (!token) {
  207. throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
  208. }
  209. return { token };
  210. }
  211. // 删除操作
  212. async destroy({ key }) {
  213. const res = await this.app.redis.del(key);
  214. console.log(res);
  215. return res;
  216. }
  217. }
  218. module.exports = LoginService;