CommonService.php
10.4 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
<?php
namespace App\Services\Html;
use App\Console\Commands\ProjectService;
use App\Helper\Translate;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\Project\DomainInfo;
use App\Models\Project\Project;
use App\Models\Project\UpdateProgressModel;
use App\Models\RouteMap;
use App\Models\WebSetting\WebLanguage;
use App\Models\WebSetting\WebProofreading;
use App\Models\WebSetting\WebSetting;
use App\Models\WebSetting\WebSettingCountry;
use App\Models\WebSetting\WebSettingHtml;
use App\Models\WebSetting\WebSettingText;
use App\Models\WebSetting\WebTemplateCommon;
use Illuminate\Support\Facades\Redis;
use phpQuery;
/**
* 生成静态页相关
*/
class CommonService
{
/**
* 翻译
*/
public function webHtmlTranslate($project,$content, $lang,$domain)
{
//翻译过滤器
$lang = $lang == "zh-ct" ? "zh-TW" : $lang;
$tlsList = Translate::$tls_list;
if (isset($tlsList[$lang])){
$main_lang = $project->main_lang_id;
$mainLangInfo = WebLanguage::getProjectMainLang($main_lang);
$master_lang = $mainLangInfo ? $mainLangInfo->short : 'en';
//翻译
if ($master_lang != 'en') {
$result = Translate::translate($content, $lang, $master_lang);
$content = $result[0]['texts'] ?? '';
} else {
$content = Translate::tran($content, $lang);
}
// 翻译有时会返回空数据 再翻译一次 FIXME 后续需要重新封装
if (empty($content)) {
$result = Translate::translate($content, $lang, $master_lang);
$content = $result[0]['texts'] ?? '';
}
//翻译校对
$content = $this->translationProofread($project,$content,$lang,$domain);
}
return $content;
}
/**
* 翻译校对
*/
public function translationProofread($project, $content, $lang,$domain)
{
$webProofreading = WebProofreading::where("project_id",$project->id)->where("alias",$lang)->get();
if (!empty($webProofreading)){
foreach ($webProofreading as $item){
$content = str_ireplace($item->text,$item->translate,$content);
}
}
return $content;
}
/**
* 处理第三方代码
*/
public function thirdPartyCodeHandle($project,$content,$route,$routeMap=null): string
{
if (Redis::get("project_".$project->id."_third_party_code") != null){
$webSettingHtml = json_decode(Redis::get("project_".$project->id."_third_party_code"));
}else{
$webSettingHtml = WebSettingHtml::where("project_id",$project->id)->first();
Redis::set("project_".$project->id."_third_party_code", json_encode($webSettingHtml));
Redis::expire("project_".$project->id."_third_party_code", WebSetting::$redisExpireTime);
}
$phpQueryDom = phpQuery::newDocument($content);
//处理自定义页面样式问题
// if ($globalsojsSyles != ""){
// $phpQueryDom->find("#globalsojs-styles")->remove();
// $phpQueryDom->find('head')->append($globalsojsSyles);
// }
//网站icon
$webIcon = WebSetting::where("project_id",$project->id)->first();
if (!empty($webIcon)){
$iconDom = $phpQueryDom->find("link[rel=icon]");
if (count($iconDom) >= 1){
if (isset($webIcon->web_site_icon) && !empty($webIcon->web_site_icon) && $webIcon->web_site_icon != "" && $webIcon->web_site_icon != 0){
$iconDom->attr("href",$webIcon->web_site_icon);
}else{
$iconDom->attr("href","");
}
}
}
//第三方代码(网页顶部,中间,底部),以后启用位置
if (!empty($webSettingHtml)) {
$phpQueryDom->find('head')->append($webSettingHtml->head_html?:'');
$phpQueryDom->find('body')->append($webSettingHtml->body_html?:'');
$phpQueryDom->find('html')->append($webSettingHtml->footer_html?:'');
}
//A端添加的网站追踪代码
$deployOptimize = DeployOptimize::where("project_id",$project->id)->first();
if (!empty($deployOptimize)){
if (isset($deployOptimize->meta) && !empty($deployOptimize->meta)){
$phpQueryDom->find('head')->append($deployOptimize->meta);
}
}
//去掉非index页面header上面的headerindex属性
//if ($route != "index"){
//$phpQueryDom->find('header')->removeAttr("headerindex");
//}
//首页头部独立样式
if ($route == "index"){
$phpQueryDom->find('header')->attr("headerprivate","");
}
//处理header顶部搜索跳转链接
$phpQueryDom->find('header form')->attr("action", "/search/");
//生成锚点,以后启用位置
//$content = $this->anchorTextHandle($project,$route,$content);
//C端访问埋点
$isBodyEnd = $phpQueryDom->find('body')->eq(0);
if (count($isBodyEnd)>=1){
$phpQueryDom->find('body')->append("<script src='https://ecdn6.globalso.com/public/customerVisit.min.js'></script>");
}else{
$phpQueryDom->find('html')->append("<script src='https://ecdn6.globalso.com/public/customerVisit.min.js'></script>");
}
$data = date("Y-m-d H:i:s");
$phpQueryDom->find('html')->append("<!-- Globalso Cache file was created on ".$data." -->");
//这个功能其他类型页面移到面包屑导航处理
if ($route == "index" || (!empty($routeMap) && $routeMap->source == WebTemplateCommon::$pageName)){
$phpQueryDom->find('head')->append('<script>var currentPage = "'.$route.'"</script>');
}
if ($route == "search"){
//暂时先去掉小语种按钮显示
$phpQueryDom->find(".change-language")->remove();
$phpQueryDom->find('head')->append('<meta name="robots" content="noindex, nofollow"/>');
}
$phpQueryDom->find('body')->attr("unevents","");
$content = $phpQueryDom->htmlOuter();
unset($phpQueryDom);
phpQuery::unloadDocuments();
//处理全部内容,如果有上线正式域名,则把所有的测试域名替换为正式域名
$projectDomainInfo = DomainInfo::where("project_id",$project->id)->first();
if (!empty($projectDomainInfo)){
$deployBuild = DeployBuild::where("project_id",$project->id)->first();
if (!empty($deployBuild)){
$parsedUrl = parse_url($deployBuild->test_domain);
$domain = $projectDomainInfo->domain;
$testDomain = $parsedUrl['host'];
if ($domain != '' && $testDomain != ''){
$content = str_replace($testDomain,$domain,$content);
}
}
}
return $content;
}
/**
* 处理锚文本
*/
public function anchorTextHandle($project,$route,$content): string
{
$source = "";
//解析路由
if ($route != "index"){
$routerNewsMap = RouteMap::where("project_id",$project->id)->where("route",$route)->first();
if (!empty($routerNewsMap)){
$source = $routerNewsMap->source;
}
}else{
$source = "index";
}
//判断来源类型
switch ($source) {
case 'news_category' || 'news':
$num = 4;
break;
case 'product_category' || 'product':
$num = 2;
break;
case 'blog_category' || 'blog':
$num = 6;
break;
case 'page':
$num = 1;
break;
default:
$num = 1;
}
//网站设置锚文本
$webSetting = WebSetting::getWebSetting($project);
if (!empty($webSetting)){
$anchorIsEnable = $webSetting->anchor_is_enable;
if ($anchorIsEnable == 0){
$anchorSetting = $webSetting->anchor_setting;
$anchorSettingArr = explode(',', substr($anchorSetting, 1, -1));
foreach ($anchorSettingArr as $item){
$itemArr = explode('"', substr($item, 1, -1));
$tracingText[] = intval($itemArr[0]);
}
if (in_array($num, $tracingText)) {
$webSettingText = WebSettingText::getWebSettingText($project);
$phpQueryDom=phpQuery::newDocument($content);
$mainElement = $phpQueryDom->find('main');
$con = $mainElement->html();
if (!empty($webSettingText)){
foreach ($webSettingText as $v){
$con = str_replace( $v->key_words, '<a target="_blank" href="'. $v->url.'">'. $v->key_words.'</a>', $con);
}
}
$mainElement->html($con);
return $phpQueryDom->htmlOuter();
}
}
}
return $content;
}
/**
* 链接项目
*/
public function connectProject($updateData)
{
$project = null;
$projectIn = Project::getProjectByDomain($updateData->domain);
if (!empty($projectIn)){
$project = Project::where("id",$projectIn->project_id)->first();
if (!empty($project)){
$project->domain = $updateData->domain;
}
ProjectService::useProject($project->id,$updateData->domain);
}
return $project;
}
/**
* 获取小语种更新国家
*/
public function getMinorLanguage($updateData)
{
$minorLanguage = [];
$updateProgressQuery = UpdateProgressModel::find($updateData->id);
if (!empty($updateProgressQuery)){
$extends = $updateProgressQuery->extends;
if (!empty($extends)){
$extends = json_decode($extends);
if (isset($extends->language) && !empty($extends->language)){
$language = $extends->language;
foreach ($language as $lang){
$minorLanguage[] = (int)$lang;
}
}
}
}
return WebSettingCountry::whereIn("id", $minorLanguage)->get();
}
}