SystemMenus.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/21
  6. * @time: 13:35
  7. */
  8. namespace app\controller\admin\system;
  9. use app\common\AdminBaseController;
  10. use app\Request;
  11. use app\services\system\admin\SystemMenusServices;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\exception\ValidateException;
  16. use think\helper\Str;
  17. class SystemMenus extends AdminBaseController
  18. {
  19. public function __construct(Request $request, SystemMenusServices $service)
  20. {
  21. parent::__construct($request);
  22. $this->service = $service;
  23. $this->request->filter(['addslashes', 'trim']);
  24. $this->createParams = [
  25. ['menu_name', ''],
  26. ['icon', ''],
  27. ['params', ''],
  28. ['menu_path', ''],
  29. ['api_url', ''],
  30. ['methods', ''],
  31. ['unique_auth', ''],
  32. ['pid', 0],
  33. ['sort', 0],
  34. ['auth_type', 1],
  35. ['is_show', 1],
  36. ['is_show_path', 0],
  37. ['extend', []],
  38. ];
  39. $this->saveDeal = $this->updateDeal = function (&$data) {
  40. $data['type'] = 1;
  41. if (!$data['menu_name']) throw new ValidateException('请输入菜单名称');
  42. if (!$data['menu_name']) throw new ValidateException('请输入菜单名称');
  43. if ($data['auth_type'] == 1) {
  44. if (!$data['menu_path']) throw new ValidateException('请填写菜单路径');
  45. } else if ($data['auth_type'] == 2) {
  46. if (!$data['api_url']) throw new ValidateException('请填写接口路径');
  47. if (!in_array($data['methods'], ['GET', 'POST', 'PUT', 'DELETE'])) {
  48. throw new ValidateException('请选择接口类型');
  49. }
  50. }
  51. $data['is_show'] = ($data['auth_type'] == 2 ? 1 : $data['is_show']);
  52. if ($data['auth_type'] == 2) $data['extend'] = '';
  53. $data['extend'] = json_encode($data['extend']);
  54. };
  55. $this->searchable = [
  56. ['type', 1],
  57. ['is_show', ''],
  58. ['keyword', ''],
  59. ];
  60. }
  61. /**
  62. * 显示和隐藏
  63. * @param $id
  64. * @return mixed
  65. */
  66. public function show($id)
  67. {
  68. if (!$id) {
  69. return $this->error('参数错误,请重新打开');
  70. }
  71. [$show] = $this->request->postMore([['is_show', 0]], true);
  72. if ($this->service->update($id, ['is_show' => $show])) {
  73. return $this->success('修改成功');
  74. } else {
  75. return $this->error('修改失败');
  76. }
  77. }
  78. /**
  79. * 获取菜单数据
  80. * @return mixed
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. */
  85. public function menus()
  86. {
  87. [$menus, $unique] = $this->service->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
  88. return app('json')->success(['menus' => $menus, 'unique' => $unique]);
  89. }
  90. /**
  91. * 获取接口列表
  92. * @return array
  93. */
  94. public function ruleList($type = 1)
  95. {
  96. $this->app = app();
  97. $rule = $type == 1 ? 'adminapi/' : 'storeapi/';
  98. $this->app->route->setTestMode(true);
  99. $this->app->route->clear();
  100. $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
  101. $files = is_dir($path) ? scandir($path) : [];
  102. foreach ($files as $file) {
  103. if (strpos($file, '.php')) {
  104. include $path . $file;
  105. }
  106. }
  107. $ruleList = $this->app->route->getRuleList();
  108. $ruleNewList = [];
  109. foreach ($ruleList as $item) {
  110. if (Str::contains($item['rule'], $rule)) {
  111. $ruleNewList[] = $item;
  112. }
  113. }
  114. foreach ($ruleNewList as $key => &$value) {
  115. $only = $value['option']['only'] ?? [];
  116. $route = is_string($value['route']) ? explode('/', $value['route']) : [];
  117. $value['route'] = is_string($value['route']) ? $value['route'] : '';
  118. $action = $route[count($route) - 1] ?? null;
  119. if ($only && $action && !in_array($action, $only)) {
  120. unset($ruleNewList[$key]);
  121. }
  122. $except = $value['option']['except'] ?? [];
  123. if ($except && $action && in_array($action, $except)) {
  124. unset($ruleNewList[$key]);
  125. }
  126. $middleware = $value['option']['middleware'] ?? [];
  127. $new_middleware = [];
  128. foreach ($middleware as $v) {
  129. if (is_array($v)) $new_middleware[] = $v[0];
  130. }
  131. $value['option']['middleware'] = $new_middleware;
  132. }
  133. $ruleList = $ruleNewList;
  134. //获取所有的路由
  135. $menuApiList = $this->service->getColumn(['auth_type' => 2, 'type' => $type], "concat(`api_url`,'_',lower(`methods`)) as rule");
  136. if ($menuApiList) $menuApiList = array_column($menuApiList, 'rule');
  137. $list = [];
  138. $action_arr = ['index', 'read', 'create', 'save', 'update', 'edit', 'delete'];
  139. $middleware = $type == 1 ? 'app\http\middleware\admin\AdminCheckRoleMiddleware' : 'app\http\middleware\store\StoreCheckRoleMiddleware';
  140. foreach ($ruleList as $item) {
  141. $item['rule'] = str_replace($type == 1 ? 'adminapi/' : 'storeapi/', '', $item['rule']);
  142. if (!in_array($item['rule'] . '_' . $item['method'], $menuApiList) && isset($item['option']['middleware']) && in_array($middleware, $item['option']['middleware'])) {
  143. $real_name = $item['option']['real_name'] ?? '';
  144. if (is_array($real_name)) {
  145. foreach ($action_arr as $action) {
  146. if (Str::contains($item['route'], $action)) {
  147. $real_name = $real_name[$action] ?? '';
  148. }
  149. }
  150. }
  151. $item['real_name'] = $real_name;
  152. unset($item['option']);
  153. $item['method'] = strtoupper($item['method']);
  154. $list[] = $item;
  155. }
  156. }
  157. return app('json')->success($list);
  158. }
  159. }