login.js 6.6 KB

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