作者 lyh

gx路由设置

@@ -30,13 +30,8 @@ if (!function_exists('generateRoute')) { @@ -30,13 +30,8 @@ if (!function_exists('generateRoute')) {
30 if(is_array($string)){ 30 if(is_array($string)){
31 $string = $string[0]; 31 $string = $string[0];
32 } 32 }
33 - $last5Chars = substr($string, -5);  
34 - if($last5Chars == '.html'){  
35 - return strtolower($string);  
36 - }else{  
37 - $sign = str_replace(".", "", trim(strtolower(preg_replace('/[^\w.]+/', '-', trim($string))), '-'));  
38 - return $sign;  
39 - } 33 + $sign = str_replace(".", "", trim(strtolower(preg_replace('/[^\w.]+/', '-', trim($string))), '-'));
  34 + return $sign;
40 } 35 }
41 } 36 }
42 37
@@ -46,45 +46,44 @@ class RouteMap extends Base @@ -46,45 +46,44 @@ class RouteMap extends Base
46 const PATH_MODULE_CATE = 'module_category_route';//扩展模块 46 const PATH_MODULE_CATE = 'module_category_route';//扩展模块
47 47
48 const SOURCE_NAV = 'nav'; 48 const SOURCE_NAV = 'nav';
  49 +
49 /** 50 /**
50 - * 生成路由标识  
51 - * @param $title  
52 - * @param $source  
53 - * @param $source_id  
54 - * @param $project_id  
55 - * @return string  
56 - * @author zbj  
57 - * @date 2023/4/17 51 + * @remark :(新增与编辑)处理路由route
  52 + * @name :generateRoute
  53 + * @author :lyh
  54 + * @method :post
  55 + * @time :2025/3/12 9:30
  56 + * @param :title:路由 source:模块类型 source_id:对应数据id
  57 + * @notes :route_len:路由长度; suffix:路由后缀
58 */ 58 */
59 public static function generateRoute($title, $source, $source_id, $project_id){ 59 public static function generateRoute($title, $source, $source_id, $project_id){
60 - if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $title)){  
61 - $title = Translate::tran($title, 'en'); 60 + //发现路由是.html结尾时,不做任何处理
  61 + $last5Chars = substr($title, -5);
  62 + if($last5Chars == '.html'){
  63 + return $title;
62 } 64 }
63 - if(contains_russian($title)){ 65 + //处理其他情况(俄语+中文时翻译为英文)
  66 + if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $title) || contains_russian($title)){
64 $title = Translate::tran($title, 'en'); 67 $title = Translate::tran($title, 'en');
65 } 68 }
66 - $i=1; 69 + //过滤特殊字符
67 $sign = generateRoute($title); 70 $sign = generateRoute($title);
  71 + //查看当前路由是否存在
68 $info = self::where(['project_id' => $project_id, 'source' => $source, 'source_id'=>$source_id])->first(); 72 $info = self::where(['project_id' => $project_id, 'source' => $source, 'source_id'=>$source_id])->first();
69 - $suffix = '';  
70 - $last5Chars = substr($sign, -5);  
71 - if($last5Chars == '.html'){  
72 - return $last5Chars;  
73 - }else{  
74 -  
75 - } 73 + $suffix = '';//设置路由后缀
  74 + $route_len = 180;//默认设置字符为180
  75 + //产品路由新增时,拼接-product
76 if(empty($info)){ 76 if(empty($info)){
77 - $len = 60;  
78 if($source == self::SOURCE_PRODUCT){ 77 if($source == self::SOURCE_PRODUCT){
79 $suffix = '-product'; 78 $suffix = '-product';
80 } 79 }
81 - }else{  
82 - $len = 180; 80 + $route_len = 65;
83 } 81 }
84 $length = strlen($sign); 82 $length = strlen($sign);
85 - if($length > $len){  
86 - $sign = trim(mb_substr($sign, 0, $len, 'UTF-8'),'-'); 83 + if($length > $route_len){
  84 + $sign = trim(mb_substr($sign, 0, $route_len, 'UTF-8'),'-');
87 } 85 }
  86 + $i=1;//路由重复时拼接
