user.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 assert = require('assert');
  6. //
  7. class UserService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'user');
  10. this.redis = this.app.redis;
  11. this.deleteUserKey = 'deleteUser:';
  12. this.redisTimeout = this.app.config.redisTimeout;
  13. }
  14. /**
  15. * 生成操作key
  16. * @param {String} opera_id 操作人id
  17. * @param {String} target 操作对象数据
  18. */
  19. async getDeleteUserKey(opera_id, target) {
  20. // 生成key
  21. const key = `${this.deleteUserKey}${opera_id}`;
  22. // 存储该key可操作的人
  23. await this.redis.set(key, target, 'EX', this.redisTimeout);
  24. // 返回key
  25. return key;
  26. }
  27. /**
  28. * 解析key数据
  29. * @param {Object} key 操作key
  30. */
  31. async getKeyData(key) {
  32. const target = await this.redis.get(key);
  33. if (target) await this.redis.del(key);
  34. const arr = key.split(this.deleteUserKey);
  35. const opera_id = _.last(arr);
  36. return { opera_id, target };
  37. }
  38. }
  39. module.exports = UserService;