updateMenu.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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~2020 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 updateMenu extends Command
  32. {
  33. protected function configure()
  34. {
  35. // 指令配置
  36. $this->setName('menu')
  37. ->setDescription('the menu command');
  38. }
  39. /**
  40. * @Author:Qinii
  41. * @Date: 2020/5/15
  42. * @param Input $input
  43. * @param Output $output
  44. * @return int|void|null
  45. */
  46. protected function execute(Input $input, Output $output)
  47. {
  48. $output->writeln('开始执行');
  49. $output->writeln('---------------->');
  50. $count = $this->routeList();
  51. $output->writeln('<----------------');
  52. $output->writeln('执行成功,合计'.$count.'条');
  53. }
  54. /**
  55. * @Author:Qinii
  56. * @Date: 2020/5/15
  57. * @param string|null $dir
  58. * @return mixed
  59. */
  60. public function routeList(string $dir = null)
  61. {
  62. $this->app->route->setTestMode(true);
  63. $this->app->route->clear();
  64. $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
  65. include $path . 'admin.php';
  66. include $path . 'merchant.php';
  67. // $files = is_dir($path) ? scandir($path) : [];
  68. // foreach ($files as $file) {
  69. // if (strpos($file, '.php')) {
  70. // include $path . $file;
  71. // }
  72. // }
  73. //触发路由载入完成事件
  74. $this->app->event->trigger(RouteLoaded::class);
  75. $routeList = $this->app->route->getRuleList();
  76. $rows = [];
  77. $name = [];
  78. foreach ($routeList as $k => $item) {
  79. if (!(strpos($item['name'], '/') !== false) && !(strpos($item['name'], '@') !== false) && is_string($item['route'])) {
  80. $rule = Route::getRule($item['rule']);
  81. $group = ($rule[$item['route']])->getParent();
  82. $groupRoute = $this->getGroupTree($group);
  83. $groupName = !empty($group->getName()) ? $group->getName() : 'route';
  84. if (in_array($item['name'], $name))
  85. throw new Exception( "< " . $item['name'] . ' >路由名重复');
  86. $name[] = $item['name'];
  87. $rows[$groupRoute][$groupName][] = $item['name'];
  88. }
  89. }
  90. return app()->make(MenuRepository::class)->commandCreate($rows);
  91. }
  92. /**
  93. * @Author:Qinii
  94. * @Date: 2020/5/15
  95. * @param $group
  96. * @return mixed
  97. */
  98. protected function getGroupTree($group)
  99. {
  100. $name = $group->getName();
  101. if (in_array($name, ['sys','mer'])) {
  102. return $name;
  103. } else {
  104. return $this->getGroupTree($group->getParent());
  105. }
  106. }
  107. }