achieve_expert.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/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 Achieve_expertService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'achieve_expert');
  11. this.model = this.ctx.model.AchieveExpert;
  12. }
  13. async create(data) {
  14. const { password } = data;
  15. data.password = { secret: password };
  16. const res = await this.model.create(data);
  17. return res;
  18. }
  19. /**
  20. * 专家临时账号修改(包括评分与意见)
  21. * @param {Object} id 专家临时账号数据id
  22. * @param {Object} data 要修改的内容
  23. */
  24. async update({ id }, data) {
  25. const { password } = data;
  26. const older = await this.model.findById(id);
  27. if (!older) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家信息');
  28. // 此处是检验专家是否可以进行修改
  29. const { status } = older;
  30. if (status === '1') throw new BusinessError(ErrorCode.SERVICE_FAULT, '您的工作已完成,若有问题请联系平台管理员!');
  31. if (password) { data.password = { secret: password }; }
  32. await this.model.findByIdAndUpdate(id, data);
  33. return await this.model.findById(id);
  34. }
  35. /**
  36. * 临时专家账号登录
  37. * @param {Object} params 手机号,密码
  38. */
  39. async login({ phone, password }) {
  40. const expert = await this.model.findOne({ phone, status: '0' }, '+password');
  41. if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '您不存在未完成的工作,无需登录');
  42. const { password: op } = expert;
  43. const { secret } = op;
  44. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  45. const data = _.omit(JSON.parse(JSON.stringify(expert)), [ 'expert_name', 'meta', 'password', '__v', 'verify' ]);
  46. const { secret: secrets } = this.config.jwt;
  47. data.name = _.get(expert, 'expert_name');
  48. data.role = '3';
  49. const token = jwt.sign(data, secrets);
  50. return token;
  51. }
  52. }
  53. module.exports = Achieve_expertService;