login.js 6.7 KB

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