feat : role
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Cache\Redis\Admin;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Constants\Admin\AuthCode;
|
||||
use App\Model\AdminMenu;
|
||||
use App\Service\ServiceTrait\AdminRoleMenuTrait;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
@@ -12,6 +13,8 @@ use RedisException;
|
||||
|
||||
class MenuCache
|
||||
{
|
||||
use AdminRoleMenuTrait;
|
||||
|
||||
/**
|
||||
* @var RedisCache $redis
|
||||
*/
|
||||
@@ -30,6 +33,9 @@ class MenuCache
|
||||
*/
|
||||
protected string $menuKey;
|
||||
|
||||
/**
|
||||
* 构造函数注入 key
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->menuKey = AdminRedisKey::adminMenuList();
|
||||
@@ -51,7 +57,7 @@ class MenuCache
|
||||
|
||||
$data = $this->getDbMenu($allMenuList);
|
||||
|
||||
$this->redis->set($this->menuKey,json_encode($data));
|
||||
$this->redis->set($this->menuKey,json_encode($data),'system');
|
||||
|
||||
return $data;
|
||||
}
|
||||
@@ -64,38 +70,6 @@ class MenuCache
|
||||
*/
|
||||
public function delMenu(): void
|
||||
{
|
||||
$this->redis->delete($this->menuKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成合适的数据
|
||||
* @param array $allMenuList
|
||||
* @param int $parentId
|
||||
* @return array
|
||||
*/
|
||||
private function getDbMenu(array $allMenuList, int $parentId = 0): array
|
||||
{
|
||||
$menuList = [];
|
||||
foreach ($allMenuList as $menu) {
|
||||
if ($menu['parent_id'] == $parentId) {
|
||||
$children = $this->getDbMenu($allMenuList, (int)$menu['id']);
|
||||
if (!empty($children)) {
|
||||
//获取第一个 type 如何是菜单
|
||||
if ($children[0]['type'] == AuthCode::MENU_TYPE_LIST) {
|
||||
$menu['children'] = $children;
|
||||
} else {
|
||||
foreach ($children as $child) {
|
||||
$menu['permissionList'][] = [
|
||||
'id' => $child['id'],
|
||||
'label' => $child['title'],
|
||||
'value' => $child['value'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
$menuList[] = $menu;
|
||||
}
|
||||
}
|
||||
return $menuList;
|
||||
$this->redis->delete($this->menuKey,'system');
|
||||
}
|
||||
}
|
||||
129
app/Cache/Redis/Admin/RoleCache.php
Normal file
129
app/Cache/Redis/Admin/RoleCache.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace App\Cache\Redis\Admin;
|
||||
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Constants\Admin\AuthCode;
|
||||
use App\Model\AdminMenu;
|
||||
use App\Model\AdminRoleMenu;
|
||||
use App\Service\ServiceTrait\AdminRoleMenuTrait;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class RoleCache
|
||||
{
|
||||
use AdminRoleMenuTrait;
|
||||
|
||||
/**
|
||||
* 注入 redis 方法
|
||||
* @var RedisCache $redis
|
||||
*/
|
||||
#[Inject]
|
||||
protected RedisCache $redis;
|
||||
|
||||
/**
|
||||
* 注入模型
|
||||
* @var AdminRoleMenu $adminRoleMenuModel
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminRoleMenu $adminRoleMenuModel;
|
||||
|
||||
/**
|
||||
* 注入模型
|
||||
* @var AdminMenu $adminMenuModel
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminMenu $adminMenuModel;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $roleMenuArrKey;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $roleMenuListKey;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $roleId = 0;
|
||||
|
||||
/**
|
||||
* 获取 key
|
||||
* @return void
|
||||
*/
|
||||
private function getKey(): void
|
||||
{
|
||||
$this->roleMenuArrKey = AdminRedisKey::adminMenuArrByRoleId($this->roleId);
|
||||
$this->roleMenuListKey = AdminRedisKey::adminMenuListByRoleId($this->roleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $roleId
|
||||
* @return array|array[]
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \RedisException
|
||||
*/
|
||||
public function getRoleCache(int $roleId): array
|
||||
{
|
||||
$this->roleId = $roleId;
|
||||
|
||||
$this->getKey();
|
||||
|
||||
if ($this->redis->exists($this->roleMenuArrKey,'system') && $this->redis->exists($this->roleMenuListKey,'system')) {
|
||||
return [
|
||||
'role_arr' => $this->redis->sMembers($this->roleMenuArrKey,'system'),
|
||||
'role_list' => json_decode($this->redis->get($this->roleMenuListKey,'system'),true),
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->roleId == AuthCode::SUPERADMIN) {
|
||||
$menuIds = $this->adminMenuModel->getAllIds();
|
||||
$data = (new MenuCache)->getMenu();
|
||||
// $menuList = $this->adminMenuModel->getAllMenu();
|
||||
} else {
|
||||
$menuIds = $this->adminRoleMenuModel->getMenuByRoleId($this->roleId);
|
||||
$menuList = $this->adminMenuModel->getRoleMenu($menuIds);
|
||||
$data = $this->getDbMenu($menuList);
|
||||
}
|
||||
|
||||
if (empty($menuIds) || empty($menuList)) {
|
||||
return [
|
||||
'role_arr' => [],
|
||||
'role_list' => []
|
||||
];
|
||||
}
|
||||
|
||||
$this->delRoleCache();
|
||||
|
||||
$this->redis->set($this->roleMenuListKey,json_encode($data),'system');
|
||||
$this->redis->sAddBatch($this->roleMenuArrKey,$menuIds,'system');
|
||||
|
||||
return [
|
||||
'role_arr' => $menuIds,
|
||||
'role_list' => $data
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $roleId
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \RedisException
|
||||
*/
|
||||
public function delRoleCache(int $roleId = 0): void
|
||||
{
|
||||
if ($roleId > 0) {
|
||||
$this->roleId = $roleId;
|
||||
$this->getKey();
|
||||
}
|
||||
|
||||
$this->redis->delete($this->roleMenuArrKey,'system');
|
||||
$this->redis->delete($this->roleMenuListKey,'system');
|
||||
}
|
||||
}
|
||||
@@ -272,6 +272,21 @@ class RedisCache
|
||||
return $this->getRedis($poolName)->sRem($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合批量添加
|
||||
* @param $key
|
||||
* @param array $values
|
||||
* @param string $poolName
|
||||
* @return bool|int|Redis
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws RedisException
|
||||
*/
|
||||
public function sAddBatch($key, array $values, string $poolName = 'default')
|
||||
{
|
||||
return $this->getRedis($poolName)->sAddArray($key, $values);
|
||||
}
|
||||
|
||||
// +--------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// | hash
|
||||
// +--------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user