feat : menu list

This commit is contained in:
2024-10-28 11:55:57 +08:00
parent b828d48672
commit c86c6c1baf
6 changed files with 322 additions and 16 deletions

View File

@@ -15,6 +15,8 @@ use App\Constants\Admin\AuthCode;
use App\Exception\AdminException;
use App\Model\AdminMenu;
use App\Service\Admin\BaseService;
use Exception;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -49,18 +51,154 @@ class RoleMenuService extends BaseService
return $this->return->success('success', ['list' => $data]);
}
public function add()
/**
* 添加逻辑
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function add(): array
{
$url = $this->request->input('menu_url');
if (!empty($this->adminMenuModel->getMenuByUrl($url))) throw new AdminException('已存在相同路由的权限菜单');
$insertButtonArr = [];
if (!empty($permissionList = $this->request->input('permission_list'))) {
$permissionList = json_decode($permissionList, true);
foreach ($permissionList as $one) {
if (empty($one['value'])) throw new AdminException('按钮权限值错误');
if (empty($one['menu_url'])) throw new AdminException('按钮路由值错误');
$insertButtonArr[] = [
'url' => $one['menu_url'],
'value' => $one['value'],
'title' => $one['label'],
'type' => AuthCode::MENU_TYPE_BUTTON,
'status' => AuthCode::MENU_STATUS_ENABLE,
'sort' => 0,
];
}
}
Db::beginTransaction();
try {
$batchModel = $model = new AdminMenu();
$model->parent_id = $this->request->input('parent_id',0);
$model->url = $url;
$model->title = $this->request->input('menu_name');
$model->value = $this->request->input('menu_value','views');
$model->type = $this->request->input('menu_type',AuthCode::MENU_TYPE_LIST);
$model->icon = $this->request->input('menu_icon','');
$model->status = $this->request->input('menu_status',AuthCode::MENU_STATUS_ENABLE);
$model->sort = $this->request->input('menu_sort',0);
if (!$model->save()) throw new Exception('添加失败');
foreach ($insertButtonArr as &$one) {
$one['parent_id'] = $model->id;
}
if (!$batchModel->insert($insertButtonArr)) throw new Exception('添加失败');
Db::commit();
$this->reconfigurationCache();
} catch (Exception $exception) {
Db::rollBack();
throw new AdminException($exception->getMessage());
}
return $this->return->success();
}
public function edit()
/**
* 修改权限
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function edit(): array
{
$menuId = $this->request->input('menu_id');
$url = $this->request->input('menu_url');
$oldUrlInfo = $this->adminMenuModel->getMenuByUrl($url);
$menuInfo = $this->adminMenuModel->where('id',$menuId)->first();
if (!empty($oldUrlInfo) && $oldUrlInfo->id != $menuId) throw new AdminException('已存在相同路由的权限菜单');
if (empty($menuInfo)) throw new AdminException('路由不存在');
$childrenType = $this->adminMenuModel->getChildMenuType($menuId);
$permissionList = $this->request->input('permission_list');
$insertButtonArr = [];
if (!empty($permissionList) && $childrenType != AuthCode::MENU_TYPE_LIST) {
$permissionList = json_decode($permissionList, true);
foreach ($permissionList as $one) {
if (empty($one['value'])) throw new AdminException('按钮权限值错误');
if (empty($one['menu_url'])) throw new AdminException('按钮路由值错误');
$insertButtonArr[] = [
'parent_id' => $menuId,
'url' => $one['menu_url'],
'value' => $one['value'],
'title' => $one['label'],
'type' => AuthCode::MENU_TYPE_BUTTON,
'status' => AuthCode::MENU_STATUS_ENABLE,
'sort' => 0,
];
}
}
Db::beginTransaction();
try {
if (!empty($permissionList)) {
$model = new AdminMenu();
if (!$model->insert($insertButtonArr)) throw new Exception('添加失败');
}
$menuInfo->parent_id = $this->request->input('parent_id',0);
$menuInfo->url = $url;
$menuInfo->title = $this->request->input('menu_name');
$menuInfo->value = $this->request->input('menu_value','views');
$menuInfo->type = $this->request->input('menu_type',AuthCode::MENU_TYPE_LIST);
$menuInfo->icon = $this->request->input('menu_icon','');
$menuInfo->status = $this->request->input('menu_status',AuthCode::MENU_STATUS_ENABLE);
$menuInfo->sort = $this->request->input('menu_sort',0);
if (!$menuInfo->save()) throw new Exception('添加失败');
Db::commit();
$this->reconfigurationCache();
} catch (Exception $exception) {
Db::rollBack();
throw new AdminException($exception->getMessage());
}
return $this->return->success();
}
public function del()
/**
* 删除逻辑
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function del(): array
{
$menuId = $this->request->input('menu_id');
$res = $this->adminMenuModel->where('id',$menuId)->first();
if (!$res) throw new AdminException('路由不存在');
$ids = [$res->id];
$topIds = [$res->id];
while(true) {
$topIds = $this->adminMenuModel->whereIn('parent_id',$topIds)->pluck('id')->toArray();
$ids = array_merge($ids,$topIds);
if (empty($oneLevelIds)) break;
}
$this->adminMenuModel->whereIn('id',$ids)->delete();
$this->reconfigurationCache();
return $this->return->success();
}
@@ -72,7 +210,7 @@ class RoleMenuService extends BaseService
{
$menuId = $this->request->input('menu_id');
$res = $this->adminMenuModel->where('id',$menuId)->first();
if (!$res) throw new AdminException('角色不存在');
if (!$res) throw new AdminException('路由不存在');
$res = $res->toArray();
//闭包函数获取子集
@@ -100,4 +238,31 @@ class RoleMenuService extends BaseService
return $this->return->success('success',['info' => $res]);
}
/**
* 构建缓存
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function buildCache()
{
$this->reconfigurationCache();
return $this->return->success('success');
}
/**
* 重构缓存
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
private function reconfigurationCache(): void
{
$this->menuCache->delMenu();
$this->menuCache->getMenu();
}
}