RouteMap.php
5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace App\Models\RouteMap;
use App\Helper\Translate;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Base;
use App\Models\Project\Project;
use App\Models\Template\BTemplate;
/**
* 路由映射表
* Class RouteMap
* @package App\Http\Models
* @author zbj
* @date 2023/4/17
*/
class RouteMap extends Base
{
//设置关联表名
protected $table = 'gl_route_map';
//连接数据库
protected $connection = 'custom_mysql';
//路由类型
const SOURCE_PRODUCT = 'product';
const SOURCE_PRODUCT_CATE = 'product_category';
const SOURCE_PRODUCT_KEYWORD = 'product_keyword';
const SOURCE_PAGE = 'page'; //单页面
const SOURCE_INDEX = 'index'; //首页
const SOURCE_BLOG = 'blog';
const SOURCE_BLOG_CATE = 'blog_category';
const SOURCE_NEWS = 'news';
const SOURCE_NEWS_CATE = 'news_category';
//自定义模块
const SOURCE_MODULE = 'module';
const SOURCE_AI_BLOG = 'ai_blog';
const SOURCE_AI_VIDEO = 'ai_video';
const SOURCE_AI_BLOG_AUTHOR = 'ai_blog_author';//ai博客作者
const SOURCE_AI_BLOG_LIST = 'ai_blog_list';
//自定义模块分类
const SOURCE_MODULE_CATE = 'module_category';
//路由二级目录
const PATH_NEWS_CATE = 'news_catalog';
const PATH_BLOG_CATE = 'blog_catalog';
const PATH_MODULE_CATE = 'module_category_route';//扩展模块
const SOURCE_NAV = 'nav';
/**
* @remark :(新增与编辑)处理路由route
* @name :generateRoute
* @author :lyh
* @method :post
* @time :2025/3/12 9:30
* @param :title:路由 source:模块类型 source_id:对应数据id
* @notes :route_len:路由长度; suffix:路由后缀
*/
public static function generateRoute($title, $source, $source_id, $project_id){
//发现路由是.html结尾时,不做任何处理
$last5Chars = substr($title, -5);
if($last5Chars == '.html'){
return $title;
}
//处理其他情况(俄语+中文时翻译为英文)
if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $title) || contains_russian($title)){
$title = Translate::tran($title, 'en');
}
//过滤特殊字符
$sign = generateRoute($title);
//查看当前数据路由是否存在
$routeInfo = self::where(['project_id' => $project_id, 'source' => $source, 'source_id'=>$source_id])->first();
$suffix = '';//设置路由后缀
$route_len = 180;//默认设置字符为180
if(empty($routeInfo)){
if($source == self::SOURCE_PRODUCT){
$suffix = '-product';
}
$route_len = 65;
}
$length = strlen($sign);
if($length > $route_len){
$sign = trim(mb_substr($sign, 0, $route_len, 'UTF-8'),'-');
}
$i=1;//路由重复时拼接
$route = $sign.$suffix;
while(self::isExist($route, $source_id, $project_id,$source)){
$route = $sign .'-'.$i.$suffix;
$i++;
}
return $route;
}
/**
* @remark :验证路由是否重复
* @name :isExist
* @author :lyh
* @method :post
* @time :2025/3/12 9:51
* @param :route:路由 source:模块类型 source_id:对应数据id
*/
protected static function isExist($route, $source_id, $project_id,$source){
$fixed = ['api']; //固定的路由
if(in_array($route, $fixed)){
return true;
}
$where = [
'project_id' => $project_id, 'route' => $route,
];
$route = self::where($where)->first();
if($route){
if(($route->source_id != $source_id)){
return true;
}else{
if($source != $route->source){
//source_id想同,source不相同,继续循环
return true;
}
}
}
return false;
}
/**
* @remark :保存路由
* @name :setRoute
* @author :lyh
* @method :post
* @time :2025/3/12 9:54
* @param :title:路由 source:模块类型 source_id:对应数据id
*/
public static function setRoute($title, $source, $source_id, $project_id = 0){
$route = self::generateRoute($title, $source, $source_id, $project_id);
if(!$route){
return time().$project_id;
}
try {
$route_map = self::where(['project_id' => $project_id, 'source' => $source, 'source_id'=>$source_id])->first();
if(!$route_map){
$route_map = new self();
$route_map->source = $source;
$route_map->source_id = $source_id;
$route_map->project_id = $project_id;
}
$route_map->route = $route;
if($source == self::SOURCE_NEWS){
$route_map->path = self::SOURCE_NEWS;
}elseif ($source == self::SOURCE_BLOG){
$route_map->path = self::SOURCE_BLOG.'s';
}elseif($source == self::SOURCE_MODULE){
$route_map->path = self::PATH_MODULE_CATE;
}
$route_map->save();
}catch (\Exception $e){
throw new \Exception('路由映射失败');
}
return $route;
}
/**
* @param $source
* @param $source_id
* @param $project_id
* @return mixed
* @author zbj
* @date 2023/4/17
*/
public static function getRoute($source, $source_id, $project_id){
return self::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->value('route');
}
/**
* @param $source
* @param $source_id
* @param $project_id
* @return mixed
* @author zbj
* @date 2023/4/17
*/
public static function delRoute($source, $source_id, $project_id){
return self::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->delete();
}
}