answerTea.js 1.0 KB

123456789101112131415161718192021222324252627
  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. // 咨询师
  7. class AnswerTeaService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'answer_tea');
  10. this.model = this.ctx.model.Patent.AnswerTea;
  11. }
  12. async login({ phone, password }) {
  13. const object = await this.model.findOne({ phone }, '+password');
  14. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  15. const { password: op } = object;
  16. const { secret } = op;
  17. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  18. const data = _.omit(JSON.parse(JSON.stringify(object)), ['meta', 'password', '__v']);
  19. const { secret: secrets } = this.config.jwt;
  20. const jwt = require('jsonwebtoken');
  21. const token = jwt.sign(data, secrets);
  22. return token;
  23. }
  24. }
  25. module.exports = AnswerTeaService;