123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2024/11/21
- * @time: 13:35
- */
- namespace app\controller\admin\system;
- use app\common\AdminBaseController;
- use app\Request;
- use app\services\system\admin\SystemMenusServices;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- use think\helper\Str;
- class SystemMenus extends AdminBaseController
- {
- public function __construct(Request $request, SystemMenusServices $service)
- {
- parent::__construct($request);
- $this->service = $service;
- $this->request->filter(['addslashes', 'trim']);
- $this->createParams = [
- ['menu_name', ''],
- ['icon', ''],
- ['params', ''],
- ['menu_path', ''],
- ['api_url', ''],
- ['methods', ''],
- ['unique_auth', ''],
- ['pid', 0],
- ['sort', 0],
- ['auth_type', 1],
- ['type', 1],
- ['is_show', 1],
- ['is_show_path', 0],
- ['extend', []],
- ];
- $this->saveDeal = $this->updateDeal = function (&$data) {
- if (!$data['menu_name']) throw new ValidateException('请输入菜单名称');
- if ($data['auth_type'] == 1) {
- if (!$data['menu_path']) throw new ValidateException('请填写菜单路径');
- } else if ($data['auth_type'] == 2) {
- if (!$data['api_url']) throw new ValidateException('请填写接口路径');
- if (!in_array($data['methods'], ['GET', 'POST', 'PUT', 'DELETE'])) {
- throw new ValidateException('请选择接口类型');
- }
- }
- $data['is_show'] = ($data['auth_type'] == 2 ? 1 : $data['is_show']);
- if ($data['auth_type'] == 2) $data['extend'] = '';
- $data['extend'] = $data['extend'] ? json_encode($data['extend']) : '';
- };
- $this->searchable = [
- ['type', 1],
- ['is_show', ''],
- ['keyword', ''],
- ];
- }
- /**
- * 显示和隐藏
- * @param $id
- * @return mixed
- */
- public function show($id)
- {
- if (!$id) {
- return $this->error('参数错误,请重新打开');
- }
- [$show] = $this->request->postMore([['is_show', 0]], true);
- if ($this->service->update($id, ['is_show' => $show])) {
- return $this->success('修改成功');
- } else {
- return $this->error('修改失败');
- }
- }
- /**
- * 获取菜单数据
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function menus()
- {
- [$menus, $unique] = $this->service->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
- return app('json')->success(['menus' => $menus, 'unique' => $unique]);
- }
- /**
- * 获取接口列表
- * @return array
- */
- public function ruleList($type = 1)
- {
- $this->app = app();
- $rule = $type == 1 ? 'adminapi/' : 'storeapi/';
- $this->app->route->setTestMode(true);
- $this->app->route->clear();
- $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
- $files = is_dir($path) ? scandir($path) : [];
- foreach ($files as $file) {
- if (strpos($file, '.php')) {
- include $path . $file;
- }
- }
- $ruleList = $this->app->route->getRuleList();
- $ruleNewList = [];
- foreach ($ruleList as $item) {
- if (Str::contains($item['rule'], $rule)) {
- $ruleNewList[] = $item;
- }
- }
- foreach ($ruleNewList as $key => &$value) {
- $only = $value['option']['only'] ?? [];
- $route = is_string($value['route']) ? explode('/', $value['route']) : [];
- $value['route'] = is_string($value['route']) ? $value['route'] : '';
- $action = $route[count($route) - 1] ?? null;
- if ($only && $action && !in_array($action, $only)) {
- unset($ruleNewList[$key]);
- }
- $except = $value['option']['except'] ?? [];
- if ($except && $action && in_array($action, $except)) {
- unset($ruleNewList[$key]);
- }
- $middleware = $value['option']['middleware'] ?? [];
- $new_middleware = [];
- foreach ($middleware as $v) {
- if (is_array($v)) $new_middleware[] = $v[0];
- }
- $value['option']['middleware'] = $new_middleware;
- }
- $ruleList = $ruleNewList;
- //获取所有的路由
- $menuApiList = $this->service->getColumn(['auth_type' => 2, 'type' => $type], "concat(`api_url`,'_',lower(`methods`)) as rule");
- if ($menuApiList) $menuApiList = array_column($menuApiList, 'rule');
- $list = [];
- $action_arr = ['index', 'read', 'create', 'save', 'update', 'edit', 'delete'];
- $middleware = $type == 1 ? 'app\http\middleware\admin\AdminCheckRoleMiddleware' : 'app\http\middleware\store\StoreCheckRoleMiddleware';
- foreach ($ruleList as $item) {
- $item['rule'] = str_replace($type == 1 ? 'adminapi/' : 'storeapi/', '', $item['rule']);
- if (!in_array($item['rule'] . '_' . $item['method'], $menuApiList) && isset($item['option']['middleware']) && in_array($middleware, $item['option']['middleware'])) {
- $real_name = $item['option']['real_name'] ?? '';
- if (is_array($real_name)) {
- foreach ($action_arr as $action) {
- if (Str::contains($item['route'], $action)) {
- $real_name = $real_name[$action] ?? '';
- }
- }
- }
- $item['real_name'] = $real_name;
- unset($item['option']);
- $item['method'] = strtoupper($item['method']);
- $list[] = $item;
- }
- }
- return app('json')->success($list);
- }
- }
|