achieve_expert.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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} id 数据id
  38. */
  39. async delete({ id }) {
  40. await this.model.updateOne({ _id: id }, { status: '1' });
  41. }
  42. /**
  43. * 解冻账号
  44. * @param {Object} id 数据id
  45. */
  46. async restore({ id }) {
  47. await this.model.updateOne({ _id: id }, { status: '0' });
  48. }
  49. /**
  50. * 临时专家账号登录
  51. * @param {Object} params 手机号,密码
  52. */
  53. async login({ phone, password }) {
  54. const expert = await this.model.findOne({ phone }, '+password');
  55. if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到您的临时账号,如有疑问,请联系管理员');
  56. const { password: op, status } = expert;
  57. if (status !== '0') throw new BusinessError(ErrorCode.ACCESS_DENIED, '您的临时账号已被注销,如有疑问,请联系管理员');
  58. const { secret } = op;
  59. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  60. const data = _.omit(JSON.parse(JSON.stringify(expert)), [ 'expert_name', 'meta', 'password', '__v', 'verify' ]);
  61. const { secret: secrets } = this.config.jwt;
  62. data.name = _.get(expert, 'expert_name');
  63. data.role = '3';
  64. const token = jwt.sign(data, secrets);
  65. return token;
  66. }
  67. }
  68. module.exports = Achieve_expertService;