EnterpriseCache.Class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Jindouyun\Cache;
  3. use http\Exception;
  4. use Mall\Framework\Factory;
  5. use Mall\Framework\Core\ResultWrapper;
  6. use Mall\Framework\Core\ErrorCode;
  7. class EnterpriseCache
  8. {
  9. private $cache;
  10. protected $UserRelationEnterpriseKey = 'userRelationEnterprise';//用户id和公司id关联缓存
  11. protected $EnterpriseIdRelationTokenKey = 'enterpriseIdRelationTokenKey';
  12. protected $StaffTotalKey = 'EnterpriseStaffTotal';//企业员工总数
  13. protected $enterpriseSettingKey = 'EnterpriseSetting';//企业设置
  14. // 企业相关配置信息数据
  15. protected $enterpriseInfoData = 'enterpriseInfoData';
  16. public function __construct()
  17. {
  18. $this->cache = Factory::cache('user');
  19. }
  20. /******************* start:userCenterId与企业Id的关联 value:enterpriseId score:roleType **************/
  21. /**
  22. * 增:
  23. * 缓存userCenterId和enterpriseId关系数据
  24. * @param $uid int 用户id
  25. * @param $roleType int 角色
  26. * @param $enterpriseId 企业id
  27. * @return
  28. */
  29. public function cacheUserCenterIdAndEnterpriseId($uid, $roleType, $enterpriseId)
  30. {
  31. $this->cache->zadd($this->UserRelationEnterpriseKey.'::'.$uid, $roleType, $enterpriseId);
  32. }
  33. /**
  34. * 查:
  35. * 获取用户和企业的关联关系数组
  36. *
  37. * @param $userCenterId
  38. * @param int $enterpriseId 公司ID
  39. *
  40. * @return boolean
  41. */
  42. public function getUserCenterIdAndEnterpriseIds($userCenterId)
  43. {
  44. $result = $this->cache->zrangebyscore($this->UserRelationEnterpriseKey.'::'.$userCenterId, 1,2, array('withscores' => TRUE));
  45. return $result;
  46. }
  47. /**
  48. * 删:
  49. * 删除userCenterId和企业id的绑定关系
  50. * @param $userCenterId
  51. * @param int $enterpriseId 被删除的企业id
  52. */
  53. public function deleteEnterprise($userCenterId, $enterpriseId)
  54. {
  55. $this->cache->zrem($this->UserRelationEnterpriseKey.'::'.$userCenterId , $enterpriseId);
  56. }
  57. /******************* end:userCenterId与企业Id的关联 value:enterpriseId score:roleType **************/
  58. /******************* start:企业token与企业id的关联 value:Token score:enterpriseId **************/
  59. /**
  60. * 缓存 企业id 和 token 关联关系
  61. * @param $enterpriseId
  62. * @param $token
  63. * @return ResultWrapper
  64. */
  65. public function enterpriseIdAndTokenCache($enterpriseId, $token)
  66. {
  67. return $this->cache->zadd($this->EnterpriseIdRelationTokenKey, $enterpriseId, $token);
  68. }
  69. /**
  70. * 根据token获取企业id 反解token需要
  71. * @param $token
  72. * @return
  73. */
  74. public function getEnterpriseIdByToken($token)
  75. {
  76. return $this->cache->zscore($this->EnterpriseIdRelationTokenKey, $token);
  77. }
  78. /******************* end:企业token与企业id的关联 value:Token score:enterpriseId **************/
  79. /**
  80. * 缓存企业数据
  81. */
  82. public function EnterpriseDataCache($enterpriseId, $field, $value)
  83. {
  84. return $this->cache->hset($enterpriseId, $field, $value);
  85. }
  86. /**
  87. * 获取企业数据
  88. */
  89. public function getEnterpriseData($enterpriseId, $field)
  90. {
  91. return $this->cache->hget($enterpriseId, $field);
  92. }
  93. /******************************** 缓存企业下员工总数(未删除) **************************/
  94. public function setStaffTotalByEnterpriseId($enterpriseId, $total)
  95. {
  96. $result = $this->cache->zadd($this->StaffTotalKey, $total, $enterpriseId);
  97. return $result;
  98. }
  99. public function getStaffTotalByEnterpriseId($enterpriseId)
  100. {
  101. $result = $this->cache->zscore($this->StaffTotalKey, $enterpriseId);
  102. return $result;
  103. }
  104. public function delStaffTotalByEnterpriseId($enterpriseId)
  105. {
  106. $result = $this->cache->del($this->StaffTotalKey, $enterpriseId);
  107. return $result;
  108. }
  109. /******************************** 缓存企业下商铺总数(未删除) **************************/
  110. public function setShopTotalByEnterpriseId($enterpriseId, $total)
  111. {
  112. $result = $this->cache->zadd($this->StaffTotalKey, $total, $enterpriseId);
  113. return $result;
  114. }
  115. public function getShopTotalByEnterpriseId($enterpriseId)
  116. {
  117. $result = $this->cache->zscore($this->StaffTotalKey, $enterpriseId);
  118. return $result;
  119. }
  120. public function delShopTotalByEnterpriseId($enterpriseId)
  121. {
  122. $result = $this->cache->del($this->StaffTotalKey, $enterpriseId);
  123. return $result;
  124. }
  125. /******************************* 缓存企业设置数据 ************************************/
  126. /**
  127. * 添加企业设置
  128. * @param $key
  129. * @param $value
  130. * @return bool
  131. */
  132. public function setEnterpriseSetting($key,$value)
  133. {
  134. $data = json_encode($value);
  135. $result = $this->cache->hset($this->enterpriseSettingKey, $key, $data);
  136. if (!$result) {
  137. return false;
  138. }
  139. return true;
  140. }
  141. /**
  142. * 获取企业设置
  143. * @param $key
  144. * @return array|mixed
  145. */
  146. public function getEnterpriseSetting($key)
  147. {
  148. $result = $this->cache->hget($this->enterpriseSettingKey, $key);
  149. if (!$result) return [];
  150. return json_decode($result, true);
  151. }
  152. /**
  153. * 删除企业设置
  154. * @param $key
  155. * @return mixed
  156. */
  157. public function delEnterpriseSetting($key)
  158. {
  159. return $this->cache->hdel($this->enterpriseSettingKey, $key);
  160. }
  161. }