login.js 6.8 KB

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