SystemRouteServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\services\system;
  14. use app\dao\system\SystemRouteDao;
  15. use app\services\BaseServices;
  16. use Darabonba\GatewaySpi\Models\InterceptorContext\response;
  17. use think\facade\Route;
  18. use think\helper\Str;
  19. /**
  20. * Class SystemRouteServices
  21. * @author 等风来
  22. * @email 136327134@qq.com
  23. * @date 2023/4/6
  24. * @package app\services\system
  25. */
  26. class SystemRouteServices extends BaseServices
  27. {
  28. /**
  29. * SystemRouteServices constructor.
  30. * @param SystemRouteDao $dao
  31. */
  32. public function __construct(SystemRouteDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取某个应用下的所有路由权限
  38. * @param string $app
  39. * @return array
  40. * @author 等风来
  41. * @email 136327134@qq.com
  42. * @date 2023/4/6
  43. */
  44. public function getRouteListAll(string $app = 'adminapi')
  45. {
  46. //获取所有的路由
  47. $this->app = app();
  48. $this->app->route->setTestMode(true);
  49. $this->app->route->clear();
  50. $path = $this->app->getRootPath() . 'app' . DS . $app . DS . 'route' . DS;
  51. $files = is_dir($path) ? scandir($path) : [];
  52. foreach ($files as $file) {
  53. if (strpos($file, '.php')) {
  54. include $path . $file;
  55. }
  56. }
  57. return $this->app->route->getRuleList();
  58. }
  59. /**
  60. * 同步路由
  61. * @author 等风来
  62. * @email 136327134@qq.com
  63. * @date 2023/4/6
  64. */
  65. public function syncRoute(string $app = 'adminapi')
  66. {
  67. $listAll = $this->getRouteListAll($app);
  68. //保持新增的权限路由
  69. $data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray();
  70. $save = [];
  71. foreach ($listAll as $item) {
  72. if (!$this->diffRoute($data, $item['rule'], $item['method']) && strstr($item['rule'], '<MISS>') === false) {
  73. $save[] = [
  74. 'name' => $item['option']['real_name'] ?? $item['name'],
  75. 'path' => $item['rule'],
  76. 'app_name' => $app,
  77. 'type' => isset($item['option']['is_common']) && $item['option']['is_common'] ? 1 : 0,
  78. 'method' => $item['method'],
  79. 'add_time' => date('Y-m-d H:i:s'),
  80. ];
  81. }
  82. }
  83. if ($save) {
  84. $this->dao->saveAll($save);
  85. }
  86. //删除不存在的权限路由
  87. $data = $this->dao->selectList(['app_name' => $app], 'path,method,id')->toArray();
  88. $delete = [];
  89. foreach ($data as $item) {
  90. if (!$this->diffRoute($listAll, $item['path'], $item['method'], 'rule') && $item['path'] !== '<MISS>') {
  91. $delete[] = $item['id'];
  92. }
  93. }
  94. if ($delete) {
  95. $this->dao->delete([['id', 'in', $delete]]);
  96. }
  97. }
  98. /**
  99. * 对比路由
  100. * @param array $data
  101. * @param string $path
  102. * @param string $method
  103. * @return bool
  104. * @author 等风来
  105. * @email 136327134@qq.com
  106. * @date 2023/4/6
  107. */
  108. protected function diffRoute(array $data, string $path, string $method, string $key = 'path')
  109. {
  110. $res = false;
  111. foreach ($data as $item) {
  112. if (strtolower($item['method']) == strtolower($method) && strtolower($item[$key]) == strtolower($path)) {
  113. $res = true;
  114. break;
  115. } else if ($method === '*' && strtolower($item[$key]) == strtolower($path)) {
  116. $res = true;
  117. break;
  118. }
  119. }
  120. return $res;
  121. }
  122. }