organization.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 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.Organization;
  13. }
  14. /**
  15. * 创建用户
  16. * @param {Object} params 用户信息
  17. */
  18. async create({ password, ...data }) {
  19. data.password = { secret: password };
  20. const { institution_code } = data;
  21. // 检查是否重复
  22. const num = await this.model.count({ institution_code, isdel: '0' });
  23. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个机构使用该 统一社会信用代码');
  24. return await this.model.create(data);
  25. }
  26. /**
  27. * 修改密码
  28. * @param {Object} {id,password} 用户id和密码
  29. */
  30. async password({ id, password }) {
  31. const object = await this.model.findById(id);
  32. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  33. object.password = { secret: password };
  34. await object.save();
  35. }
  36. /**
  37. * 登陆
  38. * @param {Object} params 登陆信息
  39. * @property institution_code 手机号
  40. * @property password 密码
  41. */
  42. async login({ institution_code, password }) {
  43. const object = await this.model.findOne({ institution_code, isdel: '0' }, '+password');
  44. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  45. const { password: op, status } = object;
  46. const { secret } = op;
  47. if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
  48. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  49. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  50. const { secret: secrets } = this.config.jwt;
  51. const token = jwt.sign(data, secrets);
  52. // 记录登陆
  53. let number = await this.redis.get('login_number') || 0;
  54. number++;
  55. await this.redis.set('login_number', number);
  56. return token;
  57. }
  58. async delete({ id }) {
  59. const object = await this.model.findById(id);
  60. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  61. object.isdel = '1';
  62. await object.save();
  63. }
  64. /**
  65. * 用手机号获取企业列表
  66. * @param {Object} query phone:电话号
  67. * @param {Object} options skip;limit
  68. */
  69. async getList({ name, phone }, { skip = 0, limit = 0 } = {}) {
  70. assert(phone, '缺少手机号');
  71. const query = { phone, status: '1' };
  72. if (name) query.name = name;
  73. const list = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  74. return list;
  75. }
  76. /**
  77. * 企业绑定微信
  78. * @param {Object} body
  79. * @property id 企业id
  80. * @property openid 微信openid
  81. */
  82. async bind({ id, openid }) {
  83. await this.bindRemove({ openid });
  84. const org = await this.model.findById(id);
  85. if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
  86. org.openid = openid;
  87. await org.save();
  88. return await org.save();
  89. }
  90. /**
  91. * 解除绑定
  92. * @param {Object} body
  93. * @property id 企业id
  94. * @property openid 微信id
  95. * 两种方式:id=>指定企业的openid解绑;openid=>删除所有企业的该openid
  96. */
  97. async bindRemove({ id, openid }) {
  98. if (id) {
  99. const org = await this.model.findById(id);
  100. if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
  101. org.openid = undefined;
  102. return await org.save();
  103. }
  104. const res = await this.model.updateMany({ openid }, { openid: undefined });
  105. return res;
  106. }
  107. /**
  108. * 企业微信登录
  109. * @param {Object} body post body
  110. * @property openid
  111. */
  112. async wxLogin({ openid }) {
  113. const org = await this.model.findOne({ openid });
  114. if (!org) return org;
  115. const data = _.omit(JSON.parse(JSON.stringify(org)), [ 'meta', 'password', '__v' ]);
  116. const { secret: secrets } = this.config.jwt;
  117. const token = jwt.sign(data, secrets);
  118. return token;
  119. }
  120. }
  121. module.exports = OrganizationService;