login.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class LoginService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'login');
  11. this.uModel = this.ctx.model.User;
  12. this.stuModel = this.ctx.model.Student;
  13. this.tModel = this.ctx.model.Teacher;
  14. this.schModel = this.ctx.model.School;
  15. this.hModel = this.ctx.model.Headteacher;
  16. }
  17. async login(data) {
  18. const { mobile, passwd } = data;
  19. assert(mobile, 'mobile不能为空');
  20. // assert(/^\d{11}$/i.test(mobile), 'mobile无效');
  21. assert(passwd, 'passwd不能为空');
  22. const res = await this.uModel.findOne({ mobile }, '+passwd');
  23. if (!res) {
  24. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  25. }
  26. // 验证密码
  27. console.log(res.passwd.secret);
  28. console.log(passwd);
  29. if (res.passwd.secret !== passwd) {
  30. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  31. }
  32. return await this.createJwt(res);
  33. }
  34. // 创建登录Token
  35. async createJwt({ _id, name, mobile, openid, type, uid }) {
  36. const { secret, expiresIn = '1d', issuer = type } = this.config.jwt;
  37. const subject = mobile;
  38. let _userid = '';
  39. let res = {};
  40. if (type === '0') {
  41. _userid = _id.toString();
  42. res = { userid: _userid, name, type, id: _id };
  43. } else if (type === '1') {
  44. _userid = uid.toString();
  45. const result = await this.hModel.findById(_userid);
  46. res = { userid: _userid, name, type, id: _id };
  47. } else if (type === '2') {
  48. _userid = uid.toString();
  49. const result = await this.schModel.findById(_userid);
  50. res = { userid: _userid, code: result.code, name, type, id: _id };
  51. } else if (type === '3') {
  52. _userid = uid.toString();
  53. const result = await this.tModel.findById(_userid);
  54. res = { userid: _userid, schid: result.schid, schname: result.schname, name, type, id: _id };
  55. } else if (type === '4') {
  56. _userid = uid.toString();
  57. const result = await this.stuModel.findById(_userid);
  58. res = { userid: _userid, schid: result.schoolid, schname: result.school_name, name, type, id: _id };
  59. }
  60. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  61. return token;
  62. }
  63. async wxlogin(data) {
  64. const { openid } = data;
  65. assert(openid, 'openid不能为空');
  66. const res = await this.uModel.findOne({ openid });
  67. let newdata = {};
  68. if (!res) {
  69. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  70. } else {
  71. newdata = { id: res.id, name: res.name, openid: res.openid, type: res.type };
  72. }
  73. return await newdata;
  74. }
  75. }
  76. module.exports = LoginService;