123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- declare (strict_types=1);
- namespace app\command;
- use Swoole\Coroutine\MySQL\Exception;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use think\event\RouteLoaded;
- use think\facade\Route;
- use app\common\repositories\system\auth\MenuRepository;
- class updateAuth extends Command
- {
- protected $k = [];
- protected $kv =[];
- protected function configure()
- {
-
- $this->setName('setAuth')
- ->addArgument('prompt',Argument::OPTIONAL, 'php think menu [s] / [e]')
- ->setDescription('使用方法: `php think menu` , 可选传参数 s 只显示成功信息');
- }
-
- protected function execute(Input $input, Output $output)
- {
- $prompt = $input->getArgument('prompt');
- $output->writeln('开始执行');
- $output->writeln('<----------------');
- $output->writeln('开始平台权限');
- $sys = $this->routeList('sys',$prompt);
- $output->writeln('开始商户权限');
- $mer = $this->routeList('mer',$prompt);
- $output->writeln('<----------------');
- $output->writeln('平台执行成功,合计: '. $sys .'条 , 商户执行成功,合计: '. $mer .'条');
- }
-
- public function routeList($type, $prompt)
- {
- $this->k = [];
- $this->kv = [];
- $this->app->route->setTestMode(true);
- $this->app->route->clear();
- $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
- if ($type == 'sys')
- include $path . 'admin.php';
- if ($type == 'mer')
- include $path . 'merchant.php';
-
- $this->app->event->trigger(RouteLoaded::class);
- $routeList = $this->app->route->getRuleList();
- $resp = [];
- foreach ($routeList as $k => $item) {
- if ($item['option'] && isset($item['option']['_auth']) && $item['option']['_auth']) {
- if (!(strpos($item['name'], '/') !== false) && !(strpos($item['name'], '@') !== false)) {
- if (isset($item['option']['_init'])) {
- $route = (new $item['option']['_init'][0]())->create($item, $item['option']['_init'][1]);
- } else {
- $route = [$item];
- }
- if ($route) {
- foreach ($route as $one) {
- if (!isset($one['option']['_name'])) $one['option']['_name'] = $one['name'];
- $this->menu($one['option']['_path'] ?? '', $one['option'], $resp);
- }
- }
- }
- }
- }
- return app()->make(MenuRepository::class)->commandMenu($type, $resp, $prompt);
- }
-
- protected function menu($_path, $data, &$resp = [], $isAppend = 0)
- {
- $check = true;
- if ($_path && is_array($data)) {
- $v = [
- 'route' => $data['_name'],
- 'menu_name' => $data['_alias'] ?? '权限',
- 'params' => $data['_params'] ?? '',
- ];
- if (!isset($data['_repeat']) || (isset($data['_repeat']) && !$data['_repeat'])){
- $check = $this->checkRepeat($v['route'], $v['menu_name']);
- $this->k[] = $v['route'];
- $this->kv[$v['route']] = $v['menu_name'];
- }
- if (!$check) {
- throw new Exception( "路由名重复 < " . $v['route']. ' >' . '「'. $v['menu_name']. ' 」');
- }
- if ($isAppend) {
- $_path = 'append_'.$_path;
- }
- $resp[$_path][$data['_name']] = $v;
- if (isset($data['_append']) && !empty($data['_append'])) {
- foreach ($data['_append'] as $datum) {
- $datum['_repeat'] = true;
- $this->menu($datum['_path'] ?? $data['_path'], $datum, $resp, 1);
- }
- }
- }
- return $resp;
- }
- protected function checkRepeat($key, $value)
- {
- if (in_array($key, $this->k)) {
- if ($value != $this->kv[$key]) {
- return false;
- }
- }
- return true;
- }
- }
|