login.js 6.7 KB

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