SystemMenus.php 6.1 KB

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