88 $route = $sign.$suffix; 87 $route = $sign.$suffix;
89 while(self::isExist($route, $source_id, $project_id)){ 88 while(self::isExist($route, $source_id, $project_id)){
90 $route = $sign .'-'.$i.$suffix; 89 $route = $sign .'-'.$i.$suffix;
@@ -94,14 +93,12 @@ class RouteMap extends Base @@ -94,14 +93,12 @@ class RouteMap extends Base
94 } 93 }
95 94
96 /** 95 /**
97 - * 路由是否存在  
98 - * @param $route  
99 - * @param $source  
100 - * @param $source_id  
101 - * @param $project_id  
102 - * @return bool  
103 - * @author zbj  
104 - * @date 2023/4/17 96 + * @remark :验证路由是否重复
  97 + * @name :isExist
  98 + * @author :lyh
  99 + * @method :post
  100 + * @time :2025/3/12 9:51
  101 + * @param :route:路由 source:模块类型 source_id:对应数据id
105 */ 102 */
106 protected static function isExist($route, $source_id, $project_id){ 103 protected static function isExist($route, $source_id, $project_id){
107 $fixed = ['api']; //固定的路由 104 $fixed = ['api']; //固定的路由
@@ -113,23 +110,20 @@ class RouteMap extends Base @@ -113,23 +110,20 @@ class RouteMap extends Base
113 ]; 110 ];
114 $route = self::where($where)->first(); 111 $route = self::where($where)->first();
115 if($route){ 112 if($route){
116 - if($route->source_id == $source_id){  
117 - return false; 113 + if($route->source_id != $source_id){
  114 + return true;
118 } 115 }
119 - return true;  
120 } 116 }
121 return false; 117 return false;
122 } 118 }
123 119
124 /** 120 /**
125 - * @param $title  
126 - * @param $source  
127 - * @param $source_id  
128 - * @param int $project_id  
129 - * @return bool  
130 - * @throws \Exception  
131 - * @author zbj  
132 - * @date 2023/4/17 121 + * @remark :保存路由
  122 + * @name :setRoute
  123 + * @author :lyh
  124 + * @method :post
  125 + * @time :2025/3/12 9:54
  126 + * @param :title:路由 source:模块类型 source_id:对应数据id
133 */ 127 */
134 public static function setRoute($title, $source, $source_id, $project_id = 0){ 128 public static function setRoute($title, $source, $source_id, $project_id = 0){
135 $route = self::generateRoute($title, $source, $source_id, $project_id); 129 $route = self::generateRoute($title, $source, $source_id, $project_id);
@@ -137,7 +131,7 @@ class RouteMap extends Base @@ -137,7 +131,7 @@ class RouteMap extends Base
137 throw new \Exception('路由生成失败'); 131 throw new \Exception('路由生成失败');
138 } 132 }
139 try { 133 try {
140 - $route_map = self::where('project_id', $project_id)->where('source_id', $source_id)->where('source', $source)->first(); 134 + $route_map = self::where(['project_id' => $project_id, 'source' => $source, 'source_id'=>$source_id])->first();
141 if(!$route_map){ 135 if(!$route_map){
142 $route_map = new self(); 136 $route_map = new self();
143 $route_map->source = $source; 137 $route_map->source = $source;
@@ -160,17 +154,6 @@ class RouteMap extends Base @@ -160,17 +154,6 @@ class RouteMap extends Base
160 } 154 }
161 155
162 /** 156 /**
163 - * @param $route  
164 - * @param $project_id  
165 - * @return mixed  
166 - * @author zbj  
167 - * @date 2023/4/17  
168 - */  
169 - public function getRouteInfo($route, $project_id){  
170 - return self::where('project_id',$project_id)->where('route', $route)->get();  
171 - }  
172 -  
173 - /**  
174 * @param $source 157 * @param $source
175 * @param $source_id 158 * @param $source_id
176 * @param $project_id 159 * @param $project_id
@@ -183,19 +166,6 @@ class RouteMap extends Base @@ -183,19 +166,6 @@ class RouteMap extends Base
183 } 166 }
184 167
185 /** 168 /**
186 - * @param $route  
187 - * @param $source  
188 - * @param int $project_id  
189 - * @return mixed  
190 - * @author zbj  
191 - * @date 2023/4/17  
192 - */  
193 - public static function getSourceId($route, $source, $project_id){  
194 - return self::where('project_id', $project_id)->where('source', $source)->where('route', $route)->value('source_id');  
195 - }  
196 -  
197 -  
198 - /**  
199 * @param $source 169 * @param $source
200 * @param $source_id 170 * @param $source_id
201 * @param $project_id 171 * @param $project_id