Menu.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\controller\common;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\DataExtend;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\system\SystemMenu;
  7. use app\service\system\MenuService;
  8. use DI\Attribute\Inject;
  9. use LinFly\Annotation\Attributes\Route\Controller;
  10. use LinFly\Annotation\Attributes\Route\GetMapping;
  11. use LinFly\Annotation\Attributes\Route\Middleware;
  12. use LinFly\Annotation\Attributes\Route\PostMapping;
  13. use support\Request;
  14. use support\Response;
  15. #[Controller(prefix: "/api/menu"),Middleware(AuthMiddleware::class)]
  16. class Menu extends Base
  17. {
  18. #[Inject]
  19. protected MenuService $service;
  20. /**
  21. * 获取菜单
  22. */
  23. #[GetMapping("list")]
  24. public function getMenuList(Request $request): Response
  25. {
  26. try {
  27. $param = $this->_valid([
  28. "form.default" => $request->user['type'],
  29. "type.default" => 1
  30. ]);
  31. $hide = 0;
  32. $menu = $this->service->getMenuList($request->user['is_super'],$hide,$param['form']);
  33. $permissionsData = [];
  34. foreach ($menu as $val) {
  35. if ($val['type'] == 'button') {
  36. $permissionsData[] = $val['name'];
  37. }
  38. }
  39. $menu = $this->filterMenu(DataExtend::arr2tree($menu),$param['type']);
  40. if($param['type'] == 1) {
  41. $permissions = $permissionsData;
  42. $dashboardGrid = [];
  43. return successTrans("success.data",compact('menu','permissions','dashboardGrid'));
  44. }
  45. return successTrans("success.data",$menu);
  46. } catch (\Throwable $throwable) {
  47. echo $throwable->getFile()."\n";
  48. echo $throwable->getLine()."\n";
  49. return error($throwable->getMessage());
  50. }
  51. }
  52. /**
  53. * 更新
  54. * @param Request $request
  55. * @return Response
  56. */
  57. #[PostMapping("save")]
  58. public function saveMenuData(Request $request): Response
  59. {
  60. try {
  61. $state = (new SystemMenu)->setAutoData($request->post());
  62. if(!$state) return errorTrans("error.data");
  63. return successTrans("success.data");
  64. } catch (\Throwable $throwable) {
  65. return error($throwable->getMessage());
  66. }
  67. }
  68. }