RouteModel.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model;
  12. use think\Model;
  13. class RouteModel extends Model
  14. {
  15. /**
  16. * 获取所有url美化规则
  17. * @param boolean $refresh 是否强制刷新
  18. * @return array|mixed|string|\think\Collection
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function getRoutes($refresh = false)
  24. {
  25. $routes = cache("routes");
  26. $appUrls = $this->getAppUrls();
  27. if ((!empty($routes) || is_array($routes)) && !$refresh) {
  28. return $routes;
  29. }
  30. $routes = $this->where("status", 1)->order("list_order asc")->select();
  31. $allRoutes = [];
  32. $cacheRoutes = [];
  33. foreach ($routes as $er) {
  34. $fullUrl = htmlspecialchars_decode($er['full_url']);
  35. // 解析URL
  36. $info = parse_url($fullUrl);
  37. $vars = [];
  38. // 解析参数
  39. if (isset($info['query'])) { // 解析地址里面参数 合并到vars
  40. parse_str($info['query'], $vars);
  41. ksort($vars);
  42. }
  43. if (isset($info['scheme'])) { //插件
  44. $plugin = cmf_parse_name($info['scheme']);
  45. $controller = cmf_parse_name($info['host']);
  46. $action = trim(strtolower($info['path']), '/');
  47. $pluginParams = [
  48. '_plugin' => $plugin,
  49. '_controller' => $controller,
  50. '_action' => $action,
  51. ];
  52. $path = '\\cmf\\controller\\PluginController@index?' . http_build_query($pluginParams);
  53. $fullUrl = $path . (empty($vars) ? '' : '&') . http_build_query($vars);
  54. } else { // 应用
  55. $path = explode("/", $info['path']);
  56. if (count($path) != 3) {//必须是完整 url
  57. continue;
  58. }
  59. $path = $info['path'];
  60. $fullUrl = $path . (empty($vars) ? "" : "?") . http_build_query($vars);
  61. }
  62. $url = htmlspecialchars_decode($er['url']);
  63. if (isset($cacheRoutes[$path])) {
  64. array_push($cacheRoutes[$path], ['vars' => $vars]);
  65. } else {
  66. $cacheRoutes[$path] = [];
  67. array_push($cacheRoutes[$path], ['vars' => $vars]);
  68. }
  69. //$cacheRoutes[$fullUrl] = true;
  70. // if (strpos($url, ':') === false) {
  71. // $cacheRoutes['static'][$fullUrl] = $url;
  72. // } else {
  73. // $cacheRoutes['dynamic'][$path][] = ["query" => $vars, "url" => $url];
  74. // }
  75. if (empty($appUrls[$path]['pattern'])) {
  76. $allRoutes[$url] = $fullUrl;
  77. } else {
  78. $allRoutes[$url] = [$fullUrl, [], $appUrls[$path]['pattern']];
  79. }
  80. }
  81. cache("routes", $cacheRoutes);
  82. if (strpos(cmf_version(), '5.0.') === false) {
  83. $routeDir = CMF_DATA . "route/"; // 5.1
  84. } else {
  85. $routeDir = CMF_DATA . "conf/"; // 5.0
  86. }
  87. if (!file_exists($routeDir)) {
  88. mkdir($routeDir);
  89. }
  90. $route_file = $routeDir . "route.php";
  91. file_put_contents($route_file, "<?php\treturn " . var_export($allRoutes, true) . ";");
  92. return $cacheRoutes;
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function getAppUrls()
  98. {
  99. $apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  100. array_push($apps, 'admin', 'user');
  101. $appUrls = [];
  102. foreach ($apps as $app) {
  103. $urlConfigFile = cmf_get_app_config_file($app, 'url');
  104. if (file_exists($urlConfigFile)) {
  105. $urls = include $urlConfigFile;
  106. foreach ($urls as $action => $url) {
  107. $action = $app . '/' . $action;
  108. $appUrls[$action] = $url;
  109. if (!empty($url['vars'])) {
  110. foreach ($url['vars'] as $urlVarName => $urlVar) {
  111. $appUrls[$action]['pattern'][$urlVarName] = $urlVar['pattern'];
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return $appUrls;
  118. }
  119. public function getUrl($action, $vars)
  120. {
  121. $fullUrl = $this->buildFullUrl($action, $vars);
  122. $url = $this->where('full_url', $fullUrl)->value('url');
  123. return empty($url) ? '' : $url;
  124. }
  125. public function getFullUrlByUrl($url)
  126. {
  127. $full_url = $this->where('url', $url)->value('full_url');
  128. return empty($full_url) ? '' : $full_url;
  129. }
  130. public function buildFullUrl($action, $vars)
  131. {
  132. // 解析参数
  133. if (is_string($vars)) {
  134. // aaa=1&bbb=2 转换成数组
  135. parse_str($vars, $vars);
  136. }
  137. if (!empty($vars)) {
  138. ksort($vars);
  139. $fullUrl = $action . '?' . http_build_query($vars);
  140. } else {
  141. $fullUrl = $action;
  142. }
  143. return $fullUrl;
  144. }
  145. public function existsRoute($url, $fullUrl)
  146. {
  147. $findRouteCount = $this->where('url', $url)->where('full_url', 'neq', $fullUrl)->count();
  148. return $findRouteCount > 0 ? true : false;
  149. }
  150. public function setRoute($url, $action, $vars, $type = 2, $listOrder = 10000)
  151. {
  152. $fullUrl = $this->buildFullUrl($action, $vars);
  153. $findRoute = $this->where('full_url', $fullUrl)->find();
  154. if (preg_match("/[()'\";]/", $url)) {
  155. return false;
  156. }
  157. if ($findRoute) {
  158. if (empty($url)) {
  159. $this->where('id', $findRoute['id'])->delete();
  160. } else {
  161. $this->where('id', $findRoute['id'])->update([
  162. 'url' => $url,
  163. 'list_order' => $listOrder,
  164. 'type' => $type
  165. ]);
  166. }
  167. } else {
  168. if (!empty($url)) {
  169. $this->insert([
  170. 'full_url' => $fullUrl,
  171. 'url' => $url,
  172. 'list_order' => $listOrder,
  173. 'type' => $type
  174. ]);
  175. }
  176. }
  177. }
  178. /**
  179. * @param $action
  180. * @param $vars
  181. * @return bool
  182. * @throws \Exception
  183. */
  184. public function deleteRoute($action, $vars)
  185. {
  186. $fullUrl = $this->buildFullUrl($action, $vars);
  187. $this->where('full_url', $fullUrl)->delete();
  188. return true;
  189. }
  190. }