RoleAclCache.Class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 RoleAclCache
  8. {
  9. private $cache;
  10. protected $StaffRelationRoleKey = 'staffRelationRole';//缓存员工和角色的关系
  11. protected $RoleRelationAclKey = 'roleRelationAcl';//缓存角色和权限的关系 hash
  12. protected $staffRelationDataFieldKey = 'staffUidRelationDataField';//缓存员工和数据域的关系 hash
  13. protected $staffIdRelationUidKey = 'staffIdRelationUid';//缓存职工id和uid的关系 hash
  14. protected $uidRelationStaffIdKey = 'uidRelationStaffIdKey';//缓存uid和职工id的关系 hash
  15. protected $shopIdRelationWarehouseIdKey = 'shopIdRelationWarehouseId';//缓存商铺和仓库的关系 hash
  16. protected $authority = 'Authority';//权限缓存
  17. protected $Administrator = 'AdministratorRole';//超级管理员角色
  18. protected $authorityBindRole = 'AuthorityBindRole';//角色绑定权限
  19. public function __construct()
  20. {
  21. $this->cache = Factory::cache('user');
  22. }
  23. /******************* start:缓存员工和角色的关系 value:userCenterId score:roleId **************/
  24. /**
  25. * 缓存员工和角色的关系
  26. * @param $enterpriseId
  27. * @param $uid
  28. * @param $roleId
  29. */
  30. public function cacheStaffAndRole($enterpriseId, $uid, $roleId)
  31. {
  32. $this->cache->zadd($this->StaffRelationRoleKey.'::'.$enterpriseId, $roleId, $uid);
  33. }
  34. /**
  35. * 获取员工的角色Id
  36. * @param $enterpriseId
  37. * @param $uid
  38. * @return mixed
  39. */
  40. public function getRoleIdOfStaff($enterpriseId, $uid)
  41. {
  42. $result = $this->cache->zrange($this->StaffRelationRoleKey.'::'.$enterpriseId, 0,-1, true);
  43. return isset($result[$uid]) ? (int)$result[$uid] : '';
  44. }
  45. /**
  46. * 删除userCenterId和roleId的绑定关系
  47. * @param $enterpriseId
  48. * @param $uid
  49. */
  50. public function deleteStaffRelationRole($enterpriseId, $uid)
  51. {
  52. $this->cache->zrem($this->StaffRelationRoleKey.'::'.$enterpriseId , $uid);
  53. }
  54. /******************* end:缓存员工和角色的关系 value:userCenterId score:roleId **************/
  55. /******************* start:角色与权限的关系 **************/
  56. /**
  57. * 缓存 角色组id 和 权限
  58. * @param $enterpriseId
  59. * @param $roleId
  60. * @param $aclData
  61. * @return mixed
  62. */
  63. public function cacheRoleIdAndAcl($enterpriseId, $roleId, $aclData = [])
  64. {
  65. return $this->cache->hset($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId, json_encode($aclData));
  66. }
  67. /**
  68. * 删除角色组对应的权限
  69. * @param $enterpriseId
  70. * @param $roleId
  71. * @return mixed
  72. */
  73. public function delRoleIdAndAcl($enterpriseId, $roleId)
  74. {
  75. return $this->cache->hdel($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId);
  76. }
  77. /**
  78. * 获取角色组对应的权限
  79. * @param $enterpriseId
  80. * @param $roleId
  81. * @return mixed
  82. */
  83. public function getRoleIdAndAcl($enterpriseId, $roleId)
  84. {
  85. $aclResult = $this->cache->hget($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId);
  86. return !empty($aclResult) ? json_decode($aclResult, true) : [];
  87. }
  88. /******************* end:角色与权限的关系 **************/
  89. /******************* start:员工与数据域的关系 **************/
  90. /**
  91. * 缓存 员工的userCenterid 和 数据域对象
  92. * @param $enterpriseId
  93. * @param $userCenterId
  94. * @param $dataField
  95. * @return mixed
  96. */
  97. public function cacheStaffUidAndDataField($enterpriseId, $userCenterId, $dataField = [])
  98. {
  99. return $this->cache->hset($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId, json_encode($dataField));
  100. }
  101. /**
  102. * 删除员工的userCenterid 和 数据域对象 的关联
  103. * @param $enterpriseId
  104. * @param $userCenterId
  105. * @return mixed
  106. */
  107. public function delStaffUidAndDataField($enterpriseId, $userCenterId)
  108. {
  109. return $this->cache->hdel($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId);
  110. }
  111. /**
  112. * 获取员工的userCenterid 和 数据域对象 的关联
  113. * @param $enterpriseId
  114. * @param $userCenterId
  115. * @return mixed
  116. */
  117. public function getStaffUidAndDataField($enterpriseId, $userCenterId)
  118. {
  119. $dataField = $this->cache->hget($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId);
  120. return !empty($dataField) ? json_decode($dataField, true) : [];
  121. }
  122. /******************* end:员工与数据域的关系 **************/
  123. /******************* start:职工id与userCenterId的关系 **************/
  124. /**
  125. * 缓存 职工id与userCenterId的关系
  126. * @param $enterpriseId
  127. * @param $staffId
  128. * @param $userCenterId
  129. * @return mixed
  130. */
  131. public function cacheStaffIdAndUserCenterId($enterpriseId, $userCenterId, $staffId)
  132. {
  133. return $this->cache->hset($this->staffIdRelationUidKey.'::'.$enterpriseId, $userCenterId, $staffId);
  134. }
  135. /**
  136. * 获取职工id与userCenterId的关系
  137. * @param $enterpriseId
  138. * @param $staffId
  139. * @return mixed
  140. */
  141. public function getStaffIdAndUserCenterId($enterpriseId, $userCenterId)
  142. {
  143. $staffId = $this->cache->hget($this->staffIdRelationUidKey.'::'.$enterpriseId, $userCenterId);
  144. return $staffId ? $staffId : '';
  145. }
  146. /******************* end:职工id与userCenterId的关系 **************/
  147. /******************* start:userCenterId和职工id的关系 **************/
  148. /**
  149. * @param $enterpriseId
  150. * @param $staffId
  151. * @param $userCenterId
  152. * @return mixed
  153. */
  154. public function cacheUserCenterIdAndStaffId($enterpriseId, $staffId, $userCenterId)
  155. {
  156. return $this->cache->hset($this->uidRelationStaffIdKey.'::'.$enterpriseId, $staffId, $userCenterId);
  157. }
  158. /**
  159. * @param $enterpriseId
  160. * @param $staffId
  161. * @return mixed
  162. */
  163. public function getUserCenterIdAndStaffId($enterpriseId, $staffId)
  164. {
  165. $userCenterId = $this->cache->hget($this->uidRelationStaffIdKey.'::'.$enterpriseId, $staffId);
  166. return $userCenterId ? $userCenterId : '';
  167. }
  168. /******************* end:userCenterId和职工id的关系 **************/
  169. /******************* start:商铺和仓库的关系 **************/
  170. /**
  171. * @param $enterpriseId
  172. * @param $shopId
  173. * @param $warehouseId
  174. * @return mixed
  175. */
  176. public function cacheShopIdAndWarehouseId($enterpriseId, $shopId, $warehouseId)
  177. {
  178. return $this->cache->hset($this->shopIdRelationWarehouseIdKey.'::'.$enterpriseId, $shopId, $warehouseId);
  179. }
  180. /**
  181. * @param $enterpriseId
  182. * @param $shopId
  183. * @return mixed
  184. */
  185. public function getShopIdAndWarehouseId($enterpriseId, $shopId)
  186. {
  187. $warehouseId = $this->cache->hget($this->shopIdRelationWarehouseIdKey.'::'.$enterpriseId, $shopId);
  188. return $warehouseId ? $warehouseId : '';
  189. }
  190. /******************* end:商铺和仓库的关系 **************/
  191. /*******************权限缓存 (无序集合) **************/
  192. /**
  193. * 权限新增
  194. * @param $id
  195. * @param $value
  196. * @return mixed
  197. */
  198. public function addAuthority($value)
  199. {
  200. return $this->cache->sadd($this->authority, strtolower($value));
  201. }
  202. /**
  203. * 权限查询
  204. * @param $value
  205. * @return mixed
  206. */
  207. public function getAuthority($value)
  208. {
  209. return $this->cache->sismember($this->authority, strtolower($value));
  210. }
  211. /**
  212. * 权限删除
  213. * @param $value
  214. * @return mixed
  215. */
  216. public function delAuthority($value)
  217. {
  218. return $this->cache->srem($this->authority, strtolower($value));
  219. }
  220. /***********************超级管理员角色*******************
  221. * 新增
  222. * @param $enterpriseId
  223. * @param $value
  224. * @return mixed
  225. */
  226. public function addAdministrator($enterpriseId, $value)
  227. {
  228. return $this->cache->sadd($this->Administrator.'::'.$enterpriseId, $value);
  229. }
  230. /**
  231. * 查询
  232. * @param $enterpriseId
  233. * @param $value
  234. * @return mixed
  235. */
  236. public function getAdministrator($enterpriseId, $value)
  237. {
  238. return $this->cache->sismember($this->Administrator.'::'.$enterpriseId, $value);
  239. }
  240. /**
  241. * 删除
  242. * @param $enterpriseId
  243. * @param $value
  244. * @return mixed
  245. */
  246. public function delAdministrator($enterpriseId, $value)
  247. {
  248. return $this->cache->srem($this->Administrator.'::'.$enterpriseId, $value);
  249. }
  250. /***********************角色绑定权限*******************
  251. * 新增
  252. * @param $enterpriseId
  253. * @param $roleId
  254. * @param $value
  255. * @return mixed
  256. */
  257. public function addAuthorityBindRole($enterpriseId, $roleId, $value)
  258. {
  259. return $this->cache->sadd($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
  260. }
  261. /**
  262. * 查询
  263. * @param $enterpriseId
  264. * @param $roleId
  265. * @param $value
  266. * @return mixed
  267. */
  268. public function getAuthorityBindRole($enterpriseId, $roleId, $value)
  269. {
  270. return $this->cache->sismember($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
  271. }
  272. /**
  273. * 删除
  274. * @param $enterpriseId
  275. * @param $roleId
  276. * @param $value
  277. * @return mixed
  278. */
  279. public function delAuthorityBindRole($enterpriseId, $roleId, $value = '')
  280. {
  281. if($value){
  282. return $this->cache->srem($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
  283. }else{
  284. return $this->cache->del($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId);
  285. }
  286. }
  287. }