updateAuth.php 6.0 KB

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