login.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.uModel = this.ctx.model.User;
  13. this.stuModel = this.ctx.model.Student;
  14. this.tModel = this.ctx.model.Teacher;
  15. this.schModel = this.ctx.model.School;
  16. this.hModel = this.ctx.model.Headteacher;
  17. }
  18. async login(data) {
  19. const { mobile, passwd } = data;
  20. assert(mobile, 'mobile不能为空');
  21. // assert(/^\d{11}$/i.test(mobile), 'mobile无效');
  22. assert(passwd, 'passwd不能为空');
  23. const res = await this.uModel.findOne({ mobile }, '+passwd');
  24. if (!res) {
  25. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  26. }
  27. // 验证密码
  28. console.log('sjk-->' + res.passwd.secret);
  29. console.log('sr-->' + passwd);
  30. if (res.passwd.secret !== passwd) {
  31. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  32. }
  33. return await this.createJwt(res);
  34. }
  35. // 创建登录Token
  36. async createJwt({ _id, name, mobile, openid, type, uid, status }) {
  37. const { secret, expiresIn = '1d', issuer = type } = this.config.jwt;
  38. const subject = mobile;
  39. let _userid = '';
  40. let res = {};
  41. // 身份,0、中心管理元1、班主任用户2、学校用户3、教师4、学生
  42. if (type === '0') {
  43. _userid = _id.toString();
  44. res = { userid: _userid, name, type, id: _id, status };
  45. } else if (type === '1') {
  46. _userid = uid.toString();
  47. const result = await this.hModel.findById(_userid);
  48. res = { userid: _userid, name, type, id: _id, status };
  49. } else if (type === '2') {
  50. _userid = uid.toString();
  51. const result = await this.schModel.findById(_userid);
  52. res = { userid: _userid, code: result.code, name, type, id: _id, status };
  53. } else if (type === '3') {
  54. _userid = uid.toString();
  55. const result = await this.tModel.findById(_userid);
  56. res = { userid: _userid, schid: result.schid, schname: result.schname, name, subid: result.subid, type, id: _id, status };
  57. } else if (type === '4') {
  58. _userid = uid;
  59. console.log('进入查询--' + _userid);
  60. const result = await this.stuModel.findById(_userid);
  61. console.log(result);
  62. res = { userid: _userid, schid: result.schid, schname: result.school_name, termid: result.termid, batchid: result.batchid, classid: result.classid, bedroomid: result.bedroomid, bedroom: result.bedroom, job: result.job, name, type, id: _id, status, planid: result.planid };
  63. }
  64. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  65. return token;
  66. }
  67. // 微信校验mq接口
  68. async wxcheck(data) {
  69. const { qrcode, openid } = data;
  70. assert(qrcode, 'qrcode不能为空');
  71. assert(openid, 'openid不能为空');
  72. // TODO: 发布扫码成功消息
  73. console.log('qrcode-->' + qrcode);
  74. const { mq } = this.ctx;
  75. const ex = 'qrcode.login';
  76. const parm = {
  77. durable: true,
  78. headers: {
  79. openid,
  80. } };
  81. if (mq) {
  82. await mq.topic(ex, qrcode, 'scaned', parm);
  83. }
  84. }
  85. // 微信登录接口
  86. async wxlogin(data) {
  87. const { qrcode, openid } = data;
  88. assert(qrcode, 'qrcode不能为空');
  89. assert(openid, 'openid不能为空');
  90. const res = await this.uModel.findOne({ openid });
  91. if (!res) {
  92. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  93. }
  94. const result = await this.createJwt(res);
  95. return result;
  96. }
  97. async userbind(data) {
  98. const { qrcode, openid } = data;
  99. assert(qrcode, 'qrcode不能为空');
  100. assert(openid, 'openid不能为空');
  101. const key = `naf:qrcode:login:${qrcode}`;
  102. const status = await this.app.redis.get(key);
  103. if (!status) {
  104. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  105. }
  106. if (status !== 'pending') {
  107. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
  108. }
  109. const res = await this.uModel.findOne({ openid });
  110. if (!res) {
  111. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  112. }
  113. const result = await this.createJwt(res);
  114. // TODO: 修改二维码状态,登录凭证保存到redis
  115. await this.app.redis.set(key, `scaned:${result}`, 'EX', 600);
  116. // TODO: 发布扫码成功消息
  117. const { mq } = this.ctx;
  118. const ex = 'qrcode.login';
  119. if (mq) {
  120. await mq.topic(ex, qrcode, 'scaned', { durable: true });
  121. }
  122. }
  123. /**
  124. * 创建二维码
  125. * 随机生成二维码,并保存在Redis中,状态初始为pending
  126. * 状态描述:
  127. * pending - 等待扫码
  128. * consumed - 使用二维码登录完成
  129. * ${jwt.token} - Jwt登录凭证
  130. */
  131. async createQrcode() {
  132. const qrcode = uuid();
  133. const key = `naf:qrcode:login:${qrcode}`;
  134. await this.app.redis.set(key, 'pending', 'EX', 600);
  135. return qrcode;
  136. }
  137. // 使用二维码换取登录凭证
  138. async qrcodeLogin(qrcode) {
  139. assert(qrcode, 'qrcode不能为空');
  140. const key = `naf:qrcode:login:${qrcode}`;
  141. const val = await this.app.redis.get(key);
  142. if (!val) {
  143. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  144. }
  145. const [ status, token ] = val.split(':', 2);
  146. if (status !== 'scaned' || !token) {
  147. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码状态无效');
  148. }
  149. // TODO: 修改二维码状态
  150. await this.app.redis.set(key, 'consumed', 'EX', 600);
  151. return { token };
  152. }
  153. // 检查二维码状态
  154. async checkQrcode(qrcode) {
  155. assert(qrcode, 'qrcode不能为空');
  156. const key = `naf:qrcode:login:${qrcode}`;
  157. const val = await this.app.redis.get(key);
  158. if (!val) {
  159. throw new BusinessError(ErrorCode.SERVICE_FAULT, '二维码已过期');
  160. }
  161. const [ status ] = val.split(':', 2);
  162. return { status };
  163. }
  164. }
  165. module.exports = LoginService;