trainUser.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const jwt = require('jsonwebtoken');
  7. // 培训问诊用户
  8. class TrainUserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'trainuser');
  11. this.model = this.ctx.model.Consultation.TrainUser;
  12. }
  13. /**
  14. * 登陆
  15. * @param {Object} params 登陆信息
  16. * @property phone 电话
  17. * @property password 密码
  18. * @property train_id 培训问诊id
  19. */
  20. async login({ phone, password, train_id }) {
  21. const object = await this.model.findOne({ train_id, phone }, '+password');
  22. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  23. const { password: op, status } = object;
  24. const { secret } = op;
  25. if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
  26. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  27. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  28. const { secret: secrets } = this.config.jwt;
  29. const token = jwt.sign(data, secrets);
  30. return token;
  31. }
  32. }
  33. module.exports = TrainUserService;