login.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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, isdel: '0' });
  20. console.log(`user1=>${user}`);
  21. // 增设使用code模式登陆的判断变量
  22. let is_code = false;
  23. if (!user) {
  24. // 添加code作为登录的方式
  25. user = await this.model.findOne({ code: phone, role, isdel: '0' });
  26. console.log(`user2=>${user}`);
  27. // 如果用户不存在抛出异常
  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 (_user.role === '4' || _user.role === '5' || _user.role === '6' || _user.role === '8') {
  100. const applydata = { user_id: user.id };
  101. const url = 'http://127.0.0.1:9008/api/live/getapply';
  102. const applyflag = await this.ctx.curl(url, {
  103. method: 'post',
  104. headers: {
  105. 'content-type': 'application/json',
  106. },
  107. dataType: 'json',
  108. data: JSON.stringify(applydata),
  109. });
  110. // 如果有值true,说明登录者是申请用户,反之,false
  111. if (applyflag.data.res.length > 0) {
  112. logFlag = true;
  113. dockList = applyflag.data.res;
  114. } else {
  115. logFlag = true;
  116. }
  117. } else if (_user.role === '2') {
  118. logFlag = true;
  119. }
  120. if (logFlag) {
  121. if (dockList.length > 0) {
  122. for (const dock of dockList) {
  123. const operationlogdata = {
  124. dockid: dock.id,
  125. login_name: user.name,
  126. login_role: user.role,
  127. type: '0',
  128. operation_edit: '登录',
  129. };
  130. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  131. const operationlog = await this.ctx.curl(url, {
  132. method: 'post',
  133. headers: {
  134. 'content-type': 'application/json',
  135. },
  136. dataType: 'json',
  137. data: JSON.stringify(operationlogdata),
  138. });
  139. }
  140. } else {
  141. const operationlogdata = {
  142. login_id: user.id,
  143. login_name: user.name,
  144. login_role: user.role,
  145. type: '0',
  146. operation_edit: '登录',
  147. };
  148. const url = 'http://127.0.0.1:9004/api/market/operationlog/';
  149. const operationlog = await this.ctx.curl(url, {
  150. method: 'post',
  151. headers: {
  152. 'content-type': 'application/json',
  153. },
  154. dataType: 'json',
  155. data: JSON.stringify(operationlogdata),
  156. });
  157. }
  158. }
  159. return { key };
  160. }
  161. // 创建登录Token
  162. async createJwtPwd(password) {
  163. const { secret, expiresIn, issuer } = this.config.jwt;
  164. const token = await jwt.sign(password, secret);
  165. return token;
  166. }
  167. // 创建登录Token
  168. async createJwt({
  169. id,
  170. name,
  171. uid,
  172. phone,
  173. role,
  174. menus,
  175. remark,
  176. openid,
  177. deptid,
  178. deptname,
  179. pid,
  180. code,
  181. }) {
  182. const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
  183. const subject = phone;
  184. const res = {
  185. uid: id,
  186. userid: uid,
  187. name,
  188. phone,
  189. role,
  190. menus,
  191. openid,
  192. remark,
  193. deptid,
  194. deptname,
  195. pid,
  196. code,
  197. };
  198. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  199. return token;
  200. }
  201. // 取得redis内token信息
  202. async token({ key }) {
  203. assert(key, 'key不能为空');
  204. const token = await this.app.redis.get(key);
  205. if (!token) {
  206. throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
  207. }
  208. return { token };
  209. }
  210. // 删除操作
  211. async destroy({ key }) {
  212. const res = await this.app.redis.del(key);
  213. console.log(res);
  214. return res;
  215. }
  216. }
  217. module.exports = LoginService;