TdkService.php
17.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
namespace App\Services\Html;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleExtentContent;
use App\Models\Module\Module;
use App\Models\Module\ModuleCategory;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\Keyword;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BCustomTemplate;
use App\Models\WebSetting\WebCustom;
use App\Models\WebSetting\WebSetting;
use App\Models\WebSetting\WebSettingSeo;
use App\Models\WebSetting\WebTemplateCommon;
use phpQuery;
/**
* C端处理TDK服务
*/
class TdkService{
/**
* 页面TDK处理
*/
public function pageTdkHandle($project,$html,$type,$routerMap=null): string
{
$titleContent = "";
$descriptionContent = "";
$keywordsContent = "";
$phpQueryDom=phpQuery::newDocument($html);
//首页TDK设置
if ($type == RouteMap::SOURCE_INDEX){
$tdkInfo = $this->indexTDK($project);
}
//单页面TDK设置
if ($type == RouteMap::SOURCE_PAGE){
$tdkInfo = $this->pageTDK($project,$routerMap);
}
//自定义模块列表页TDK设置
if ($type == RouteMap::SOURCE_MODULE_CATE){
$tdkInfo = $this->moduleCategoryTDK($project,$routerMap);
}
//自定义模块详情页TDK设置
if ($type == RouteMap::SOURCE_MODULE){
$tdkInfo = $this->moduleDetailsTDK($project,$routerMap);
}
//新闻详情TDK设置
if ($type == RouteMap::SOURCE_NEWS){
$tdkInfo = $this->newsDetailsTDK($project,$routerMap);
}
//博客详情TDK设置
if ($type == RouteMap::SOURCE_BLOG){
$tdkInfo = $this->blogDetailsTDK($project,$routerMap);
}
//聚合页TDK设置
if ($type == RouteMap::SOURCE_PRODUCT_KEYWORD){
$tdkInfo = $this->productKeywordsTDK($project,$routerMap);
}
//新闻列表页TDK设置
if ($type == RouteMap::SOURCE_NEWS_CATE){
$tdkInfo = $this->newsCategoryTDK($project,$routerMap);
}
//博客列表页TDK设置
if ($type == RouteMap::SOURCE_BLOG_CATE){
$tdkInfo = $this->blogCategoryTDK($project,$routerMap);
}
//产品列表页TDK设置
if ($type == RouteMap::SOURCE_PRODUCT_CATE){
$tdkInfo = $this->productCategoryTDK($project,$routerMap);
}
if (!empty($tdkInfo)){
$titleContent = strip_tags($tdkInfo["titleContent"]);
$descriptionContent = strip_tags($tdkInfo["descriptionContent"]);
$keywordsContent = strip_tags($tdkInfo["keywordsContent"]);
}
$phpQueryDom->find('title')->text($titleContent);
$phpQueryDom->find('meta[name="description"]')->attr('content', $descriptionContent);
$phpQueryDom->find('meta[name="keywords"]')->attr('content', $keywordsContent);
$phpQueryDom->find('head')->append("<meta property='og:title' content='".$titleContent."'/>");
$phpQueryDom->find('head')->append("<meta property='og:type' content='site'/>");
$phpQueryDom->find('head')->append("<meta property='og:site_name' content='".$titleContent."'/>");
$content = $phpQueryDom->htmlOuter();
unset($html,$tdkInfo,$phpQueryDom);
phpQuery::unloadDocuments();
return $content;
}
/**
* 首页TDK
*/
public function indexTDK($project): array
{
$tdkInfo = [];
$webSetting = WebSetting::getWebSetting($project);
if (!empty($webSetting)){
$titleContent = strip_tags($webSetting->title);
$descriptionContent = strip_tags($webSetting->remark);
$keywordsContent = strip_tags($webSetting->keyword);
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
return $tdkInfo;
}
/**
* 新闻列表页TDK
*/
public function newsCategoryTDK($project,$routerMap): array
{
$tdkInfo = [];
$pageSuffix = "";
$webSetting = WebSetting::getWebSetting($project);
$newsCategory = NewsCategory::where("project_id",$project->id)->where("id",$routerMap->source_id)->first();
//seo拼接
$webSeo = WebSettingSeo::where("project_id",$project->id)->first();
if (!empty($webSeo)){
$pageSuffix = !empty($webSeo->single_page_suffix) ? " ".$webSeo->single_page_suffix : "";
}
if (!empty($newsCategory)){
//title
if (!empty($newsCategory->seo_title)){
$titleContent = $newsCategory->seo_title.$pageSuffix;
}else{
$titleContent = $newsCategory->name.$pageSuffix;
}
//description
if (!empty($newsCategory->seo_des)){
$descriptionContent = $newsCategory->seo_des;
}else{
$descriptionContent = !empty($webSetting->remark) ? $webSetting->remark : "";
}
//keyword
if (!empty($newsCategory->seo_keywords)){
$keywordsContent = $newsCategory->seo_keywords;
}else{
$keywordsContent = !empty($webSetting->keyword) ? $webSetting->keyword : "";
}
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
return $tdkInfo;
}
/**
* 博客列表页TDK
*/
public function blogCategoryTDK($project,$routerMap): array
{
$tdkInfo = [];
$pageSuffix = "";
$webSetting = WebSetting::getWebSetting($project);
$newsCategory = BlogCategory::where("project_id",$project->id)->where("id",$routerMap->source_id)->first();
//seo拼接
$webSeo = WebSettingSeo::where("project_id",$project->id)->first();
if (!empty($webSeo)){
$pageSuffix = !empty($webSeo->single_page_suffix) ? " ".$webSeo->single_page_suffix : "";
}
if (!empty($newsCategory)){
//title
if (!empty($newsCategory->seo_title)){
$titleContent = $newsCategory->seo_title.$pageSuffix;
}else{
$titleContent = $newsCategory->name.$pageSuffix;
}
//description
if (!empty($newsCategory->seo_des)){
$descriptionContent = $newsCategory->seo_des;
}else{
$descriptionContent = !empty($webSetting->remark) ? $webSetting->remark : "";
}
//keyword
if (!empty($newsCategory->seo_keywords)){
$keywordsContent = $newsCategory->seo_keywords;
}else{
$keywordsContent = !empty($webSetting->keyword) ? $webSetting->keyword : "";
}
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
return $tdkInfo;
}
/**
* 产品列表页TDK
*/
public function productCategoryTDK($project,$routerMap): array
{
$tdkInfo = [];
$prefix = "";
$suffix = "";
$pageSuffix = "";
$webSettingTitle = "";
$webSettingDescription = "";
$webSettingKeyword = "";
$productCategory = Category::where("project_id",$project->id)->where("id",$routerMap->source_id)->first();
$webSetting = WebSetting::getWebSetting($project);
//seo拼接
$webSeo = WebSettingSeo::where("project_id",$project->id)->first();
if (!empty($webSeo)){
$prefix = !empty($webSeo->product_cate_prefix) ? $webSeo->product_cate_prefix." " : "";
$suffix = !empty($webSeo->product_cate_suffix) ? " ".$webSeo->product_cate_suffix : "";
$pageSuffix = !empty($webSeo->single_page_suffix) ? " ".$webSeo->single_page_suffix : "";
}
if (!empty($webSetting)){
$webSettingTitle = !empty($webSetting->title) ? $webSetting->title : "";
$webSettingDescription = !empty($webSetting->remark) ? $webSetting->remark : "";
$webSettingKeyword = !empty($webSetting->keyword) ? $webSetting->keyword : "";
}
if (!empty($productCategory)){
//title
if (!empty($productCategory->seo_title)){
$titleContent = $prefix.$productCategory->seo_title.$suffix;
}else{
$titleContent = $prefix.$webSettingTitle.$suffix;
}
//description
if (!empty($productCategory->seo_des)){
$descriptionContent = $productCategory->seo_des;
}else{
$descriptionContent = $webSettingDescription;
}
//keywords
if (!empty($productCategory->seo_keywords)){
$keywordsContent = $productCategory->seo_keywords;
}else{
$keywordsContent = $webSettingKeyword;
}
}else{
$titleContent = $prefix.$webSettingTitle.$suffix;
$descriptionContent = $webSettingDescription;
$keywordsContent = $webSettingKeyword;
}
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
return $tdkInfo;
}
/**
* 产品聚合页TDK
*/
public function productKeywordsTDK($project,$routerMap): array
{
$tdkInfo = [];
$keyword = Keyword::where("project_id",$project->id)->where("status",1)->where("id",$routerMap->source_id)->first();
if (!empty($keyword)){
//处理title
$title = $keyword->seo_title ?: $keyword->title;
//seo拼接
$webSeo = WebSettingSeo::where("project_id",$project->id)->first();
$titleContent = $webSeo ? $webSeo->tab_prefix . " " . $title . " " . $webSeo->tab_suffix : $title;
//处理description
$descriptionContent = $keyword->seo_description ?: $keyword->title;
//处理keywords
$keywordsContent = $keyword->seo_keywords ?: $keyword->title;
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
return $tdkInfo;
}
/**
* 新闻详情页TDK
*/
public function newsDetailsTDK($project,$routerMap): array
{
$tdkInfo = [];
$titleContent = "";
$keywordsContent = "";
$descriptionContent = "";
$data = News::where("project_id",$project->id)->where("id",$routerMap->source_id)->where("status",1)->first();
return $this->newsBlogTdk($data, $project, $titleContent, $descriptionContent, $keywordsContent, $tdkInfo);
}
/**
* 新闻详情页TDK
*/
public function blogDetailsTDK($project,$routerMap): array
{
$tdkInfo = [];
$titleContent = "";
$keywordsContent = "";
$descriptionContent = "";
$data = Blog::where("project_id",$project->id)->where("id",$routerMap->source_id)->where("status",1)->first();
return $this->newsBlogTdk($data, $project, $titleContent, $descriptionContent, $keywordsContent, $tdkInfo);
}
/**
* 自定义模块列表页TDK
*/
public function moduleDetailsTDK($project,$routerMap): array
{
$tdkInfo = [];
$moduleInfo = CustomModuleExtentContent::where("project_id",$project->id)->where("status",0)->where("id",$routerMap->source_id)->first();
$webSetting = WebSetting::getWebSetting($project);
if (!empty($moduleInfo)){
//title
$webSettingTitle = !empty($webSetting->title) ? $webSetting->title : "";
$titleContent = !empty($moduleInfo->seo_title) ? $moduleInfo->seo_title : $webSettingTitle;
//description
$webSettingDescription = !empty($webSetting->remark) ? $webSetting->remark : "";
$descriptionContent = !empty($moduleInfo->seo_description) ? $moduleInfo->seo_description : $webSettingDescription;
//keywords
$webSettingKeyword = !empty($webSetting->keyword) ? $webSetting->keyword : "";
$keywordsContent = !empty($moduleInfo->seo_keywords) ? $moduleInfo->seo_keywords : $webSettingKeyword;
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
return $tdkInfo;
}
/**
* 自定义模块列表页TDK
*/
public function moduleCategoryTDK($project,$routerMap): array
{
$tdkInfo = [];
$moduleCategoryInfo = CustomModuleCategory::getModuleCategoryAndExtendByRoute($project->id,$routerMap->route);
$webSetting = WebSetting::getWebSetting($project);
if (!empty($moduleCategoryInfo)){
//title
$webSettingTitle = !empty($webSetting->title) ? $webSetting->title : "";
$titleContent = !empty($moduleCategoryInfo->seo_title) ? $moduleCategoryInfo->seo_title : $webSettingTitle;
//description
$webSettingDescription = !empty($webSetting->remark) ? $webSetting->remark : "";
$descriptionContent = !empty($moduleCategoryInfo->seo_description) ? $moduleCategoryInfo->seo_description : $webSettingDescription;
//keywords
$webSettingKeyword = !empty($webSetting->keyword) ? $webSetting->keyword : "";
$keywordsContent = !empty($moduleCategoryInfo->seo_keywords) ? $moduleCategoryInfo->seo_keywords : $webSettingKeyword;
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
return $tdkInfo;
}
/**
* 单页面TDK
*/
public function pageTDK($project,$routerMap): array
{
$tdkInfo = [];
if (!empty($routerMap)){
$webCustom = BCustomTemplate::where("id",$routerMap->source_id)->where("status",1)->first();
//seo拼接
$webSeo = WebSettingSeo::where("project_id",$project->id)->first();
//网站设置
$webSetting = WebSetting::getWebSetting($project);
$titleContent = "";
$descriptionContent = "";
$keywordsContent = "";
if (!empty($webCustom)){
//title
if ($webCustom->title == null){
if (!empty($webSeo)){
$titleContent = $webCustom->name." ".$webSeo->single_page_suffix;
}else{
$titleContent = $webCustom->name;
}
}else{
if (!empty($webSeo)){
$titleContent = $webCustom->title." ".$webSeo->single_page_suffix;
}else{
$titleContent = $webCustom->title;
}
}
//keywords
if ($webCustom->keywords == null){
if (!empty($webSetting)){
$keywordsContent = $webSetting->keyword;
}
}else{
$keywordsContent = $webCustom->keywords;
}
//description
if ($webCustom->description == null){
if (!empty($webSetting)) {
$descriptionContent = $webSetting->remark;
}
}else{
$descriptionContent = $webCustom->description;
}
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
}
return $tdkInfo;
}
/**
* 新闻博客详情通用版块
*/
public function newsBlogTdk($data, $project, string $titleContent, $descriptionContent, $keywordsContent, array $tdkInfo): array
{
if (!empty($data)) {
//处理title
//seo拼接
$pageSuffix = "";
$webSeo = WebSettingSeo::where("project_id", $project->id)->first();
$webSetting = WebSetting::getWebSetting($project);
if (!empty($webSeo)) {
$pageSuffix = !empty($webSeo->single_page_suffix) ? " " . $webSeo->single_page_suffix : "";
}
if ($data->seo_title == null) {
$titleContent = !empty($data->name) ? $data->name . $pageSuffix : "";
} else {
$titleContent = $data->seo_title . $pageSuffix;
}
//处理description
if ($data->seo_description == null) {
$descriptionContent = !empty($webSetting->remark) ? $webSetting->remark : "";
} else {
$descriptionContent = $data->seo_description;
}
//处理keywords
if ($data->seo_keywords == null) {
$keywordsContent = !empty($webSetting->keyword) ? $webSetting->keyword : "";
} else {
$keywordsContent = $data->seo_keywords;
}
$tdkInfo["titleContent"] = $titleContent;
$tdkInfo["descriptionContent"] = $descriptionContent;
$tdkInfo["keywordsContent"] = $keywordsContent;
}
return $tdkInfo;
}
}