achieve_expert.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. const jwt = require('jsonwebtoken');
  8. // 临时专家
  9. class Achieve_expertService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'achieve_expert');
  12. this.model = this.ctx.model.AchieveExpert;
  13. }
  14. async create(data) {
  15. const { password } = data;
  16. data.password = { secret: password };
  17. const res = await this.model.create(data);
  18. return res;
  19. }
  20. /**
  21. * 专家临时账号修改(包括评分与意见)
  22. * @param {Object} id 专家临时账号数据id
  23. * @param {Object} data 要修改的内容
  24. */
  25. async update({ id }, data) {
  26. const { password } = data;
  27. const older = await this.model.findById(id);
  28. if (!older) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家信息');
  29. // 此处是检验专家是否可以进行修改
  30. const { status } = older;
  31. if (status === '1') throw new BusinessError(ErrorCode.SERVICE_FAULT, '您的工作已完成,若有问题请联系平台管理员!');
  32. if (password) { data.password = { secret: password }; }
  33. await this.model.findByIdAndUpdate(id, data);
  34. return await this.model.findById(id);
  35. }
  36. /**
  37. * 冻结账号
  38. * @param {Object} id 数据id
  39. */
  40. async delete({ id }) {
  41. await this.model.updateOne({ _id: id }, { status: '1' });
  42. }
  43. /**
  44. * 解冻账号
  45. * @param {Object} id 数据id
  46. */
  47. async restore({ id }) {
  48. await this.model.updateOne({ _id: id }, { status: '0' });
  49. }
  50. /**
  51. * 临时专家账号登录
  52. * @param {Object} params 手机号,密码
  53. */
  54. async login({ phone, password }) {
  55. const expert = await this.model.findOne({ phone, status: '0' }, '+password');
  56. if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到您的临时账号,如有疑问,请联系管理员');
  57. const { password: op } = expert;
  58. // if (status !== '0') throw new BusinessError(ErrorCode.ACCESS_DENIED, '您的临时账号已被注销,如有疑问,请联系管理员');
  59. const { secret } = op;
  60. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  61. const data = _.omit(JSON.parse(JSON.stringify(expert)), [ 'expert_name', 'meta', 'password', '__v', 'verify' ]);
  62. const { secret: secrets } = this.config.jwt;
  63. data.name = _.get(expert, 'expert_name');
  64. data.role = '3';
  65. const token = jwt.sign(data, secrets);
  66. return token;
  67. }
  68. async getExperts(ids) {
  69. const res = await this.model.find({ _id: ids });
  70. return res;
  71. }
  72. /**
  73. * 获取可以生成临时账号的专家列表
  74. */
  75. async getExpertList() {
  76. // 1,去live项目中把所有专家整来
  77. // 2,到achieve_expert表里,把可以使用的临时账号(status=0)的专家id都拿来
  78. // 3.把1中2的结果去掉,返回
  79. let expertList = [];
  80. const eres = await this.ctx.service.util.httpUtil.cpost('/spm', 'live', { service: 'users.expert', method: 'query' }, { method: 'useService' });
  81. if (eres) expertList = eres.data;
  82. const have_accounts = await this.model.find({ status: 0 }, 'expert_user_id');
  83. expertList = expertList.map(i => {
  84. const res = have_accounts.find(ae => ObjectId(ae.expert_user_id).equals(i.user_id));
  85. if (res)i.cant_use = true;
  86. return i;
  87. });
  88. return expertList;
  89. }
  90. }
  91. module.exports = Achieve_expertService;