reviewExpert.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 ReviewExpertService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'reviewexpert');
  11. this.model = this.ctx.model.ReviewExpert;
  12. }
  13. /**
  14. * 登陆
  15. * @param {Object} params 登陆信息
  16. * @property phone 手机号
  17. * @property password 密码
  18. */
  19. async login({ phone, password }) {
  20. const object = await this.model.findOne({ phone }, '+password');
  21. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  22. const { password: op, status } = object;
  23. const { secret } = op;
  24. if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
  25. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  26. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  27. const { secret: secrets } = this.config.jwt;
  28. const token = jwt.sign(data, secrets);
  29. return token;
  30. }
  31. /**
  32. * 修改密码
  33. * @param {Object} {id,password} 用户id和密码
  34. */
  35. async password({ id, password }) {
  36. const object = await this.model.findById(id);
  37. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  38. object.password = { secret: password };
  39. await object.save();
  40. }
  41. }
  42. module.exports = ReviewExpertService;