menuCache->getMenu(); return $this->return->success('success', ['list' => $data]); } /** * 添加逻辑 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function add(): array { $url = $this->request->input('menu_url'); if (!empty($this->adminMenuModel->getMenuByUrl($url))) throw new ErrException('已存在相同路由的权限菜单'); $insertButtonArr = []; if (!empty($permissionList = $this->request->input('permission_list'))) { $permissionList = json_decode($permissionList, true); foreach ($permissionList as $one) { if (empty($one['value'])) throw new ErrException('按钮权限值错误'); if (empty($one['menu_url'])) throw new ErrException('按钮路由值错误'); $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 ErrException($exception->getMessage()); } return $this->return->success(); } /** * 修改权限 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function edit(): array { $menuId = (int)$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 ErrException('已存在相同路由的权限菜单'); if (empty($menuInfo)) throw new ErrException('路由不存在'); $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 ErrException('按钮权限值错误'); if (empty($one['menu_url'])) throw new ErrException('按钮路由值错误'); $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 ErrException($exception->getMessage()); } return $this->return->success(); } /** * 删除逻辑 * @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 ErrException('路由不存在'); $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(); } /** * 详情 * @return array */ public function details(): array { $menuId = $this->request->input('menu_id'); $res = $this->adminMenuModel->where('id',$menuId)->first(); if (!$res) throw new ErrException('路由不存在'); $res = $res->toArray(); //获取子集 $res = $this->getChildren($res,$menuId); return $this->return->success('success',['info' => $res]); } /** * @param $res * @param $menuId * @return array */ private function getChildren(&$res,$menuId): array { $children = $this->adminMenuModel->where('parent_id',$menuId)->get(['type','value','title','id']); $res['permission_list'] = []; if (!empty($children)) { $children = $children->toArray(); if ($children[0]['type'] == AuthCode::MENU_TYPE_LIST) { return $res; } foreach ($children as $one) { $res['permission_list'][] = [ 'id' => $one['id'], 'label' => $one['title'], 'value' => $one['value'], ]; } } return $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(); } }