updateAuth.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\command;
  13. use Swoole\Coroutine\MySQL\Exception;
  14. use think\console\Command;
  15. use think\console\Input;
  16. use think\console\input\Argument;
  17. use think\console\input\Option;
  18. use think\console\Output;
  19. use think\event\RouteLoaded;
  20. use think\facade\Route;
  21. use app\common\repositories\system\auth\MenuRepository;
  22. class updateAuth extends Command
  23. {
  24. protected $k = [];
  25. protected $kv =[];
  26. protected function configure()
  27. {
  28. // 指令配置
  29. $this->setName('setAuth')
  30. ->addArgument('prompt',Argument::OPTIONAL, 'php think menu [s] / [e]')
  31. ->setDescription('使用方法: `php think menu` , 可选传参数 s 只显示成功信息');
  32. }
  33. /**
  34. * 更新路由
  35. * @Author:Qinii
  36. * @Date: 2020/5/15
  37. * @param Input $input
  38. * @param Output $output
  39. * @return int|void|null
  40. */
  41. protected function execute(Input $input, Output $output)
  42. {
  43. $prompt = $input->getArgument('prompt');
  44. $output->writeln('开始执行');
  45. $output->writeln('<----------------');
  46. $output->writeln('开始平台权限');
  47. $sys = $this->routeList('sys',$prompt);
  48. $output->writeln('开始商户权限');
  49. $mer = $this->routeList('mer',$prompt);
  50. $output->writeln('<----------------');
  51. $output->writeln('平台执行成功,合计: '. $sys .'条 , 商户执行成功,合计: '. $mer .'条');
  52. }
  53. /**
  54. * 路由列表
  55. * @Author:Qinii
  56. * @Date: 2020/5/15
  57. * @param string|null $dir
  58. * @return mixed
  59. */
  60. public function routeList($type, $prompt)
  61. {
  62. $this->k = [];
  63. $this->kv = [];
  64. $this->app->route->setTestMode(true);
  65. $this->app->route->clear();
  66. $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
  67. if ($type == 'sys')
  68. include $path . 'admin.php';
  69. // include $path . 'admin/config.php';
  70. if ($type == 'mer')
  71. include $path . 'merchant.php';
  72. //触发路由载入完成事件
  73. $this->app->event->trigger(RouteLoaded::class);
  74. $routeList = $this->app->route->getRuleList();
  75. $resp = [];
  76. foreach ($routeList as $k => $item) {
  77. if ($item['option'] && isset($item['option']['_auth']) && $item['option']['_auth']) {
  78. if (!(strpos($item['name'], '/') !== false) && !(strpos($item['name'], '@') !== false)) {
  79. if (isset($item['option']['_init'])) {
  80. $route = (new $item['option']['_init'][0]())->create($item, $item['option']['_init'][1]);
  81. } else {
  82. $route = [$item];
  83. }
  84. if ($route) {
  85. foreach ($route as $one) {
  86. if (!isset($one['option']['_name'])) $one['option']['_name'] = $one['name'];
  87. $this->menu($one['option']['_path'] ?? '', $one['option'], $resp);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. return app()->make(MenuRepository::class)->commandMenu($type, $resp, $prompt);
  94. }
  95. /**
  96. * 菜单权限
  97. * @param $_path
  98. * @param $data
  99. * @param array $resp
  100. * @return array
  101. * @author Qinii
  102. * @day 3/18/22
  103. */
  104. protected function menu($_path, $data, &$resp = [], $isAppend = 0)
  105. {
  106. $check = true;
  107. if ($_path && is_array($data)) {
  108. $v = [
  109. 'route' => $data['_name'],
  110. 'menu_name' => $data['_alias'] ?? '权限',
  111. 'params' => $data['_params'] ?? '',
  112. ];
  113. if (!isset($data['_repeat']) || (isset($data['_repeat']) && !$data['_repeat'])){
  114. $check = $this->checkRepeat($v['route'], $v['menu_name']);
  115. $this->k[] = $v['route'];
  116. $this->kv[$v['route']] = $v['menu_name'];
  117. }
  118. if (!$check) {
  119. throw new Exception( "路由名重复 < " . $v['route']. ' >' . '「'. $v['menu_name']. ' 」');
  120. }
  121. if ($isAppend) {
  122. $_path = 'append_'.$_path;
  123. }
  124. $resp[$_path][$data['_name']] = $v;
  125. if (isset($data['_append']) && !empty($data['_append'])) {
  126. foreach ($data['_append'] as $datum) {
  127. $datum['_repeat'] = true;
  128. $this->menu($datum['_path'] ?? $data['_path'], $datum, $resp, 1);
  129. }
  130. }
  131. }
  132. return $resp;
  133. }
  134. protected function checkRepeat($key, $value)
  135. {
  136. if (in_array($key, $this->k)) {
  137. if ($value != $this->kv[$key]) {
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. }