InitHtmlLogic.php
17.8 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
<?php
/**
* @remark :
* @name :InitHtmlLogic.php
* @author :lyh
* @method :post
* @time :2023/12/27 10:39
*/
namespace App\Http\Logic\Bside\BTemplate;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\IsCom\ProjectComConfig;
use App\Models\RouteMap\RouteMap;
use App\Models\Service\Service as ServiceSettingModel;
use App\Models\Template\BTemplate;
use App\Models\Template\BTemplateCom;
use App\Models\Template\BTemplateMain;
use App\Models\Template\Setting;
use App\Models\Template\TemplateTypeMain;
class InitHtmlLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
}
/**
* @remark :获取非定制项目复合页数据
* @name :getDetailHtml
* @author :lyh
* @method :post $param (type:类型,2产品 3博客 4新闻 对应扩展模块的id)
* @time :2023/12/27 10:50
*/
public function getDetailHtml(){
$template_id = $this->getTemplateId();
$is_custom = $this->param['is_custom'] ?? 0;//TODO::1:代表扩展模块
$is_list = $this->param['is_list'] ?? 0;//TODO::1:代表分类列表模块
$data = array();
//获取设置的默认中间部分
$bTemplateMainModel = new BTemplateMain();
$mainInfo = $bTemplateMainModel->read(['project_id'=>$this->user['project_id'],'type'=>$this->param['type'],'is_list'=>$is_list,'is_custom'=>$is_custom]);
if($mainInfo === false){
$main_html = $this->getInitModule($this->param['type'],$is_custom,$is_list);
$main_style = "<style id='globalsojs-styles'></style>";
}else{
$main_html = $mainInfo['main_html'];
$main_style = $mainInfo['main_css'];
$data['id'] = $mainInfo['id'];
$data['updated_at'] = $mainInfo['updated_at'];
}
$commonInfo = $this->getTemplateComHtml($this->param['type'],$is_list,$is_custom,$template_id); //获取头部
$html = $commonInfo['head_style'].$main_style.$commonInfo['footer_style'].$commonInfo['other'].$commonInfo['head_html'].$main_html.$commonInfo['footer_html'];
$html = $this->getHeadFooter($html);//组装数据
$data['html'] = $html;
$data['template_id'] = $template_id;
return $this->success($data);
}
/**
* @remark :非定制获取头部+底部
* @name :getTemplateComHtml
* @author :lyh
* @method :post
* @time :2024/4/29 16:53
*/
public function getTemplateComHtml($source,$is_list,$is_custom,$template_id){
$condition = ['common_type'=>BTemplate::COMMON_HEAD,'source'=>$source,'is_list'=>$is_list,'is_custom'=>$is_custom,'template_id'=>$template_id];
$headComInfo = $this->getHeadComHtml($condition,$source,$is_list,$is_custom,$template_id);
$bTemplateComModel = new BTemplateCom();
$otherInfo = $bTemplateComModel->read(['template_id'=>$template_id,'source'=>$headComInfo['source'],'common_type'=>BTemplate::COMMON_OTHER,'is_list'=>$headComInfo['is_list'],'is_custom'=>$headComInfo['is_custom']]);
if($otherInfo === false){
$this->fail('获取失败,请联系管理员2');
}
$footerComInfo = $this->getFooterComHtml($condition,$source,$is_list,$is_custom,$template_id);
$data = ['head_html'=>$headComInfo['html'] ?? '', 'head_style'=>$headComInfo['html_style'] ?? '', 'other'=>$otherInfo['html'] ?? '',
'footer_html'=>$footerComInfo['html'] ?? '','footer_style'=>$footerComInfo['html_style'] ?? ''];
return $this->success($data);
}
/**
* @remark :公共头部
* @name :HeadComHtml
* @author :lyh
* @method :post
* @time :2024/4/29 17:20
*/
public function getHeadComHtml($condition,$source,$is_list,$is_custom,$template_id){
$commonHead = $this->getType($source,$is_list,$is_custom,BTemplate::COMMON_HEAD,$template_id);
$bTemplateComModel = new BTemplateCom();
$condition['source'] = $commonHead;
$condition['common_type'] = BTemplate::COMMON_HEAD;
$headComInfo = $bTemplateComModel->read($condition);
if($headComInfo === false){
//取默认公共的
$headComInfo = $bTemplateComModel->read(['source'=>BTemplate::SOURCE_COM,'common_type'=>BTemplate::COMMON_HEAD,'template_id'=>$template_id]);
if($headComInfo === false){
$this->fail('获取失败,请联系管理员1');
}
}
return $this->success($headComInfo);
}
/**
* @remark :公共底部
* @name :footerComHtml
* @author :lyh
* @method :post
* @time :2024/4/29 17:18
*/
public function getFooterComHtml($condition,$source,$is_list,$is_custom,$template_id){
$bTemplateComModel = new BTemplateCom();
$condition['common_type'] = BTemplate::COMMON_FOOTER;
$condition['source'] = $this->getType($source,$is_list,$is_custom,BTemplate::COMMON_FOOTER,$template_id);
$footerComInfo = $bTemplateComModel->read($condition);
if($footerComInfo === false){
//取默认首页的
$footerComInfo = $bTemplateComModel->read(['source'=>BTemplate::SOURCE_COM,'common_type'=>BTemplate::COMMON_FOOTER,'template_id'=>$template_id]);
if($footerComInfo === false){
$this->fail('获取失败,请联系管理员3');
}
}
return $this->success($footerComInfo);
}
/**
* @remark :拼接获取公共头部底部
* @name :getHeadFooter
* @author :lyh
* @method :post
* @time :2023/7/21 17:22
*/
public function getHeadFooter($html){
//获取公共主题头部底部
$serviceSettingModel = new ServiceSettingModel();
$list = $serviceSettingModel->list(['type'=>2],'created_at');
//拼接html
foreach ($list as $v){
if($v['key'] == 'head'){
$html = $v['values'].$html;
}
if($v['key'] == 'footer'){
$html = $html.$v['values'];
}
}
return $html;
}
/**
* @remark :保存复合页数据
* @name :saveDetailHtml
* @author :lyh
* @method :post
* @time :2023/12/27 11:57
*/
public function saveDetailHtml(){
$template_id = $this->getTemplateId();
$is_custom = $this->param['is_custom'] ?? 0;//TODO::1:代表扩展模块
$is_list = $this->param['is_list'] ?? 0;//TODO::1:代表分类列表模块
//保存中间部分
$bTemplateMainModel = new BTemplateMain();
$mainInfo = $bTemplateMainModel->read(['project_id'=>$this->user['project_id'],'type'=>$this->param['type'],'is_custom'=>$is_custom,'is_list'=>$is_list]);
if($mainInfo === false){
$data = [
'html'=>$this->param['html'],
'main_html'=>characterTruncationStr($this->param['html'],"<main","</main>"),
'main_css'=>characterTruncation($this->param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s'),
'section_list_id'=>$this->param['section_list_id'] ?? '',
'project_id'=>$this->user['project_id'],
'type'=>$this->param['type'],
'is_custom'=>$is_custom,
'is_list'=>$is_list
];
$bTemplateMainModel->add($data);
}else{
$data = [
'html'=>$this->param['html'],
'main_html'=>characterTruncationStr($this->param['html'],"<main","</main>"),
'main_css'=>characterTruncation($this->param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s'),
'section_list_id'=>$this->param['section_list_id'] ?? '',
];
$bTemplateMainModel->edit($data,['id'=>$mainInfo['id']]);
}
$this->saveTemplateCom($this->param['html'],$template_id,$this->param['type'],$is_list,$is_custom);
$route = RouteMap::getRoute('all',0,$this->user['project_id']);
$this->curlDelRoute(['old_route'=>$route,'new_route'=>$route]);
return $this->success();
}
/**
* @remark :保存公共部分(头部。底部。连接部分)
* @name :saveTemplateCom
* @author :lyh
* @method :post
* @time :2024/4/29 10:32
*/
public function saveTemplateCom($html,$template_id,$source,$is_list,$is_custom){
$typeArr = [BTemplate::COMMON_HEAD, BTemplate::COMMON_FOOTER, BTemplate::COMMON_OTHER];
$handleInfo = $this->handleCommonParam($html);
$templateComModel = new BTemplateCom();
foreach ($typeArr as $type){
if($type == BTemplate::COMMON_HEAD){
$param['html'] = $handleInfo['head_html'];
$param['html_style'] = $handleInfo['head_style'];
$typeSource = $this->getType($source,$is_list,$is_custom,$type,$template_id);//头部是否为独立头部
}elseif ($type == BTemplate::COMMON_FOOTER){
$param['html'] = $handleInfo['footer_html'];
$param['html_style'] = $handleInfo['footer_style'];
$typeSource = $this->getType($source,$is_list,$is_custom,$type,$template_id);
}else{
$param['html'] = $handleInfo['other'];
$param['html_style'] = null;
$typeSource = $this->getType($source,$is_list,$is_custom,BTemplate::COMMON_HEAD,$template_id);
}
if($typeSource == 99){
$condition = ['project_id'=>$this->user['project_id'],'template_id'=>$template_id,'is_list'=>0,'is_custom'=>0,'source'=>$typeSource,'common_type'=>$type];
}else{
//查看当前数据是否还存在
$condition = ['project_id'=>$this->user['project_id'],'template_id'=>$template_id,'is_list'=>$is_list,'is_custom'=>$is_custom,'source'=>$typeSource,'common_type'=>$type];
}
$info = $templateComModel->read($condition);
if($info === false){
$data = array_merge($param,$condition);
$templateComModel->add($data);
}else{
$templateComModel->edit($param,$condition);
}
}
return $this->success();
}
/**
* @remark :(非定制)保存时获取获取设置的类型
* @name :getType
* @author :lyh
* @method :post
* @time :2023/10/21 17:29
*/
public function getType($source,$is_list,$is_custom = BTemplate::IS_NO_CUSTOM,$commonType = BTemplate::COMMON_HEAD,$template_id = 0){
if($template_id == 0){
return $this->success($source);
}
$type = BTemplate::SOURCE_COM;//公共头部底部
$is_head = $this->user['configuration']['is_head'] ?? BTemplate::IS_NO_HEADER;
if($is_head == BTemplate::IS_NO_HEADER){//拓展模块为公共头+底
return $this->success($type);
}
//查看页面是否设置自定义头部底部
$comConfigModel = new ProjectComConfig();
$configInfo = $comConfigModel->read(['source'=>$source,'is_list'=>$is_list,'is_custom'=>$is_custom]);
if($configInfo === false){
return $this->success($type);
}
//头部
if($commonType == BTemplate::COMMON_HEAD){
return $this->success($configInfo['header_status'] == 0 ? $type : $source);
}
//底部
if($commonType == BTemplate::COMMON_FOOTER){
return $this->success($configInfo['footer_status'] == 0 ? $type : $source);
}
return $this->success($type);
}
/**
* @remark :保存时字符串处理
* @name :handleCommonParam
* @author :lyh
* @method :post
* @time :2023/6/29 15:35
*/
public function handleCommonParam($html){
//字符串截取
$param['head_html'] = characterTruncation($html,'/<header\b[^>]*>(.*?)<\/header>/s');
$param['footer_html'] = characterTruncation($html,'/<footer\b[^>]*>(.*?)<\/footer>/s');
$param['head_style'] = characterTruncation($html,'/<style id="globalsojs-header">(.*?)<\/style>/s');
$param['footer_style'] = characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<\/style>/s');
$footer_other = str_replace('<header','',characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<header/s'));
$param['other'] = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', '', $footer_other);
return $this->success($param);
}
/**
* @remark :默认复合页数据
* @name :getProductModule
* @author :lyh
* @method :post
* @time :2023/7/27 15:08
*/
public function getInitModule($type,$is_custom,$is_list){
if($is_custom == BTemplate::IS_CUSTOM) {
$type = BTemplate::SOURCE_CUSTOM;
}
$mainModel = new TemplateTypeMain();
$info = $mainModel->read(['type'=>$type,'is_list'=>$is_list]);
return $info['main_html'];
}
/**
* @remark :获取模版id
* @name :getTemplateId
* @author :lyh
* @method :post
* @time :2023/12/27 10:51
*/
public function getTemplateId(){
$bSettingModel = new Setting();
$bSettingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']],['id','template_id']);
if($bSettingInfo === false){
$this->fail('请先设置模板');
}
return $this->success($bSettingInfo['template_id']);
}
/**
* @remark :获取代码块
* @name :getVisualizationInfo
* @author :lyh
* @method :post
* @time :2023/11/17 14:44
*/
public function getCustomizedHtml(){
$is_list = $this->param['is_list'] ?? 0;
$is_custom = $this->param['is_custom'] ?? 0;
$bTemplateMainModel = new BTemplateMain();
$info = $bTemplateMainModel->read(['type'=>$this->param['type'],'is_custom'=>$is_custom,'is_list'=>$is_list]);
if($info === false){
$html = '';
}else{
$commonInfo = $this->getCustomizeTemplateComHtml($this->param['type'],$is_custom,$is_list);
$html = $this->handleAllHtml($commonInfo,$info['main_html']);
}
//更新头部底部
return $this->success(['html'=>$html]);
}
/**
* @remark :定制获取头部底部
* @name :getCustomizedCommonHtml
* @author :lyh
* @method :post
* @time :2023/12/29 13:13
*/
public function getCustomizeTemplateComHtml($type,$is_custom,$is_list,$template_id = 0){
$data = ['head_html'=>'','head_style'=>'','footer_html'=>'','footer_style'=>'','other'=>''];
$param = ['template_id'=>$template_id,'source'=>$type,'is_custom'=>$is_custom,'is_list'=>$is_list];
$commonTemplateModel = new BTemplateCom();
$commonList = $commonTemplateModel->list($param);
if(!empty($commonList)){
foreach ($commonList as $v){
if($v['common_type'] == BTemplate::COMMON_HEAD){
$data['head_html'] = $v['html'];
$data['head_style'] = $v['html_style'];
}elseif ($v['common_type'] == BTemplate::COMMON_FOOTER){
$data['footer_html'] = $v['html'];
$data['footer_style'] = $v['html_style'];
}else{
$data['other'] = $v['html'];
}
}
}
return $this->success($data);
}
/**
* @remark :返回整个html截取代码
* @name :handleAllHtml
* @author :lyh
* @method :post
* @time :2023/12/13 15:39
*/
public function handleAllHtml($commonInfo,$html){
if(!empty($commonInfo['head_html']) && !empty($commonInfo['footer_html'])){
$html = preg_replace('/<header\b[^>]*>(.*?)<\/header>/s', $commonInfo['head_html'], $html);
$html = preg_replace('/<footer\b[^>]*>(.*?)<\/footer>/s', $commonInfo['footer_html'], $html);
$html = preg_replace('/<style id="globalsojs-header">(.*?)<\/style>/s', $commonInfo['head_style'] ?? '', $html);
$html = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', $commonInfo['footer_style'] ?? '', $html);
}
return $html;
}
/**
* @remark :保存定制html
* @name :saveHtml
* @author :lyh
* @method :post
* @time :2023/11/15 10:12
*/
public function saveCustomizedHtml(){
try {
$is_list = $this->param['is_list'] ?? 0;
$is_custom = $this->param['is_custom'] ?? 0;
$bTemplateMainModel = new BTemplateMain();
$mainInfo = $bTemplateMainModel->read(['type'=>$this->param['type'],'is_custom'=>$is_custom,'is_list'=>$is_list]);
if($mainInfo === false){
$mainData = [
'project_id'=>$this->user['project_id'],
'type'=>$this->param['type'],
'is_list'=>$is_list,
'main_html'=>$this->param['html'],
'is_custom'=>$is_custom
];
$bTemplateMainModel->add($mainData);
}else{
$bTemplateMainModel->edit(['main_html'=>$this->param['html']],['id'=>$mainInfo['id']]);
}
//更新头部底部
$this->saveTemplateCom($this->param['html'],0,$this->param['type'],$is_list,$is_custom);
}catch (\Exception $exception){
$this->fail('保存失败,请联系开发人员');
}
return $this->success();
}
/**
* @remark :前端获取设置模块(侧边栏)
* @name :getInitModuleMain
* @author :lyh
* @method :post
* @time :2024/1/3 14:30
*/
public function getInitModuleMain(){
$mainModel = new TemplateTypeMain();
$info = $mainModel->read(['type'=>$this->param['type']]);
if($info === false){
$this->fail('当前类型数据不存在');
}
return $this->success(['html'=>$info['main_html']]);
}
}