login.js 6.8 KB

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