organization.js 3.8 KB

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