organization.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. const assert = require('assert');
  7. // 机构
  8. class OrganizationService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'organization');
  11. this.redis = this.app.redis;
  12. this.model = this.ctx.model.User.Organization;
  13. this.adminModel = this.ctx.model.User.Admin;
  14. this.util = this.ctx.service.util.util;
  15. }
  16. async query(condition, { skip = 0, limit = 0 }) {
  17. const query = await this.dealQueryCondition(_.cloneDeep(condition));
  18. const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
  19. .sort({ 'meta.createdAt': -1 });
  20. return res;
  21. }
  22. async count(condition) {
  23. const query = await this.dealQueryCondition(_.cloneDeep(condition));
  24. const res = await this.model.count(query);
  25. return res;
  26. }
  27. async dealQueryCondition({ role, code, ...condition } = {}) {
  28. condition = this.util.dealQuery(condition);
  29. // 查询业务管理
  30. const busFind = async query => await this.adminModel.find({ ...query, role: '3' }, { code: 1 });
  31. // 查询机构管理
  32. const orgFind = async query => await this.adminModel.find({ ...query, role: '2' }, { code: 1 });
  33. // 查询管理员
  34. const aFind = async query => await this.adminModel.find({ ...query, role: '1' }, { code: 1 });
  35. if (role === '1' && code) {
  36. // 管理员查询
  37. // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
  38. const a = await aFind({ code });
  39. if (a.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该管理员');
  40. const aid = _.get(_.head(a), '_id');
  41. const orgList = await orgFind({ pid: aid });
  42. const busList = await busFind({ pid: orgList.map(i => i._id) });
  43. const codes = [ ...orgList.map(i => i.code), ...busList.map(i => i.code), code ];
  44. condition.code = codes;
  45. } else if (role === '2' && code) {
  46. // 机构查询
  47. // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
  48. const o = await orgFind({ code });
  49. if (o.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该机构');
  50. const oid = _.get(_.head(o), '_id');
  51. const busList = await busFind({ pid: oid });
  52. const codes = [ ...busList.map(i => i.code), code ];
  53. condition.code = codes;
  54. } else if (code) {
  55. // 业务查询
  56. // code直接查询用户返回即可
  57. condition.code = code;
  58. }
  59. // 没有code,超级管理员,说明不限制
  60. return condition;
  61. }
  62. /**
  63. * 创建用户
  64. * @param {Object} params 用户信息
  65. */
  66. async create({ password, ...data }) {
  67. data.password = { secret: password };
  68. const { institution_code } = data;
  69. // 检查是否重复
  70. const num = await this.model.count({ institution_code, isdel: '0' });
  71. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个机构使用该 统一社会信用代码');
  72. return await this.model.create(data);
  73. }
  74. /**
  75. * 修改密码
  76. * @param {Object} {id,password} 用户id和密码
  77. */
  78. async password({ id, password }) {
  79. const object = await this.model.findById(id);
  80. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  81. object.password = { secret: password };
  82. await object.save();
  83. }
  84. /**
  85. * 登陆
  86. * @param {Object} params 登陆信息
  87. * @property phone 手机号
  88. * @property password 密码
  89. */
  90. async login({ phone, password }) {
  91. const object = await this.model.findOne({ phone, isdel: '0' }, '+password');
  92. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  93. const { password: op, status } = object;
  94. const { secret } = op;
  95. if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
  96. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  97. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  98. const { secret: secrets } = this.config.jwt;
  99. const token = jwt.sign(data, secrets);
  100. // 记录登陆
  101. // let number = (await this.redis.get('login_number')) || 0;
  102. // number++;
  103. // await this.redis.set('login_number', number);
  104. return token;
  105. }
  106. // async delete({ id }) {
  107. // const object = await this.model.findById(id);
  108. // if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  109. // object.isdel = '1';
  110. // await object.save();
  111. // }
  112. /**
  113. * 用手机号获取企业列表
  114. * @param {Object} query phone:电话号
  115. * @param {Object} options skip;limit
  116. */
  117. async getList({ name, phone }, { skip = 0, limit = 0 } = {}) {
  118. assert(phone, '缺少手机号');
  119. const query = { phone, status: '1' };
  120. if (name) query.name = name;
  121. const list = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  122. return list;
  123. }
  124. /**
  125. * 企业绑定微信
  126. * @param {Object} body
  127. * @property id 企业id
  128. * @property openid 微信openid
  129. */
  130. async bind({ id, openid }) {
  131. await this.bindRemove({ openid });
  132. const org = await this.model.findById(id);
  133. if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
  134. org.openid = openid;
  135. await org.save();
  136. return await org.save();
  137. }
  138. /**
  139. * 解除绑定
  140. * @param {Object} body
  141. * @property id 企业id
  142. * @property openid 微信id
  143. * 两种方式:id=>指定企业的openid解绑;openid=>删除所有企业的该openid
  144. */
  145. async bindRemove({ id, openid }) {
  146. if (id) {
  147. const org = await this.model.findById(id);
  148. if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
  149. org.openid = undefined;
  150. return await org.save();
  151. }
  152. const res = await this.model.updateMany({ openid }, { openid: undefined });
  153. return res;
  154. }
  155. /**
  156. * 企业微信登录
  157. * @param {Object} body post body
  158. * @property openid
  159. */
  160. async wxLogin({ openid }) {
  161. const org = await this.model.findOne({ openid });
  162. if (!org) return org;
  163. const data = _.omit(JSON.parse(JSON.stringify(org)), [ 'meta', 'password', '__v' ]);
  164. const { secret: secrets } = this.config.jwt;
  165. const token = jwt.sign(data, secrets);
  166. return token;
  167. }
  168. }
  169. module.exports = OrganizationService;