101 lines
2.6 KiB
PHP
101 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Cache\Redis\Admin;
|
|
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\Admin\AuthCode;
|
|
use App\Model\AdminMenu;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use RedisException;
|
|
|
|
class MenuCache
|
|
{
|
|
/**
|
|
* @var RedisCache $redis
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* @var AdminMenu
|
|
*/
|
|
#[Inject]
|
|
protected AdminMenu $adminMenuModel;
|
|
|
|
/**
|
|
* 菜单
|
|
* @var string
|
|
*/
|
|
protected string $menuKey;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->menuKey = AdminRedisKey::adminMenuList();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws RedisException
|
|
*/
|
|
public function getMenu(): array
|
|
{
|
|
if ($this->redis->exists($this->menuKey,'system')) {
|
|
return json_decode($this->redis->get($this->menuKey,'system'),true);
|
|
}
|
|
|
|
$allMenuList = $this->adminMenuModel->getAllMenu();
|
|
|
|
$data = $this->getDbMenu($allMenuList);
|
|
|
|
$this->redis->set($this->menuKey,json_encode($data));
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws RedisException
|
|
*/
|
|
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;
|
|
}
|
|
} |