DeletePageService.php
6.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
<?php
namespace App\Services\Html;
use App\Models\Domain\DomainInfo;
use App\Models\Project\DeleteHtmlModel;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\RouteMap\RouteDelete;
use App\Models\WebSetting\WebSetting;
use Illuminate\Support\Facades\Redis;
/**
* C端删除页面服务
*/
class DeletePageService{
/**
* 删除文件
*/
public function deleteDirectory($path): bool
{
if (!is_dir($path)) {
try {
unlink($path);
} catch (\Exception $e) {
return true;
}
return true;
}
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file) {
$filePath = $path . '/' . $file;
if (is_dir($filePath)) {
$this->deleteDirectory($filePath);
} else {
try {
unlink($filePath);
} catch (\Exception $e) {
return true;
}
}
}
rmdir($path);
return true;
}
/**
* 获取项目ID下正式+测试域名
*/
public function getAllProjectDomain($data)
{
if (Redis::get($data->project_id."_all_project_domain") != null){
$allProjectDomain =Redis::get($data->project_id."_all_project_domain");
}else{
$domainArr = [];
$onlineProject = DeployOptimize::where('project_id',$data->project_id)->first();
if (!empty($onlineProject)){
if ($onlineProject->domain != null){
$domainInfo = DomainInfo::where("id",$onlineProject->domain)->where("status",1)->first();
if (!empty($domainInfo)){
$domainArr[] = $domainInfo->domain;
}
}
}
$testProject = DeployBuild::where('project_id',$data->project_id)->first();
if (!empty($testProject)){
$testDomain = $testProject->test_domain;
if (strpos($testDomain, 'http://') !== false){
$testDomain = str_replace('http://', '', $testDomain);
}
if (strpos($testDomain, 'https://') !== false){
$testDomain = str_replace('https://', '', $testDomain);
}
$testDomain = str_replace('/', '', $testDomain);
$domainArr[] = $testDomain;
}
if (!empty($domainArr)){
$allProjectDomain = json_encode($domainArr);
Redis::set($data->project_id."_all_project_domain", $allProjectDomain);
Redis::expire($data->project_id."_all_project_domain", WebSetting::$redisExpireTime);
}
}
return $allProjectDomain;
}
/**
* 构造删除路径
*/
public function deleteHtmlItems($data): bool
{
$getAllProjectDomain = $this->getAllProjectDomain($data);
if ($getAllProjectDomain != null){
$getAllProjectDomain = json_decode($getAllProjectDomain);
$deleteHtml = DeleteHtmlModel::where("project_id",$data->project_id)->get();
foreach ($getAllProjectDomain as $domainItem){
if (!empty($deleteHtml)){
foreach ($deleteHtml as $v){
//国家
$pageService = new PageService();
$webCountry = $pageService->getCountryList($data->project_id);
if ($webCountry != null){
foreach ($webCountry as $item){
if ($v->route == "index"){
$path = public_path($domainItem."/".$item->alias."/index.html");
}else{
$path = public_path($domainItem."/".$item->alias."/".$v->route);
}
if (file_exists($path)){
$this->deleteDirectory($path);
}
}
}
if ($v->route == "index"){
$path = public_path($domainItem."/index.html");
}else{
$path = public_path($domainItem."/".$v->route);
}
if (file_exists($path)){
$this->deleteDirectory($path);
}
RouteDelete::where("project_id",$data->project_id)->where("route",$v->route)->delete();
}
}
//删除C端固定页面
$pageService = new PageService();
$webCountry = $pageService->getCountryList($data->project_id);
if ($webCountry != null){
foreach ($webCountry as $item) {
//删除C端固定小语种页面
$productsPath = public_path($domainItem . "/" . $item->alias . "/products");
$newsPath = public_path($domainItem . "/" . $item->alias . "/news");
$blogPath = public_path($domainItem . "/" . $item->alias . "/blog");
if (file_exists($productsPath)) {
$this->deleteDirectory($productsPath);
}
if (file_exists($newsPath)) {
$this->deleteDirectory($newsPath);
}
if (file_exists($blogPath)) {
$this->deleteDirectory($blogPath);
}
}
}
//删除C端固定主站页面
$productsPath = public_path($domainItem."/products");
$newsPath = public_path($domainItem."/news");
$blogPath = public_path($domainItem."/blog");
if (file_exists($productsPath)){
$this->deleteDirectory($productsPath);
}
if (file_exists($newsPath)){
$this->deleteDirectory($newsPath);
}
if (file_exists($blogPath)){
$this->deleteDirectory($blogPath);
}
}
}
return true;
}
}