ReplaceHtmlLogic.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
283
284
285
286
287
288
289
290
<?php
/**
* @remark :
* @name :ReplaceHtmlLogic.php
* @author :lyh
* @method :post
* @time :2024/5/8 10:03
*/
namespace App\Http\Logic\Aside\Template;
use App\Http\Logic\aside\BaseLogic;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\CustomModule\CustomModule;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
use App\Models\Template\TemplateReplaceHtml;
use App\Models\Template\TemplateReplaceHtmlLog;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;
class ReplaceHtmlLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new TemplateReplaceHtml();
$this->param = $this->requestAll;
}
/**
* @remark :获取模版id
* @name :getTemplateId
* @author :lyh
* @method :post
* @time :2024/5/9 18:03
*/
public function getTemplateId($typeInfo){
//获取当前template_id
$bSettingModel = new Setting();
$templateInfo = $bSettingModel->read(['project_id'=>$this->param['project_id']]);
if($templateInfo === false){
$this->fail('请先选择模版');
}
$template_id = $templateInfo['template_id'];
if($typeInfo['is_custom'] == 1){//扩展模块
return $this->getCustomTemplateId($typeInfo,$template_id);
}
//获取当前项目详情
$projectInfo = (new ProjectLogic())->getProjectInfo($this->param['project_id']);
//查看当前页面是否为定制
if($projectInfo['is_customized'] == BTemplate::IS_VISUALIZATION) {//定制项目
$type = $this->getCustomizedType($typeInfo['source'], $typeInfo['is_list']);//获取定制界面类型
//查看当前页面是否定制,是否开启可视化
$page_array = (array)$projectInfo['is_visualization']->page_array;//获取所有定制界面
if (in_array($type, $page_array)) {//当前页面是定制界面
$template_id = 0;
}
}
return $this->success($template_id);
}
/**
* @remark :获取扩展模块的templateId
* @name :getCustomTemplateId
* @author :lyh
* @method :post
* @time :2024/5/10 14:53
*/
public function getCustomTemplateId($typeInfo,$template_id){
$customModuleModel = new CustomModule();
$moduleInfo = $customModuleModel->read(['id'=>$typeInfo['type']],['list_customized','detail_customized']);
if($moduleInfo === false){
$this->fail('当前扩展模块不存在或已被删除');
}
if($typeInfo['is_list'] == 1 && $moduleInfo['list_customized'] == 1){
$template_id = 0;
}
if($typeInfo['is_list'] == 0 && $moduleInfo['detail_customized'] == 1){
$template_id = 0;
}
return $this->success($template_id);
}
/**
* @remark :定制页面头部类型---根据source获取type类型
* @name :getType
* @author :lyh
* @method :post
* @time :2023/11/16 11:20
*/
public function getCustomizedType($source,$is_list){
$type = BTemplate::TYPE_HOME;
if($source == BTemplate::SOURCE_PRODUCT){
if($is_list == BTemplate::IS_LIST){
$type = BTemplate::TYPE_PRODUCT_LIST;
}else{
$type = BTemplate::TYPE_PRODUCT_DETAIL;
}
}
if($source == BTemplate::SOURCE_BLOG){
if($is_list == BTemplate::IS_LIST){
$type = BTemplate::TYPE_BLOG_LIST;
}else{
$type = BTemplate::TYPE_BLOG_DETAIL;
}
}
if($source == BTemplate::SOURCE_NEWS){
if($is_list == BTemplate::IS_LIST){
$type = BTemplate::TYPE_NEWS_LIST;
}else{
$type = BTemplate::TYPE_NEWS_DETAIL;
}
}
return $type;
}
/**
* @remark :替换可视化的html代码(按类型)
* @name :replaceHtml
* @author :lyh
* @method :post
* @time :2024/5/7 15:52
*/
public function replaceTemplateMainHtml(){
$data = $this->sourceTypeInfo($this->param['project_id']);
$typeInfo = $data[$this->param['name']];
ProjectServer::useProject($this->param['project_id']);
$bTemplateModel = new BTemplate();
if($typeInfo['source'] == 0){//所有页面
$bSettingModel = new Setting();
$templateInfo = $bSettingModel->read(['project_id'=>$this->param['project_id']]);
if($templateInfo === false){
$this->fail('请先选择模版');
}
$template_id = $templateInfo['template_id'];
$condition = ['template_id'=>$template_id,'main_html'=>['like','%'.$this->param['old_html'].'%']];
$total_num = $bTemplateModel->formatQuery($condition)->count();
}else{
$template_id = $this->getTemplateId($typeInfo);
$condition = ['source'=>$typeInfo['source'],'is_custom'=>$typeInfo['is_custom'], 'is_list'=>$typeInfo['is_list'],
'template_id'=>$template_id,'main_html'=>['like','%'.$this->param['old_html'].'%']];
$total_num = $bTemplateModel->formatQuery($condition)->count();
}
DB::disconnect('custom_mysql');
$replaceId = $this->saveReplaceHtml($this->param,$typeInfo,$template_id,$total_num ?? 0);
return $this->success(['id'=>$replaceId]);
}
/**
* @remark :生成一条记录
* @name :saveTemplateLog
* @author :lyh
* @method :post
* @time :2024/5/8 9:23
*/
public function saveReplaceHtml($param,$typeInfo,$template_id,$total_num){
$logData = [
'source'=>$typeInfo['source'],
'is_custom'=>$typeInfo['is_custom'],
'is_list'=>$typeInfo['is_list'],
'template_id'=>$template_id,
'status'=>$this->model::STATUS,
'old_html'=>$param['old_html'],
'html'=>$param['html'],
'project_id'=>$param['project_id'],
'total_num'=>$total_num,
'operator_id'=>$this->manager['id']
];
return $this->model->addReturnId($logData);
}
/**
* @remark :生成还原记录
* @name :reductionHtml
* @author :lyh
* @method :post
* @time :2024/5/8 10:35
*/
public function reductionHtml(){
$info = $this->model->read(['id'=>$this->param['id'],'status'=>$this->model::STATUS_END]);
if($info === false){
$this->fail('当前数据不存在');
}
if($info['source'] == 0){//当前数据是替换的所有页面
$data = $this->sourceTypeInfo($info['project_id']);
$typeInfo = $data[$this->param['name']];
if($typeInfo['source'] != 0){//回滚页面
$info['source'] = $typeInfo['source'];
$info['is_custom'] = $typeInfo['is_custom'];
$info['is_list'] = $typeInfo['is_list'];
$replaceId = $this->saveResultReplaceHtml($info);
return $this->success(['id'=>$replaceId]);
}
}
$replaceId = $this->saveResultReplaceHtml($info);
return $this->success(['id'=>$replaceId]);
}
/**
* @remark :保存还原的html
* @name :saveResultReplaceHtml
* @author :lyh
* @method :post
* @time :2024/5/10 10:01
*/
public function saveResultReplaceHtml($info){
$logData = [
'source'=>$info['source'],
'is_custom'=>$info['is_custom'],
'is_list'=>$info['is_list'],
'template_id'=>$info['template_id'],
'status'=>$this->model::STATUS,
'old_html'=>$info['html'],
'html'=>$info['old_html'],
'project_id'=>$info['project_id'],
'is_rollback'=>1,
'rollback_id'=>$info['id'],
'total_num'=>$info['total_num'],
'operator_id'=>$this->manager['id']
];
return $this->model->addReturnId($logData);
}
/**
* @remark :替换类型
* @name :sourceTypeInfo
* @author :lyh
* @method :post
* @time :2024/5/9 17:15
*/
public function sourceTypeInfo($project_id){
$data = $this->model->sourceType();
ProjectServer::useProject($project_id);
$customModule = new CustomModule();
$moduleList = $customModule->list(['project_id'=>$project_id],'id',['id','name']);
foreach ($moduleList as $value){
$data[$value['name'].'详情'] = ['source'=>$value['id'],'is_list'=>0,'is_custom'=>1];
$data[$value['name'].'列表'] = ['source'=>$value['id'],'is_list'=>1,'is_custom'=>1];
}
DB::disconnect('custom_mysql');
return $this->success($data);
}
/**
* @remark :根据类型获取名称
* @name :getSourceName
* @author :lyh
* @method :post
* @time :2024/5/10 16:55
*/
public function getSourceName($source,$is_list,$is_custom,$project_id){
$arr = ['source'=>$source,'is_list'=>$is_list,'is_custom'=>$is_custom];
$data = $this->sourceTypeInfo($project_id);
foreach ($data as $k => $v){
if($v == $arr){
return $this->success($k);
}
}
return $this->success();
}
/**
* @remark :根据id还原
* @name :rollbackIdHtml
* @author :lyh
* @method :post
* @time :2024/5/11 11:31
*/
public function rollbackIdHtml(){
$replaceHtmlLogModel = new TemplateReplaceHtmlLog();
$info = $replaceHtmlLogModel->read(['id'=>$this->param['id']]);
ProjectServer::useProject($info['project_id']);
$bTemplateModel = new BTemplate();
$condition = ['source'=>$info['source'],'source_id'=>$info['source_id'],
'template_id'=>$info['template_id'],'is_custom'=>$info['is_custom'],'is_list'=>$info['is_list']];
$old_html = $info['old_html'];
$html = $info['html'];
if($info['template_id'] == 0){
$bTemplateModel->formatQuery($condition)->update(['html' => DB::raw("REPLACE(html, '$old_html', '$html')")]);
}else{
$bTemplateModel->formatQuery($condition)->update(['main_html' => DB::raw("REPLACE(main_html, '$old_html', '$html')")]);
}
//修改当前数据为还原数据
$replaceHtmlLogModel->edit(['is_rollback'=>1],['id'=>$this->param['id']]);
DB::disconnect('custom_mysql');
return $this->success();
}
}