RouteModel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 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 huo\model;
  12. use think\Model;
  13. class RouteModel extends Model
  14. {
  15. /**
  16. * 获取所有url美化规则
  17. * @param boolean $refresh 是否强制刷新
  18. * @return mixed|void|boolean|NULL|unknown[]|unknown
  19. */
  20. public function getRoutes($refresh = false)
  21. {
  22. $routes = cache("routes");
  23. $appUrls = $this->getAppUrls();
  24. if ((!empty($routes) || is_array($routes)) && !$refresh) {
  25. return $routes;
  26. }
  27. $routes = $this->where("status", 1)->order("list_order asc")->select();
  28. $allRoutes = [];
  29. $cacheRoutes = [];
  30. foreach ($routes as $er) {
  31. $fullUrl = htmlspecialchars_decode($er['full_url']);
  32. // 解析URL
  33. $info = parse_url($fullUrl);
  34. $path = explode("/", $info['path']);
  35. if (count($path) != 3) {//必须是完整 url
  36. continue;
  37. }
  38. $module = strtolower($path[0]);
  39. // 解析参数
  40. $vars = [];
  41. if (isset($info['query'])) { // 解析地址里面参数 合并到vars
  42. parse_str($info['query'], $params);
  43. $vars = array_merge($params, $vars);
  44. }
  45. $vars_src = $vars;
  46. ksort($vars);
  47. $path = $info['path'];
  48. $fullUrl = $path . (empty($vars) ? "" : "?") . http_build_query($vars);
  49. $url = htmlspecialchars_decode($er['url']);
  50. if (isset($cacheRoutes[$path])) {
  51. array_push($cacheRoutes[$path], ['vars' => $vars]);
  52. } else {
  53. $cacheRoutes[$path] = [];
  54. array_push($cacheRoutes[$path], ['vars' => $vars]);
  55. }
  56. //$cacheRoutes[$fullUrl] = true;
  57. // if (strpos($url, ':') === false) {
  58. // $cacheRoutes['static'][$fullUrl] = $url;
  59. // } else {
  60. // $cacheRoutes['dynamic'][$path][] = ["query" => $vars, "url" => $url];
  61. // }
  62. if (empty($appUrls[$path]['pattern'])) {
  63. $allRoutes[$url] = $fullUrl;
  64. } else {
  65. $allRoutes[$url] = [$fullUrl, [], $appUrls[$path]['pattern']];
  66. }
  67. }
  68. cache("routes", $cacheRoutes);
  69. $route_dir = CMF_ROOT . "data/conf/";
  70. if (!file_exists($route_dir)) {
  71. mkdir($route_dir);
  72. }
  73. $route_file = $route_dir . "route.php";
  74. file_put_contents($route_file, "<?php\treturn " . stripslashes(var_export($allRoutes, true)) . ";");
  75. return $cacheRoutes;
  76. }
  77. public function getAppUrls()
  78. {
  79. $apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  80. $appUrls = [];
  81. foreach ($apps as $app) {
  82. $urlConfigFile = APP_PATH . $app . '/url.php';
  83. if (file_exists($urlConfigFile)) {
  84. $urls = include $urlConfigFile;
  85. foreach ($urls as $action => $url) {
  86. $action = $app . '/' . $action;
  87. $appUrls[$action] = $url;
  88. if (!empty($url['vars'])) {
  89. foreach ($url['vars'] as $urlVarName => $urlVar) {
  90. $appUrls[$action]['pattern'][$urlVarName] = $urlVar['pattern'];
  91. }
  92. }
  93. }
  94. }
  95. }
  96. return $appUrls;
  97. }
  98. public function getUrl($action, $vars)
  99. {
  100. $fullUrl = $this->buildFullUrl($action, $vars);
  101. $url = $this->where('full_url', $fullUrl)->value('url');
  102. return empty($url) ? '' : $url;
  103. }
  104. public function getFullUrlByUrl($url)
  105. {
  106. $full_url = $this->where('url', $url)->value('full_url');
  107. return empty($full_url) ? '' : $full_url;
  108. }
  109. public function buildFullUrl($action, $vars)
  110. {
  111. // 解析参数
  112. if (is_string($vars)) {
  113. // aaa=1&bbb=2 转换成数组
  114. parse_str($vars, $vars);
  115. }
  116. if (!empty($vars)) {
  117. ksort($vars);
  118. $fullUrl = $action . '?' . http_build_query($vars);
  119. } else {
  120. $fullUrl = $action;
  121. }
  122. return $fullUrl;
  123. }
  124. public function exists($url, $fullUrl)
  125. {
  126. $findRouteCount = $this->where(['url' => $url, 'full_url' => ['neq', $fullUrl]])->count();
  127. return $findRouteCount > 0 ? true : false;
  128. }
  129. public function setRoute($url, $action, $vars, $type = 2, $listOrder = 10000)
  130. {
  131. $fullUrl = $this->buildFullUrl($action, $vars);
  132. $findRoute = $this->where(['full_url' => $fullUrl])->find();
  133. if ($findRoute) {
  134. if (empty($url)) {
  135. $this->where(['id' => $findRoute['id']])->delete();
  136. } else {
  137. $this->where(['id' => $findRoute['id']])->update(['url' => $url, 'list_order' => $listOrder, 'type' => $type]);
  138. }
  139. } else {
  140. if (!empty($url)) {
  141. $this->insert(['full_url' => $fullUrl, 'url' => $url, 'list_order' => $listOrder, 'type' => $type]);
  142. }
  143. }
  144. }
  145. public function deleteRoute($action, $vars)
  146. {
  147. $fullUrl = $this->buildFullUrl($action, $vars);
  148. $this->where(['full_url' => $fullUrl])->delete();
  149. return true;
  150. }
  151. }