123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace Jindouyun\Cache;
- use http\Exception;
- use Mall\Framework\Factory;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\ErrorCode;
- class RoleAclCache
- {
- private $cache;
- protected $StaffRelationRoleKey = 'staffRelationRole';//缓存员工和角色的关系
- protected $RoleRelationAclKey = 'roleRelationAcl';//缓存角色和权限的关系 hash
- protected $staffRelationDataFieldKey = 'staffUidRelationDataField';//缓存员工和数据域的关系 hash
- protected $staffIdRelationUidKey = 'staffIdRelationUid';//缓存职工id和uid的关系 hash
- protected $uidRelationStaffIdKey = 'uidRelationStaffIdKey';//缓存uid和职工id的关系 hash
- protected $shopIdRelationWarehouseIdKey = 'shopIdRelationWarehouseId';//缓存商铺和仓库的关系 hash
- protected $authority = 'Authority';//权限缓存
- protected $Administrator = 'AdministratorRole';//超级管理员角色
- protected $authorityBindRole = 'AuthorityBindRole';//角色绑定权限
- public function __construct()
- {
- $this->cache = Factory::cache('user');
- }
- /******************* start:缓存员工和角色的关系 value:userCenterId score:roleId **************/
- /**
- * 缓存员工和角色的关系
- * @param $enterpriseId
- * @param $uid
- * @param $roleId
- */
- public function cacheStaffAndRole($enterpriseId, $uid, $roleId)
- {
- $this->cache->zadd($this->StaffRelationRoleKey.'::'.$enterpriseId, $roleId, $uid);
- }
- /**
- * 获取员工的角色Id
- * @param $enterpriseId
- * @param $uid
- * @return mixed
- */
- public function getRoleIdOfStaff($enterpriseId, $uid)
- {
- $result = $this->cache->zrange($this->StaffRelationRoleKey.'::'.$enterpriseId, 0,-1, true);
- return isset($result[$uid]) ? (int)$result[$uid] : '';
- }
- /**
- * 删除userCenterId和roleId的绑定关系
- * @param $enterpriseId
- * @param $uid
- */
- public function deleteStaffRelationRole($enterpriseId, $uid)
- {
- $this->cache->zrem($this->StaffRelationRoleKey.'::'.$enterpriseId , $uid);
- }
- /******************* end:缓存员工和角色的关系 value:userCenterId score:roleId **************/
- /******************* start:角色与权限的关系 **************/
- /**
- * 缓存 角色组id 和 权限
- * @param $enterpriseId
- * @param $roleId
- * @param $aclData
- * @return mixed
- */
- public function cacheRoleIdAndAcl($enterpriseId, $roleId, $aclData = [])
- {
- return $this->cache->hset($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId, json_encode($aclData));
- }
- /**
- * 删除角色组对应的权限
- * @param $enterpriseId
- * @param $roleId
- * @return mixed
- */
- public function delRoleIdAndAcl($enterpriseId, $roleId)
- {
- return $this->cache->hdel($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId);
- }
- /**
- * 获取角色组对应的权限
- * @param $enterpriseId
- * @param $roleId
- * @return mixed
- */
- public function getRoleIdAndAcl($enterpriseId, $roleId)
- {
- $aclResult = $this->cache->hget($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId);
- return !empty($aclResult) ? json_decode($aclResult, true) : [];
- }
- /******************* end:角色与权限的关系 **************/
- /******************* start:员工与数据域的关系 **************/
- /**
- * 缓存 员工的userCenterid 和 数据域对象
- * @param $enterpriseId
- * @param $userCenterId
- * @param $dataField
- * @return mixed
- */
- public function cacheStaffUidAndDataField($enterpriseId, $userCenterId, $dataField = [])
- {
- return $this->cache->hset($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId, json_encode($dataField));
- }
- /**
- * 删除员工的userCenterid 和 数据域对象 的关联
- * @param $enterpriseId
- * @param $userCenterId
- * @return mixed
- */
- public function delStaffUidAndDataField($enterpriseId, $userCenterId)
- {
- return $this->cache->hdel($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId);
- }
- /**
- * 获取员工的userCenterid 和 数据域对象 的关联
- * @param $enterpriseId
- * @param $userCenterId
- * @return mixed
- */
- public function getStaffUidAndDataField($enterpriseId, $userCenterId)
- {
- $dataField = $this->cache->hget($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId);
- return !empty($dataField) ? json_decode($dataField, true) : [];
- }
- /******************* end:员工与数据域的关系 **************/
- /******************* start:职工id与userCenterId的关系 **************/
- /**
- * 缓存 职工id与userCenterId的关系
- * @param $enterpriseId
- * @param $staffId
- * @param $userCenterId
- * @return mixed
- */
- public function cacheStaffIdAndUserCenterId($enterpriseId, $userCenterId, $staffId)
- {
- return $this->cache->hset($this->staffIdRelationUidKey.'::'.$enterpriseId, $userCenterId, $staffId);
- }
- /**
- * 获取职工id与userCenterId的关系
- * @param $enterpriseId
- * @param $staffId
- * @return mixed
- */
- public function getStaffIdAndUserCenterId($enterpriseId, $userCenterId)
- {
- $staffId = $this->cache->hget($this->staffIdRelationUidKey.'::'.$enterpriseId, $userCenterId);
- return $staffId ? $staffId : '';
- }
- /******************* end:职工id与userCenterId的关系 **************/
- /******************* start:userCenterId和职工id的关系 **************/
- /**
- * @param $enterpriseId
- * @param $staffId
- * @param $userCenterId
- * @return mixed
- */
- public function cacheUserCenterIdAndStaffId($enterpriseId, $staffId, $userCenterId)
- {
- return $this->cache->hset($this->uidRelationStaffIdKey.'::'.$enterpriseId, $staffId, $userCenterId);
- }
- /**
- * @param $enterpriseId
- * @param $staffId
- * @return mixed
- */
- public function getUserCenterIdAndStaffId($enterpriseId, $staffId)
- {
- $userCenterId = $this->cache->hget($this->uidRelationStaffIdKey.'::'.$enterpriseId, $staffId);
- return $userCenterId ? $userCenterId : '';
- }
- /******************* end:userCenterId和职工id的关系 **************/
- /******************* start:商铺和仓库的关系 **************/
- /**
- * @param $enterpriseId
- * @param $shopId
- * @param $warehouseId
- * @return mixed
- */
- public function cacheShopIdAndWarehouseId($enterpriseId, $shopId, $warehouseId)
- {
- return $this->cache->hset($this->shopIdRelationWarehouseIdKey.'::'.$enterpriseId, $shopId, $warehouseId);
- }
- /**
- * @param $enterpriseId
- * @param $shopId
- * @return mixed
- */
- public function getShopIdAndWarehouseId($enterpriseId, $shopId)
- {
- $warehouseId = $this->cache->hget($this->shopIdRelationWarehouseIdKey.'::'.$enterpriseId, $shopId);
- return $warehouseId ? $warehouseId : '';
- }
- /******************* end:商铺和仓库的关系 **************/
- /*******************权限缓存 (无序集合) **************/
- /**
- * 权限新增
- * @param $id
- * @param $value
- * @return mixed
- */
- public function addAuthority($value)
- {
- return $this->cache->sadd($this->authority, strtolower($value));
- }
- /**
- * 权限查询
- * @param $value
- * @return mixed
- */
- public function getAuthority($value)
- {
- return $this->cache->sismember($this->authority, strtolower($value));
- }
- /**
- * 权限删除
- * @param $value
- * @return mixed
- */
- public function delAuthority($value)
- {
- return $this->cache->srem($this->authority, strtolower($value));
- }
- /***********************超级管理员角色*******************
- * 新增
- * @param $enterpriseId
- * @param $value
- * @return mixed
- */
- public function addAdministrator($enterpriseId, $value)
- {
- return $this->cache->sadd($this->Administrator.'::'.$enterpriseId, $value);
- }
- /**
- * 查询
- * @param $enterpriseId
- * @param $value
- * @return mixed
- */
- public function getAdministrator($enterpriseId, $value)
- {
- return $this->cache->sismember($this->Administrator.'::'.$enterpriseId, $value);
- }
- /**
- * 删除
- * @param $enterpriseId
- * @param $value
- * @return mixed
- */
- public function delAdministrator($enterpriseId, $value)
- {
- return $this->cache->srem($this->Administrator.'::'.$enterpriseId, $value);
- }
- /***********************角色绑定权限*******************
- * 新增
- * @param $enterpriseId
- * @param $roleId
- * @param $value
- * @return mixed
- */
- public function addAuthorityBindRole($enterpriseId, $roleId, $value)
- {
- return $this->cache->sadd($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
- }
- /**
- * 查询
- * @param $enterpriseId
- * @param $roleId
- * @param $value
- * @return mixed
- */
- public function getAuthorityBindRole($enterpriseId, $roleId, $value)
- {
- return $this->cache->sismember($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
- }
- /**
- * 删除
- * @param $enterpriseId
- * @param $roleId
- * @param $value
- * @return mixed
- */
- public function delAuthorityBindRole($enterpriseId, $roleId, $value = '')
- {
- if($value){
- return $this->cache->srem($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
- }else{
- return $this->cache->del($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId);
- }
- }
- }
|