expert.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. this.adminModel = this.ctx.model.User.Admin;
  13. this.util = this.ctx.service.util.util;
  14. }
  15. async query({ skip = 0, limit, ...condition } = {}) {
  16. condition = this.util.dealQuery(condition);
  17. const { code, status, company, name, phone } = condition;
  18. let query = {};
  19. if (code) query.code = code;
  20. if (status) query.status = status;
  21. if (name) query.name = name;
  22. if (phone) query.phone = phone;
  23. if (company) {
  24. if (company === '中科系') {
  25. query.company = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] };
  26. } else if (company === '其他') {
  27. query.company = {
  28. $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ],
  29. };
  30. } else query.company = company;
  31. }
  32. query = await this.dealQueryCondition(query);
  33. const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  34. const total = await this.model.count(query);
  35. // // 因为聚合管道要兼容使用limit,所以当不传limit时,可能需要所有数据,要手动查出limit
  36. // if (!limit) limit = await this.model.count();
  37. // const aggArr = [
  38. // { $match: { is_expert: true } },
  39. // { $project: { name: 1, phone: 1, email: 1, code: 1, expert: 1 } },
  40. // {
  41. // $lookup: {
  42. // from: 'expert',
  43. // localField: '_id',
  44. // foreignField: 'user_id',
  45. // as: 'expert',
  46. // },
  47. // },
  48. // { $unwind: '$expert' },
  49. // { $match: query },
  50. // ];
  51. // const dataAggArr = _.cloneDeep(aggArr);
  52. // if (skip) dataAggArr.push({ $skip: parseInt(skip) });
  53. // if (limit > 0) dataAggArr.push({ $limit: parseInt(limit) });
  54. // let data = await this.personal.aggregate(dataAggArr);
  55. // data = data.map(i => {
  56. // const { expert, ...info } = _.omit(i, [ '_id' ]);
  57. // const object = { ...expert, ...info };
  58. // return object;
  59. // });
  60. // const totalSearch = await this.personal.aggregate(aggArr);
  61. // const total = _.get(_.head(totalSearch), 'total', 0);
  62. return { data, total };
  63. }
  64. async dealQueryCondition({ role, code, ...condition } = {}) {
  65. condition = this.util.dealQuery(condition);
  66. // 查询业务管理
  67. const busFind = async query => await this.adminModel.find({ ...query, role: '3' }, { code: 1 });
  68. // 查询机构管理
  69. const orgFind = async query => await this.adminModel.find({ ...query, role: '2' }, { code: 1 });
  70. // 查询管理员
  71. const aFind = async query => await this.adminModel.find({ ...query, role: '1' }, { code: 1 });
  72. if (role === '1' && code) {
  73. // 管理员查询
  74. // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
  75. const a = await aFind({ code });
  76. if (a.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该管理员');
  77. const aid = _.get(_.head(a), '_id');
  78. const orgList = await orgFind({ pid: aid });
  79. const busList = await busFind({ pid: orgList.map(i => i._id) });
  80. const codes = [ ...orgList.map(i => i.code), ...busList.map(i => i.code), code ];
  81. condition.code = codes;
  82. } else if (role === '2' && code) {
  83. // 机构查询
  84. // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
  85. const o = await orgFind({ code });
  86. if (o.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该机构');
  87. const oid = _.get(_.head(o), '_id');
  88. const busList = await busFind({ pid: oid });
  89. const codes = [ ...busList.map(i => i.code), code ];
  90. condition.code = codes;
  91. } else if (code) {
  92. // 业务查询
  93. // code直接查询用户返回即可
  94. condition.code = code;
  95. }
  96. // 没有code,超级管理员,说明不限制
  97. return condition;
  98. }
  99. // async fetch({ user_id }) {
  100. // const personal = await this.personal.findById(user_id, 'name phone email code');
  101. // if (!personal) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到个人用户部分信息');
  102. // let expert = await this.model.findOne({ user_id });
  103. // if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家部分信息');
  104. // expert = JSON.parse(JSON.stringify(expert));
  105. // const pinfo = _.omit(JSON.parse(JSON.stringify(personal)), [ '_id' ]);
  106. // expert = { ...pinfo, ...expert };
  107. // return expert;
  108. // }
  109. /**
  110. * 创建用户
  111. * @param {Object} params 用户信息
  112. */
  113. async create({ password, ...data }) {
  114. if (!password) password = "123456";
  115. data.password = { secret: password };
  116. const { phone } = data;
  117. // 检查手机号
  118. const num = await this.model.count({ phone, isdel: '0' });
  119. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有专家用户使用该手机号');
  120. return await this.model.create(data);
  121. }
  122. // async delete({ id }) {
  123. // const object = await this.model.findById(id);
  124. // if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  125. // object.isdel = '1';
  126. // await object.save();
  127. // }
  128. /**
  129. * 修改密码
  130. * @param {Object} {id,password} 用户id和密码
  131. */
  132. async password({ id, password }) {
  133. const object = await this.model.findById(id);
  134. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  135. object.password = { secret: password };
  136. await object.save();
  137. }
  138. /**
  139. * 登陆
  140. * @param {Object} params 登陆信息
  141. * @property phone 手机号
  142. * @property password 密码
  143. */
  144. async login({ phone, password, code }) {
  145. const object = await this.model.findOne({ phone, isdel: '0' }, '+password');
  146. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  147. const { password: op, status } = object;
  148. const { secret } = op;
  149. if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
  150. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  151. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  152. const { secret: secrets } = this.config.jwt;
  153. const token = jwt.sign(data, secrets);
  154. // 记录登陆
  155. // let number = await this.redis.get('login_number') || 0;
  156. // number++;
  157. // await this.redis.set('login_number', number);
  158. return token;
  159. }
  160. }
  161. module.exports = ExpertService;