CustomTemplateLogic.php
22.2 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
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\BCustomTemplate;
use App\Models\Template\BTemplateCom;
use App\Models\Template\BTemplateLog;
use App\Models\Template\Setting;
use App\Models\Template\BTemplate;
use App\Models\Template\TemplateTypeMain;
class CustomTemplateLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new BCustomTemplate();
$this->param = $this->requestAll;
}
/**
* @remark :获取自定义模块列表
* @name :lists
* @author :lyh
* @method :post
* @time :2023/6/29 15:46
*/
public function customTemplateLists($map,$page,$row,$order = 'created_at'){
$filed = ['id','name','status','url','operator_id','title','keywords','description','project_id','is_upgrade','six_read','is_visualization','created_at','updated_at'];
$map['deleted_status'] = 0;
$map['project_id'] = $this->user['project_id'];
$lists = $this->model->lists($map,$page,$row,$order,$filed);
return $this->success($lists);
}
/**
* @remark :获取当前自定义界面详情
* @name :customTemplateInfo
* @author :lyh
* @method :post
* @time :2023/6/29 16:23
*/
public function customTemplateInfo(){
$info = $this->model->read(['id'=>$this->param['id']]);
$info['image'] = getImageUrl($info['image'],$this->user['storage_type'],$this->user['project_location']);
if($info === false){
$this->fail('当前数据不存在');
}
if($info['is_visualization'] == 0 || $info['is_visualization'] == 1){
$template_id = $this->getTemplateId();
$html = $this->getTemplateComHtml($info['html'],$info['html_style'],$template_id);
$info['html'] = $this->getHeadFooter($html);
}
return $this->success($info);
}
/**
* @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 :getTemplateComHtml
* @author :lyh
* @method :post
* @time :2024/4/29 16:53
*/
public function getTemplateComHtml($html,$html_style,$template_id){
if(empty($html)){
$html = "<main></main>";
$html_style = "<style id='globalsojs-styles'></style>";
}
$headComInfo = $this->getHeadComHtml(['common_type'=>BTemplate::COMMON_HEAD,'template_id'=>$template_id]);
$bTemplateComModel = new BTemplateCom();
$condition['common_type'] = BTemplate::COMMON_OTHER;
$condition['source'] = $headComInfo['source'];
$condition['template_id'] = $template_id;
$otherInfo = $bTemplateComModel->read($condition);
if($otherInfo === false){
$this->fail('获取失败,请联系管理员');
}
$footerComInfo = $this->getFooterComHtml($template_id);
$commonInfo = ['head_html'=>$headComInfo['html'] ?? '', 'head_style'=>$headComInfo['html_style'] ?? '', 'other'=>$otherInfo['html'] ?? '',
'footer_html'=>$footerComInfo['html'] ?? '','footer_style'=>$footerComInfo['html_style'] ?? ''];
$html = $commonInfo['head_style'].$html_style.$commonInfo['footer_style'].$commonInfo['other']. $commonInfo['head_html'].$html.$commonInfo['footer_html'];
return $this->success($html);
}
/**
* @remark :公共头部
* @name :HeadComHtml
* @author :lyh
* @method :post
* @time :2024/4/29 17:20
*/
public function getHeadComHtml($condition){
$bTemplateComModel = new BTemplateCom();
$condition['source'] = $this->getType();
$condition['common_type'] = BTemplate::COMMON_HEAD;
$headComInfo = $bTemplateComModel->read($condition);
if($headComInfo === false){
//取默认公共的
$template_id = $this->getTemplateId();
$condition['source'] = BTemplate::SOURCE_COM;
$headComInfo = $bTemplateComModel->read($condition);
if($headComInfo === false){
$this->fail('获取失败,请联系管理员');
}
}
return $this->success($headComInfo);
}
/**
* @remark :公共底部
* @name :footerComHtml
* @author :lyh
* @method :post
* @time :2024/4/29 17:18
*/
public function getFooterComHtml($template_id){
$bTemplateComModel = new BTemplateCom();
$condition['template_id'] = $template_id;
$condition['common_type'] = BTemplate::COMMON_FOOTER;
$condition['source'] = $this->getType(BTemplate::COMMON_FOOTER);
$footerComInfo = $bTemplateComModel->read($condition);
if($footerComInfo === false){
$template_id = $this->getTemplateId();
//取默认首页的
$condition['source'] = BTemplate::SOURCE_COM;
$footerComInfo = $bTemplateComModel->read($condition);
if($footerComInfo === false){
$this->fail('获取失败,请联系管理员');
}
}
return $this->success($footerComInfo);
}
/**
* @remark :保存自定义界面
* @name :customTemplateSave
* @author :lyh
* @method :post
* @time :2023/6/29 16:21
*/
public function customTemplateSave(){
$this->param = $this->handleSaveParam();;
if(isset($this->param['id']) && !empty($this->param['id'])){
$id = $this->param['id'];
$is_upgrade = $this->param['is_upgrade'] ?? 0;//1:5.0数据 0:6.0
$six_read = $this->param['six_read'] ?? 0;//5.0数据时,是否按6.0显示
if($is_upgrade == 0 || $six_read == 0) {
$this->param['url'] = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_PAGE, $id, $this->user['project_id']);
if(($this->param['url'] == 'news') || ($this->param['url'] == 'product') || ($this->param['url'] == 'blog')){
$this->fail('不允许创建路由为:'.$this->param['url']);
}
}
$this->editCustomRoute($this->param['url']);
$this->param['is_visualization'] = isset($this->param['is_visualization']) ? $this->param['is_visualization'] : 0;
if($this->param['is_visualization'] == 0 || $this->param['is_visualization'] == 1){
unset($this->param['html'],$this->param['html_style']);
}
$this->model->edit($this->param,['id'=>$id]);
}else{
if($this->param['url'] == $this->model::NOT_FOUND_PAGE_URL){
$this->fail('404页面已存在');
}
if($this->param['url'] == 'search'){
$bTemplateMainModel = new TemplateTypeMain();
$mainInfo = $bTemplateMainModel->read(['type'=>8]);
if($mainInfo !== false){
$this->param['html'] = $mainInfo['main_html'];
$this->param['html_style'] = "<style id='globalsojs-styles'></style>";
}
}
if(($this->param['url'] == 'news') || ($this->param['url'] == 'product') || ($this->param['url'] == 'blog')){
$this->fail('不允许修改路由为:'.$this->param['url']);
}
$this->param['project_id'] = $this->user['project_id'];
$id = $this->model->addReturnId($this->param);
$route = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_PAGE, $id, $this->user['project_id']);
$this->addUpdateNotify(RouteMap::SOURCE_PAGE,$route);
$this->model->edit(['url'=>$route],['id'=>$id]);
}
return $this->success(['id'=>$id]);
}
/**
* @remark :参数验证
* @name :handleSaveParam
* @author :lyh
* @method :post
* @time :2024/8/6 16:52
*/
public function handleSaveParam(){
if(isset($param['image'])){
$this->param['image'] = str_replace_url($this->param['image'] ?? '');
}
$this->param['url'] = str_replace_url($this->param['url']);
$this->param['operator_id'] = $this->user['id'];
if(empty($this->project)){
$this->fail('登录已过期,请重新登录');
}
if(($this->project['deploy_build']['is_search'] == 0) && ($this->param['url'] == 'search')){
$this->fail('请先开启搜索页可视化');
}
if(($this->project['deploy_build']['is_keyword'] == 0) && ($this->param['url'] == 'keyword')){
$this->fail('请先开启聚合页可视化');
}
return $this->success($this->param);
}
/**
* @remark :可视化保存html
* @name :saveHtml
* @author :lyh
* @method :post
* @time :2023/9/16 17:13
*/
public function saveHtml(){
$html = $this->param['html'];
$info = $this->model->read(['id'=>$this->param['id']],['id','is_visualization','url']);
if($info === false){
$this->fail('当前数据不存在或已被删除');
}
if($info['is_visualization'] == 0 || $info['is_visualization'] == 1){//非定制项目+可视化页面
//获取设置的默认模版
$bSettingModel = new Setting();
$bSettingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
if($bSettingInfo === false){
$this->fail('请先选择模版');
}
$handleInfo = $this->handleResultParam($html);
if(!isset($this->param['template_status'])){//代表直接替换模版 不更新头部底部
$this->saveTemplateCom($handleInfo,$bSettingInfo['template_id']);
}
$this->param['html'] = $handleInfo['main_html'];
$this->param['html_style'] = $handleInfo['main_style'];
}
$rs = $this->model->edit($this->param,['id'=>$this->param['id'],'project_id'=>$this->user['project_id']]);
if($rs === false){
$this->fail('系统错误,请联系管理');
}
$this->setTemplateLog($bSettingInfo['template_id'] ?? 0,$html,$this->param['id']);
//通知
$this->addUpdateNotify(RouteMap::SOURCE_PAGE,$info['url']);
$this->curlDelRoute(['old_route'=>$info['url'],'new_route'=>$info['url']]);
return $this->success();
}
/**
* @remark :生成记录
* @name :setTemplateLog
* @author :lyh
* @method :post
* @time :2023/8/23 11:16
*/
public function setTemplateLog($template_id,$html,$source_id){
$handleInfo = $this->handleResultParam($html);
$data = [
'template_id'=>$template_id,
'project_id'=>$this->user['project_id'],
'operator_id'=>$this->user['id'],
'text'=>$html,
'source'=>9,
'source_id'=>$source_id,
'head_html'=>$handleInfo['head_html'],
'head_css'=>$handleInfo['head_style'],
'footer_html'=>$handleInfo['footer_html'],
'footer_css'=>$handleInfo['footer_style'],
'main_html' => $handleInfo['main_html'],
'main_css' => $handleInfo['main_style'],
'other'=>$handleInfo['other']
];
$bTemplateLogModel = new BTemplateLog();
return $bTemplateLogModel->add($data);
}
/**
* @remark :截取数据返回
* @name :handleResultParam
* @author :lyh
* @method :post
* @time :2024/5/6 14:14
*/
public function handleResultParam($html){
$handleInfo = [
'head_html'=>characterTruncation($html,'/<header\b[^>]*>(.*?)<\/header>/s'),
'head_style'=>characterTruncation($html,'/<style id="globalsojs-header">(.*?)<\/style>/s'),
'footer_html'=>characterTruncation($html,'/<footer\b[^>]*>(.*?)<\/footer>/s'),
'footer_style'=>characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<\/style>/s'),
'main_html' => characterTruncationStr($html,"<main","</main>"),
'main_style' => characterTruncation($html,'/<style id="globalsojs-styles">(.*?)<\/style>/s'),
];
$footer_other = str_replace('<header','',characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<header/s'));
$other = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', '', $footer_other);
$handleInfo['other'] = $other;
return $this->success($handleInfo);
}
/**
* @remark :保存公共部分(头部。底部。连接部分)
* @name :saveTemplateCom
* @author :lyh
* @method :post
* @time :2024/4/29 10:32
*/
public function saveTemplateCom($handleInfo,$template_id){
$typeArr = [BTemplate::COMMON_HEAD, BTemplate::COMMON_FOOTER, BTemplate::COMMON_OTHER];
$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($type);//头部是否为独立头部
}elseif ($type == BTemplate::COMMON_FOOTER){
$param['html'] = $handleInfo['footer_html'];
$param['html_style'] = $handleInfo['footer_style'];
$typeSource = $this->getType($type);
}else{
$param['html'] = $handleInfo['other'];
$param['html_style'] = null;
$typeSource = $this->getType();
}
//查看当前数据是否还存在
$condition = ['project_id'=>$this->user['project_id'],'template_id'=>$template_id,'source'=>$typeSource,'common_type'=>$type];
$info = $templateComModel->read($condition);
if($info === false){
$data = array_merge($param,$condition);
$rs = $templateComModel->add($data);
}else{
$rs = $templateComModel->edit($param,$condition);
}
if($rs === false){
$this->fail('保存错误,请联系管理员');
}
}
return $this->success();
}
/**
* @remark :(非定制)保存时获取获取设置的类型
* @name :getType
* @author :lyh
* @method :post
* @time :2023/10/21 17:29
*/
public function getType($commonType = BTemplate::COMMON_HEAD){
$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'=>9,'is_list'=>0,'is_custom'=>0]);
if ($configInfo === false) {
return $this->success($type);
}
//头部
if($commonType == BTemplate::COMMON_HEAD){
return $this->success($configInfo['header_status'] == 0 ? $type : 9);
}
//底部
if($commonType == BTemplate::COMMON_FOOTER){
return $this->success($configInfo['footer_status'] == 0 ? $type : 9);
}
return $this->success($type);
}
/**
* @remark :查看路由是否更新
* @name :editProduct
* @author :lyh
* @method :post
* @time :2023/9/7 10:02
*/
public function editCustomRoute($route){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info['url'] == $this->model::NOT_FOUND_PAGE_URL){
if($route != '404'){
$this->fail('不可删除, 只可以修改tdk');
}
}
$this->addUpdateNotify(RouteMap::SOURCE_PAGE,$route);
$this->curlDelRoute(['old_route'=>$info['url'],'new_route'=>$route]);
return true;
}
/**
* @remark :删除自定义界面
* @name :customTemplateDel
* @author :lyh
* @method :post
* @time :2023/6/29 16:21
*/
public function customTemplateDel(){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前数据不存在或者已被删除');
}
if($info['url'] == $this->model::NOT_FOUND_PAGE_URL){
$this->fail('404页面不可删除');
}
try {
if($info['status'] != 2){
$this->model->edit(['status'=>2],['id'=>$this->param['id']]);
}else {
$this->delRoute($this->param['id']);
$this->model->del(['id' => $this->param['id']]);
}
}catch (\Exception $e){
$this->fail('系统错误,请联系管理员');
}
return $this->success();
}
/**
* @remark :删除路由
* @name :delRoute
* @author :lyh
* @method :post
* @time :2023/9/7 10:50
*/
public function delRoute($id)
{
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_PAGE, $id, $this->user['project_id']);
//生成一条删除路由记录
$info = $this->model->read(['id' => $id], ['id', 'url']);
$this->curlDelRoute(['old_route'=>$info['url']]);
return $this->success();
}
/**
* @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 :getStatusNumber
* @author :lyh
* @method :post
* @time :2023/7/29 17:40
*/
public function getStatusNumber(){
//三种状态 0:草稿 1:发布 2:回收站
$data = ['dra'=>0,'pub'=>1,'del'=>2,'tal'=>3];
foreach ($data as $k => $v){
if($v == 3){
$data[$k] = $this->model->where(['deleted_status'=>0,'project_id'=>$this->user['project_id']])->count();
}else{
$data[$k] = $this->model->where(['status'=>$v,'deleted_status'=>0,'project_id'=>$this->user['project_id']])->count();
}
}
return $this->success($data);
}
/**
* @remark :单页面还原
* @name :saveRollbackVersion
* @author :lyh
* @method :post
* @time :2024/4/23 14:00
*/
public function saveRollbackVersion(){
//获取当前数据记录详情
$log = new BTemplateLog();
$logInfo = $log->read(['id'=>$this->param['id']]);
if($logInfo === false){
$this->fail('未获取到当前记录');
}
//获取当前数据详情
$info = $this->model->read(['id'=>$logInfo['source_id']],['is_visualization']);
if($info['is_visualization'] == 0 || $info['is_visualization'] == 1){
//还原头部+底部
$comTemplateModel = new BTemplateCom();
//还原头部底部
$header_type = $this->getType(BTemplate::COMMON_HEAD);
$comTemplateModel->edit(['html'=>$logInfo['other']],['template_id'=>$logInfo['template_id'],'source'=>$header_type,'project_id'=>$this->user['project_id'],'common_type'=>BTemplate::COMMON_OTHER]);
$comTemplateModel->edit(['html'=>$logInfo['head_html'], 'html_style'=>$logInfo['head_css']],['template_id'=>$logInfo['template_id'],'source'=>$header_type,'project_id'=>$this->user['project_id'],'common_type'=>BTemplate::COMMON_HEAD]);
$footer_type = $this->getType(BTemplate::COMMON_FOOTER);
$comTemplateModel->edit(['html'=>$logInfo['footer_html'], 'html_style'=>$logInfo['footer_css']],['template_id'=>$logInfo['template_id'],'source'=>$footer_type,'project_id'=>$this->user['project_id'],'common_type'=>BTemplate::COMMON_FOOTER]);
$this->model->edit(['html'=>$logInfo['main_html'],'html_style'=>$logInfo['main_css']],['id'=>$logInfo['source_id']]);
}else{
$this->model->edit(['html'=>$logInfo['text']],['id'=>$logInfo['source_id']]);
}
return $this->success();
}
/**
* @remark :复制单页面
* @name :copyCustomTemplateInfo
* @author :lyh
* @method :post
* @time :2024/4/28 16:16
*/
public function copyCustomTemplateInfo(){
$info = $this->model->read(['id'=>$this->param['id']]);
$param = $this->setCustomTemplateParams($info);
$save_id = $this->model->insertGetId($param);
$route = RouteMap::setRoute($param['url'], RouteMap::SOURCE_PRODUCT, $save_id, $this->user['project_id']);
$this->model->edit(['route'=>$route],['id'=>$save_id]);
return $this->success(['id'=>$save_id]);
}
/**
* @remark :组装数据
* @name :setCustomTemplateParams
* @author :lyh
* @method :post
* @time :2024/4/28 16:18
*/
public function setCustomTemplateParams($info)
{
return [
'name' => $info['name'] . '-copy',
'status' => $info['status'],
'url' => $info['url'],
'html' => $info['html'],
'html_style' => $info['html_style'],
'project_id' => $info['project_id'],
'section_list_id' => $info['section_list_id'],
'is_visualization' => $info['is_visualization'],
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
}
}