Route.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. namespace app\controller\api;
  12. use think\facade\View;
  13. use think\facade\Cache;
  14. use think\event\RouteLoaded;
  15. use crmeb\basic\BaseController;
  16. class Route extends BaseController
  17. {
  18. public $type = ['admin','merchant','api','openapi','manager','pc','service'];
  19. public $type_name = [
  20. 'admin' => '平台后台',
  21. 'merchant' => '商户后台',
  22. 'api' => '用户端',
  23. 'openapi' => '开放接口',
  24. 'manager' => '员工端',
  25. 'pc' => 'PC端',
  26. 'service' => '客服端'
  27. ];
  28. public function list()
  29. {
  30. return app('json')->success('请开发者注释当前行代码,才可查看路由列表');
  31. $type = $this->request->param('type','');
  32. $type = $type == '' ? $this->type : [$type];
  33. foreach ($type as $route) {
  34. $data[$route] = $this->getRoute($route);
  35. }
  36. $table = '';
  37. foreach ($data as $t => $list) {
  38. foreach ($list as $item) {
  39. $table .= '<tr>';
  40. $table .= "<td> {$this->type_name[$t]} </td>";
  41. foreach ($item as $value) {
  42. $table .= "<td>" . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . "</td>";
  43. }
  44. $table .= '</tr>';
  45. }
  46. }
  47. $html = $this->getHtml($table);
  48. return view::display($html);
  49. }
  50. protected function getRoute($type)
  51. {
  52. $cache_key = $type.'_route_list';
  53. $res = Cache::get($cache_key);
  54. if ($res) return $res;
  55. $this->app->route->clear();
  56. $path = root_path(). 'route' . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR;
  57. $file = root_path(). 'route' . DIRECTORY_SEPARATOR . $type . '.php';
  58. if (is_file($file)) {
  59. include $file;
  60. }
  61. if (is_dir($path)) {
  62. $dir = scandir($path);
  63. foreach ($dir as $file) {
  64. if ($file == '.' || $file == '..') {
  65. continue;
  66. }
  67. include $path.$file;
  68. }
  69. }
  70. $this->app->event->trigger(RouteLoaded::class);
  71. $res = $this->routeList();
  72. Cache::set($cache_key, $res, 60);
  73. return $res;
  74. }
  75. protected function reloadRoute()
  76. {
  77. $this->app->route->clear();
  78. $path = root_path(). 'route' . DIRECTORY_SEPARATOR;
  79. $dir = scandir($path);
  80. foreach ($dir as $file) {
  81. if ($file == '.' || $file == '..') {
  82. continue;
  83. }
  84. if (is_file($path.$file)) {
  85. include $path.$file;
  86. }
  87. }
  88. $this->app->event->trigger(RouteLoaded::class);
  89. }
  90. protected function routeList()
  91. {
  92. $routeList = $this->app->route->getRuleList();
  93. $rows = [];
  94. foreach ($routeList as $item) {
  95. $item['route'] = $item['route'] instanceof \Closure ? '<Closure>' : $item['route'];
  96. $row = [
  97. $item['rule'],
  98. ($item['option']['prefix'] ?? '').$item['route'],
  99. $item['method'], $item['name'],
  100. $item['option']['_alias'] ?? ''
  101. ];
  102. $rows[] = $row;
  103. }
  104. $this->reloadRoute();
  105. return $rows;
  106. }
  107. protected function getHtml($content)
  108. {
  109. $html = <<<HTML
  110. <!doctype html>
  111. <html class="x-admin-sm">
  112. <head>
  113. <meta charset="UTF-8">
  114. <title>路由列表</title>
  115. <meta name="renderer" content="webkit|ie-comp|ie-stand">
  116. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  117. <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  118. <meta http-equiv="Cache-Control" content="no-siteapp" />
  119. </head>
  120. <body class="index">
  121. <table border="1" width="100%" height="200" align="left" cellpadding="10" cellspacing="0">
  122. <tr><th>所属端</th><th>路由</th><th>方法位置</th><th>请求方式</th><th>名称</th><th>方法注释</th></tr>
  123. $content
  124. </table>
  125. </body>
  126. </html>
  127. HTML;
  128. return $html;
  129. }
  130. }