expert.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 jwt = require('jsonwebtoken');
  6. // 专家
  7. class ExpertService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'expert');
  10. this.personal = this.ctx.model.User.Personal;
  11. this.model = this.ctx.model.User.Expert;
  12. }
  13. async query({ skip = 0, limit, ...condition } = {}) {
  14. condition = this.ctx.service.util.util.dealQuery(condition);
  15. const { code, status, company, name, phone } = condition;
  16. const query = {};
  17. if (code) query.code = code;
  18. if (status) query['expert.status'] = status;
  19. if (name) query.name = name;
  20. if (phone) query.phone = phone;
  21. if (company) {
  22. if (company === '中科系') {
  23. query['expert.company'] = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] };
  24. } else if (company === '其他') {
  25. query['expert.company'] = {
  26. $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ],
  27. };
  28. } else query['expert.company'] = company;
  29. }
  30. const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  31. const total = await this.model.count(query);
  32. // // 因为聚合管道要兼容使用limit,所以当不传limit时,可能需要所有数据,要手动查出limit
  33. // if (!limit) limit = await this.model.count();
  34. // const aggArr = [
  35. // { $match: { is_expert: true } },
  36. // { $project: { name: 1, phone: 1, email: 1, code: 1, expert: 1 } },
  37. // {
  38. // $lookup: {
  39. // from: 'expert',
  40. // localField: '_id',
  41. // foreignField: 'user_id',
  42. // as: 'expert',
  43. // },
  44. // },
  45. // { $unwind: '$expert' },
  46. // { $match: query },
  47. // ];
  48. // const dataAggArr = _.cloneDeep(aggArr);
  49. // if (skip) dataAggArr.push({ $skip: parseInt(skip) });
  50. // if (limit > 0) dataAggArr.push({ $limit: parseInt(limit) });
  51. // let data = await this.personal.aggregate(dataAggArr);
  52. // data = data.map(i => {
  53. // const { expert, ...info } = _.omit(i, [ '_id' ]);
  54. // const object = { ...expert, ...info };
  55. // return object;
  56. // });
  57. // const totalSearch = await this.personal.aggregate(aggArr);
  58. // const total = _.get(_.head(totalSearch), 'total', 0);
  59. return { data, total };
  60. }
  61. // async fetch({ user_id }) {
  62. // const personal = await this.personal.findById(user_id, 'name phone email code');
  63. // if (!personal) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到个人用户部分信息');
  64. // let expert = await this.model.findOne({ user_id });
  65. // if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家部分信息');
  66. // expert = JSON.parse(JSON.stringify(expert));
  67. // const pinfo = _.omit(JSON.parse(JSON.stringify(personal)), [ '_id' ]);
  68. // expert = { ...pinfo, ...expert };
  69. // return expert;
  70. // }
  71. /**
  72. * 创建用户
  73. * @param {Object} params 用户信息
  74. */
  75. async create({ password, ...data }) {
  76. data.password = { secret: password };
  77. const { phone } = data;
  78. // 检查手机号
  79. const num = await this.model.count({ phone, isdel: '0' });
  80. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有专家用户使用该手机号');
  81. return await this.model.create(data);
  82. }
  83. async delete({ id }) {
  84. const object = await this.model.findById(id);
  85. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  86. object.isdel = '1';
  87. await object.save();
  88. }
  89. /**
  90. * 修改密码
  91. * @param {Object} {id,password} 用户id和密码
  92. */
  93. async password({ id, password }) {
  94. const object = await this.model.findById(id);
  95. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  96. object.password = { secret: password };
  97. await object.save();
  98. }
  99. /**
  100. * 登陆
  101. * @param {Object} params 登陆信息
  102. * @property phone 手机号
  103. * @property password 密码
  104. */
  105. async login({ phone, password, code }) {
  106. const object = await this.model.findOne({ phone, isdel: '0' }, '+password');
  107. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  108. const { password: op, status } = object;
  109. const { secret } = op;
  110. if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
  111. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  112. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  113. const { secret: secrets } = this.config.jwt;
  114. const token = jwt.sign(data, secrets);
  115. // 记录登陆
  116. // let number = await this.redis.get('login_number') || 0;
  117. // number++;
  118. // await this.redis.set('login_number', number);
  119. return token;
  120. }
  121. }
  122. module.exports = ExpertService;