feat: menu

This commit is contained in:
2024-10-27 23:45:20 +08:00
parent 5285dd6972
commit b828d48672
9 changed files with 226 additions and 10 deletions

View File

@@ -13,4 +13,33 @@ class AdminRedisKey
{
return 'admin:token:user:'.$userId;
}
/**
* 权限集合
* @return string
*/
public static function adminMenuList(): string
{
return '__system:admin:menu:list:role_id:all';
}
/**
* 角色集合 menu id
* @param int $roleId
* @return string
*/
public static function adminMenuArrByRoleId(int $roleId): string
{
return '__system:admin:menu:arr:role_id:'.$roleId;
}
/**
* 角色权限集合
* @param $roleId
* @return string
*/
public static function adminMenuListByRoleId($roleId): string
{
return '__system:admin:menu:list:role_id:'.$roleId;
}
}

View File

@@ -0,0 +1,81 @@
<?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;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function getMenu(): array
{
$key = AdminRedisKey::adminMenuList();
if ($this->redis->exists($key,'system')) {
return json_decode($this->redis->get($key,'system'),true);
}
$allMenuList = $this->adminMenuModel->getAllMenu();
$data = $this->getDbMenu($allMenuList);
$this->redis->set($key,json_encode($data));
return $data;
}
/**
* 递归生成合适的数据
* @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;
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Constants\Admin;
class AuthCode
{
/**
* 启用
*/
const MENU_STATUS_ENABLE = 1;
/**
* 禁用
*/
const MENU_STATUS_DISABLE = 2;
/**
* 菜单列表
*/
const MENU_TYPE_LIST = 1;
/**
* 按钮
*/
const MENU_TYPE_BUTTON = 2;
}

View File

@@ -35,12 +35,15 @@ class AuthController extends AbstractController
return (new RoleMenuService)->del();
}
#[RequestMapping(path: "menu_list", methods: "GET")]
public function menu_list()
{
return (new RoleMenuService)->handle();
}
public function mean()
#[RequestMapping(path: "menu", methods: "GET")]
#[Scene(scene: "menu_info")]
public function menu(AuthRequest $request)
{
return (new RoleMenuService)->details();
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Model;
use App\Constants\Admin\AuthCode;
use Hyperf\DbConnection\Model\Model;
/**
@@ -42,4 +43,12 @@ class AdminMenu extends Model
const UPDATED_AT = 'update_time';
/**
* 获取所有
* @return array
*/
public function getAllMenu(): array
{
return $this->where('status', AuthCode::MENU_STATUS_ENABLE)->orderBy('sort', 'desc')->get()->toArray();
}
}

View File

@@ -23,17 +23,20 @@ class AuthRequest extends FormRequest
{
return [
'role_id' => 'required|integer',
'menu_id' => 'required|integer',
];
}
public function messages(): array
{
return [
''
'role_id.required' => 'id必填',
'menu_id.required' => 'id必填',
];
}
protected array $scenes = [
'role_info' => 'role_id',
'menu_info' => 'menu_id',
];
}

View File

@@ -10,13 +10,43 @@ declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Cache\Redis\Admin\MenuCache;
use App\Constants\Admin\AuthCode;
use App\Exception\AdminException;
use App\Model\AdminMenu;
use App\Service\Admin\BaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
class RoleMenuService extends BaseService
{
public function handle()
/**
* 注入缓存
* @var MenuCache $menuCache
*/
#[Inject]
protected MenuCache $menuCache;
/**
* @var AdminMenu $adminMenuModel
*/
#[Inject]
protected AdminMenu $adminMenuModel;
/**
* 列表
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function handle(): array
{
return $this->return->success();
$data = $this->menuCache->getMenu();
return $this->return->success('success', ['list' => $data]);
}
public function add()
@@ -34,8 +64,40 @@ class RoleMenuService extends BaseService
return $this->return->success();
}
public function details()
/**
* 详情
* @return array
*/
public function details(): array
{
return $this->return->success();
$menuId = $this->request->input('menu_id');
$res = $this->adminMenuModel->where('id',$menuId)->first();
if (!$res) throw new AdminException('角色不存在');
$res = $res->toArray();
//闭包函数获取子集
$res = function() use($res,$menuId) {
$children = $this->adminMenuModel->where('parent_id',$menuId)->select(['type','value','title','id'])->get();
$res['permissionList'] = [];
if (!empty($children)) {
$children = $children->toArray();
if ($children[0]['type'] == AuthCode::MENU_TYPE_LIST) {
return $res;
}
foreach ($children as $one)
{
$res['permissionList'][] = [
'id' => $one['id'],
'label' => $one['title'],
'value' => $one['value'],
];
}
}
return $res;
};
return $this->return->success('success',['info' => $res]);
}
}

View File

@@ -11,7 +11,6 @@ declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Exception\AdminException;
use App\Lib\Log;
use App\Model\AdminRole;
use App\Service\Admin\BaseService;
use Exception;
@@ -133,8 +132,6 @@ class RoleService extends BaseService
$res = $this->adminRoleModel->where('id',$roleId)->first($this->field);
if (!$res) throw new AdminException('角色不存在');
return $this->return->success('success',[
'info' => $res->toArray()
]);
return $this->return->success('success',['info' => $res->toArray()]);
}
}