BTemplateLogic.php 35.3 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 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
<?php

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\Project\PageSetting;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\Service\Service as ServiceSettingModel;
use App\Models\Template\BTemplateCommon;
use App\Models\Template\BTemplateMain;
use App\Models\Template\Setting;
use App\Models\Template\BTemplate;
use App\Models\Template\BTemplateLog;
use App\Models\Template\Template;
use App\Models\Template\TemplateTypeMain;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

/**
 * @remark :b端模块
 * @name   :BTemplateLogic
 * @author :lyh
 * @time   :2023/6/28 17:00
 */
class BTemplateLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new BTemplate();
        $this->param = $this->requestAll;
    }

    /**
     * @remark :获取所有公共模板
     * @name   :publicTemplateLists
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 9:34
     */
    public function publicTemplateLists($map,$page,$row,$order = 'created_at',$filed = ['id','name','image','url','created_at','status','deleted_status']){
        $templateModel = new Template();
        $map['deleted_status'] = BTemplate::STATUS;
        $map['status'] = BTemplate::STATUS;
        $lists = $templateModel->lists($map,$page,$row,$order,$filed);
        return $this->success($lists);
    }

    /**
     * @remark :获取当前选择使用的模板
     * @name   :getModuleTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 9:44
     */
    public function getTemplate(){
        $template_id = $this->getSettingTemplate($this->param['source'],$this->param['source_id']);//设置的模版id
        $data = $this->getHtml($template_id,$this->param['source'],$this->param['source_id'],$this->param['is_custom'] ?? 0);
        return $this->success($data);
    }

    /**
     * @remark :获取可视化装修的html
     * @name   :getTemplateHtml
     * @author :lyh
     * @method :post
     * @time   :2023/12/13 10:47
     */
    public function getHtml($template_id,$source,$source_id,$is_custom){
        $templateInfo = $this->webTemplateInfo($template_id,$source,$source_id,$is_custom);
        if($templateInfo === false){
            if($this->user['is_customized'] == BTemplate::SOURCE_VISUALIZATION){//处理定制页面初始数据
                $html = $this->isCustomizedPage($source,$source_id);//查看当前页面是否定制
                return $this->success(['html'=>$html,'template_id'=>$template_id]);
            }
            $mainInfo = $this->getCommonMain($source,$source_id,$is_custom);//获取中间部分代码
        }else{
            if($templateInfo['type'] == BTemplate::ALL_HTML){//返回整个html代码
                $type = $this->getCustomizedType($source, $source_id);//定制获取头部底部类型
                $commonInfo = $this->getCommonPage(0,$this->user['project_id'],$type);//获取定制头部
                $html = $this->handleAllHtml($commonInfo,$templateInfo['html']);
                return $this->success(['html'=>$html,'template_id'=>$template_id,'id'=>$templateInfo['id']]);
            }
            $mainInfo = ['main_html'=>$templateInfo['main_html'], 'main_css'=>$templateInfo['main_css']];
        }
        $commonInfo = $this->getCommonPage($source,$source_id,$template_id);//获取头部
        $html = $commonInfo['head_css'].$mainInfo['main_css'].$commonInfo['footer_css'].$commonInfo['other'].
            $commonInfo['head_html'].$mainInfo['main_html'].$commonInfo['footer_html'];
        $html = $this->getHeadFooter($html);
        $result = ['html'=>$html,'template_id'=>$template_id];
        if($templateInfo !== false){$result['id'] = $templateInfo['id'];}
        return $this->success($result);
    }

    /**
     * @remark :返回整个html截取代码
     * @name   :handleAllHtml
     * @author :lyh
     * @method :post
     * @time   :2023/12/13 15:39
     */
    public function handleAllHtml($commonInfo,$html){
        if(!empty($commonInfo)){
            $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_css'], $html);
            $html = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', $commonInfo['footer_css'], $html);
        }
        return $html;
    }

    /**
     * @remark :页面是否为定制页面获取初始代码
     * @name   :watchProjectIsCustomized
     * @author :lyh
     * @method :post
     * @time   :2023/12/13 10:55
     */
    public function isCustomizedPage($source,$source_id)
    {
        $type = $this->getCustomizedType($source, $source_id);//获取定制界面类型
        //查看当前页面是否定制,是否开启可视化
        $page_array = (array)$this->user['is_visualization']->page_array;//获取所有定制界面
        if (in_array($type, $page_array)) {//是定制界面
            //TODO::获取初始代码
            $bTemplateMainModel = new BTemplateMain();
            $customHtmlInfo = $bTemplateMainModel->read(['type'=>$type]);
            if($customHtmlInfo === false){
                $this->fail('定制页面,请先上传代码块');
            }
            return $customHtmlInfo['main_html'];
        }
        return [];
    }

    /**
     * @remark :定制界面根据source+source_id获取type类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:20
     */
    public function getCustomizedType($source,$source_id){
        $type = BTemplate::TYPE_ONE;
        if($source == BTemplate::SOURCE_PRODUCT){
            if($source_id == 0){$type = BTemplate::TYPE_THREE;}else{$type = BTemplate::TYPE_TWO;}
        }
        if($source == BTemplate::SOURCE_BLOG){
            if($source_id == 0){$type = BTemplate::TYPE_FIVE;}else{$type = BTemplate::TYPE_FOUR;}
        }
        if($source == BTemplate::SOURCE_NEWS){
            if($source_id == 0){$type = BTemplate::TYPE_SEVEN;}else{$type = BTemplate::TYPE_SIX;}
        }
        return $type;
    }

    /**
     * @remark :获取当前项目设置的模版
     * @name   :getSettingTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/12/13 10:48
     */
    public function getSettingTemplate($source,$source_id){
        if($this->user['is_customized'] == BTemplate::SOURCE_VISUALIZATION) {//定制项目
            $type = $this->getCustomizedType($source, $source_id);//获取定制界面类型
            //查看当前页面是否定制,是否开启可视化
            $page_array = (array)$this->user['is_visualization']->page_array;//获取所有定制界面
            if (in_array($type, $page_array)) {//是定制界面
                return 0;
            }
        }
        $bSettingModel = new Setting();
        $info = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        if($info === false){
            $this->fail('请先选择模版');
        }
        return $info['template_id'];
    }

    /**
     * @remark :根据参数获取数据详情
     * @name   :webTemplateInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 16:41
     */
    public function webTemplateInfo($template_id,$source,$source_id,$is_custom = 0){
        //查看当前模板是否已编辑保存web_template
        $TemplateInfo = $this->model->read([
            'template_id'=>$template_id,
            'source'=>$source,
            'project_id'=>$this->user['project_id'],
            'source_id'=>$source_id,
            'is_custom'=>$is_custom
        ]);
        return $this->success($TemplateInfo);
    }

    /**
     * @remark :获取中间公共部分
     * @name   :getCommonMain
     * @author :lyh
     * @method :post
     * @time   :2023/10/24 15:58
     */
    public function getCommonMain($source,$source_id,$is_custom){
        $data = [];
        if($is_custom == BTemplate::SOURCE_CUSTOM){
            if($source_id != 0){$type = BTemplate::TYPE_SIX;}else{$type = BTemplate::TYPE_SEVEN;}
        }else{
            if ($source == BTemplate::SOURCE_PRODUCT) {if ($source_id != 0) {$type = BTemplate::TYPE_TWO;} else {$type = BTemplate::TYPE_THREE;}}
            if ($source == BTemplate::SOURCE_BLOG) {if ($source_id != 0) {$type = BTemplate::TYPE_FOUR;} else {$type = BTemplate::TYPE_FIVE;}}
            if ($source == BTemplate::SOURCE_NEWS) {if ($source_id != 0) {$type = BTemplate::TYPE_SIX;} else {$type = BTemplate::TYPE_SEVEN;}}
            if ($source == BTemplate::SOURCE_KEYWORD) {$type = BTemplate::TYPE_EIGHT;}
        }
        //查询有没有公共详情模板
        $bTemplateMainModel = new BTemplateMain();
        $mainInfo = $bTemplateMainModel->read(['project_id'=>$this->user['project_id'],'type'=>$type]);
        if($mainInfo === false){
            $data['main_html'] = $this->getModule($type);
            $data['main_css'] = "<style id='globalsojs-styles'></style>";
        }else{
            $data['main_html'] = $mainInfo['main_html'];
            $data['main_css'] = $mainInfo['main_css'];
        }
        return $data;
    }

    /**
     * @remark :根据类型获取公共头和底
     * @name   :getCommonPage
     * @author :lyh
     * @method :post
     * @time   :2023/10/21 16:55
     */
    public function getCommonPage($source,$source_id,$template_id){
        if(isset($this->user['configuration']['is_head']) && ($this->user['configuration']['is_head'] != 0)) {
            //查看页面是否设置自定义头部底部
            $pageSettingModel = new PageSetting();
            $pageInfo = $pageSettingModel->read(['project_id' => $this->user['project_id']]);
            if ($pageInfo != false) {
                $commonTemplateModel = new BTemplateCommon();
                $data = [
                    'template_id' => $template_id,
                    'project_id' => $this->user['project_id']
                ];
                if ($source == BTemplate::SOURCE_PRODUCT) {//产品页
                    if($source_id != 0){$data['type'] = BTemplate::TYPE_TWO;if ($pageInfo['product_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = BTemplate::TYPE_THREE;if ($pageInfo['product_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == BTemplate::SOURCE_BLOG) {//博客页
                    if ($source_id != 0) {$data['type'] = BTemplate::TYPE_FOUR;if ($pageInfo['blog_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = BTemplate::TYPE_FIVE;if ($pageInfo['blog_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == BTemplate::SOURCE_NEWS) {//新闻页
                    if ($source_id != 0) {$data['type'] = BTemplate::TYPE_SIX;if ($pageInfo['news_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = BTemplate::TYPE_SEVEN;if ($pageInfo['news_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == BTemplate::SOURCE_KEYWORD) {//聚合页
                    $data['type'] = BTemplate::TYPE_EIGHT;if ($pageInfo['polymerization'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
            }
        }
        //获取首页公共的头部和底部
        if(!isset($commonInfo) || $commonInfo === false){
            $commonTemplateModel = new BTemplateCommon();
            $commonInfo = $commonTemplateModel->read(['template_id'=>$template_id,'project_id'=>$this->user['project_id'],'type'=>BTemplate::TYPE_ONE]);
        }
        return $commonInfo;
    }

    /**
     * @remark :保存修改后的模版
     * @name   :templateSave
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 11:05
     */
    public function templateSave(){
        //演示项目不允许修改
        $this->showProjectNoEdit($this->param['source']);
//        DB::beginTransaction();
//        try {
            $this->param = $this->handleDefaultString($this->param);//设置默认字符
            $templateInfo = $this->webTemplateInfo($this->param['template_id'],$this->param['source'],
                $this->param['source_id'],$this->param['is_custom']);
            if($templateInfo === false){//执行新增
                $this->templateAddHtml($this->param['html'], $this->param['source'], $this->param['source_id'],
                    $this->param['template_id'], $this->param['section_list_id'], $this->param['is_custom']);
            }else{//执行编辑
                $this->templateEditHtml($this->param['html'],$this->param['source'],$this->param['source_id'],
                    $this->param['template_id'],$this->param['section_list_id'], $this->param['is_custom']);
            }
            //更新头部信息
            $this->saveCommonHtml($this->param['html'],$this->param['source'],$this->param['source_id'],$this->param['template_id']);
            $this->setOperationRecords($this->param['html'],$this->param['source'],$this->param['source_id'],$this->param['template_id'],$this->param['is_custom']);
//            DB::commit();
//        }catch (\Exception $e){
//            DB::rollBack();
//            $this->fail('系统错误,请联系管理员');
//        }
        //通知更新
        $this->homeOrProduct($this->param['source'],$this->param['source_id'],$this->param['is_custom']);
        return $this->success();
    }

    /**
     * @remark :保存数据时设置默认字符
     * @name   :saveDefaultString
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 10:30
     */
    public function handleDefaultString($param){
        if(!isset($param['template_id'])){
            $param['template_id'] = 0;
        }
        if(!isset($param['is_custom'])){
            $param['is_custom'] = 0;
        }
        if(!isset($param['section_list_id'])){
            $param['section_list_id'] = '';
        }
        return $this->success($param);
    }

    /**
     * @remark :可视化添加数据
     * @name   :templateAddHtml
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 10:15
     */
    public function templateAddHtml($html,$source,$source_id,$template,$section_list_id,$is_custom){
        $data = [
            'source'=>$source, 'source_id'=>$source_id,'type'=>BTemplate::PAGE_HTML,
            'template_id'=>$template, 'project_id'=>$this->user['project_id'],
            'section_list_id'=>$section_list_id,'is_custom'=>$is_custom,
        ];
        $data = $this->handleVisualizationParam($html,$source,$source_id,$data);
        $this->model->add($data);
        return true;
    }

    /**
     * @remark :可视化更新html
     * @name   :templateEditHtml
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 10:26
     */
    public function templateEditHtml($html,$source,$source_id,$template,$section_list_id,$is_custom){
        $condition = [
            'source'=>$source, 'source_id'=>$source_id,
            'is_custom'=>$is_custom, 'template_id'=>$template
        ];
        $data = [
            'section_list_id'=>$section_list_id
        ];
        $data = $this->handleVisualizationParam($html,$source,$source_id,$data);
        $this->model->edit($data,$condition);
        return true;
    }

    /**
     * @remark :处理可视化数据
     * @name   :handleProjectParam
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 10:59
     */
    public function handleVisualizationParam($html,$source, $source_id,$data){
        if($this->user['is_customized'] == BTemplate::SOURCE_VISUALIZATION){//定制项目
            $type = $this->getCustomizedType($source, $source_id);//获取定制界面类型
            //查看当前页面是否定制,是否开启可视化
            $page_array = (array)$this->user['is_visualization']->page_array;//获取所有定制界面
            if (in_array($type, $page_array)) {//当前页面是定制界面
                $data['html'] = $html;
                $data['type'] = BTemplate::ALL_HTML;
            }else{
                $mainInfo = $this->handleTemplateHtml($html);
                $data['main_html'] = $mainInfo['main_html'];
                $data['main_css'] = $mainInfo['main_css'];
            }
        }else{
            $mainInfo = $this->handleTemplateHtml($html);
            $data['main_html'] = $mainInfo['main_html'];
            $data['main_css'] = $mainInfo['main_css'];
        }
        return $data;
    }


    /**
     * @remark :保存公共头部底部
     * @name   :saveCommonHtml
     * @author :lyh
     * @method :post
     * @time   :2023/12/13 17:05
     */
    public function saveCommonHtml($html,$source,$source_id,$template_id){
        $type = $this->getType($source,$source_id,$template_id);//获取头部类型1-9(首页到自定义页面)
        $templateCommonModel = new BTemplateCommon();
        $commonInfo = $templateCommonModel->read(['template_id'=>$template_id,'project_id'=>$this->user['project_id'],'type'=>$type]);//查看当前头部是否存在
        $handleInfo = $this->handleCommonParam($html);
        if($commonInfo === false){
            $data = [
                'head_html'=>$handleInfo['head_html'], 'head_css'=>$handleInfo['head_css'],'other'=>$handleInfo['other'],
                'footer_html'=>$handleInfo['footer_html'], 'footer_css'=>$handleInfo['footer_css'],
                'type'=>$type,'template_id'=>$template_id, 'project_id'=>$this->user['project_id'],
            ];
            $templateCommonModel->add($data);
        }else{
            $data = [
                'head_html'=>$handleInfo['head_html'], 'head_css'=>$handleInfo['head_css'],'other'=>$handleInfo['other'],
                'footer_html'=>$handleInfo['footer_html'], 'footer_css'=>$handleInfo['footer_css'],
            ];
            $templateCommonModel->edit($data,['id'=>$commonInfo['id']]);
        }
        //更新所有界面的other
        return $templateCommonModel->edit(['other'=>$handleInfo['other']],['project_id'=>$this->user['project_id'],'template_id'=>$template_id]);
    }

    /**
     * @remark :演示项目不允许修改首页
     * @name   :showProject
     * @author :lyh
     * @method :post
     * @time   :2023/12/12 11:27
     */
    public function showProjectNoEdit($source){
        //演示项目,不允许其他号码编辑
        if(($this->user['project_id'] == 1) && (!in_array($this->user['mobile'],$this->model->mobile)) && ($source == BTemplate::SOURCE_HOME)){
            $this->fail('演示项目仅支持演示功能,无法更改首页');
        }
        return true;
    }

    /**
     * @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_css'] = characterTruncation($html,'/<style id="globalsojs-header">(.*?)<\/style>/s');
        $param['footer_css'] = 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   :handleSaveParam
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 15:35
     */
    public function handleTemplateHtml($html){
        //字符串截取
        $param['main_html'] = characterTruncation($html,'/<main\b[^>]*>(.*?)<\/main>/s');
        $param['main_css'] = characterTruncation($html,'/<style id="globalsojs-styles">(.*?)<\/style>/s');
        return $this->success($param);
    }
    /**
     * @remark :获取设置的类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/10/21 17:29
     */
    public function getType($source,$source_id,$template_id){
        $type = 1;//首页公共头部底部
        if($template_id == 0){//保存上传的代码块时,默认为独立头部
            $this->user['configuration']['is_head'] == 1;
        }
        //查看页面是否设置自定义头部底部
        if(isset($this->user['configuration']['is_head']) && ($this->user['configuration']['is_head'] != 0)) {
            $pageSettingModel = new PageSetting();
            $pageInfo = $pageSettingModel->read(['project_id' => $this->user['project_id']]);
            if ($pageInfo !== false) {
                if ($source == BTemplate::SOURCE_PRODUCT) {if ($source_id != 0) {if ($pageInfo['product_details'] != 0) {$type = BTemplate::TYPE_TWO;}}
                else {if ($pageInfo['product_list'] != 0) {$type = BTemplate::TYPE_THREE;}}}
                if ($source == BTemplate::SOURCE_BLOG) {if ($source_id != 0) {if ($pageInfo['blog_details'] != 0) {$type = BTemplate::TYPE_FOUR;}}
                else {if ($pageInfo['blog_list'] != 0) {$type = BTemplate::TYPE_FIVE;}}}
                if ($source == BTemplate::SOURCE_NEWS) {if ($source_id != 0) {if ($pageInfo['news_details'] != 0) {$type = BTemplate::TYPE_SIX;}}
                else {if ($pageInfo['news_list'] != 0) {$type = BTemplate::TYPE_SEVEN;}}}
                if ($source == BTemplate::SOURCE_KEYWORD) {if ($pageInfo['polymerization'] != 0) {$type = BTemplate::TYPE_EIGHT;}}
            }
        }
        return $type;
    }

    /**
     * @remark :生成记录
     * @name   :setTemplateLog
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:16
     */
    public function setOperationRecords($html,$source,$source_id,$template_id,$is_custom,$type = 0){
        $data = [
            'template_id'=>$template_id,
            'project_id'=>$this->user['project_id'],
            'operator_id'=>$this->user['id'],
            'text'=>$html,
            'type'=>$type,
            'is_custom'=>$is_custom,
            'source'=>$source,
            'source_id'=>$source_id,
            'main_html' => characterTruncation($html,'/<main\b[^>]*>(.*?)<\/main>/s'),
            'main_css' => characterTruncation($html,'/<style id="globalsojs-styles">(.*?)<\/style>/s'),
            'head_html' => characterTruncation($html,'/<header\b[^>]*>(.*?)<\/header>/s'),
            'footer_html' => characterTruncation($html,'/<footer\b[^>]*>(.*?)<\/footer>/s'),
            'head_css' => characterTruncation($html,'/<style id="globalsojs-header">(.*?)<\/style>/s'),
            'footer_css' => characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<\/style>/s'),
        ];
        $footer_other = str_replace('<header','',characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<header/s'));
        $data['other'] = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', '', $footer_other);
        $bTemplateLogModel = new BTemplateLog();
        return $bTemplateLogModel->add($data);
    }

    /**
     * @remark :通知首页更新
     * @name   :IsHome
     * @author :lyh
     * @method :post
     * @time   :2023/7/31 16:05
     */
    public function homeOrProduct($source,$source_id = 0,$is_custom = 0){
        if($is_custom == 0){
            if($source == BTemplate::SOURCE_HOME){
                RouteMap::setRoute('index', RouteMap::SOURCE_PAGE, 0, $this->user['project_id']);
                $type = RouteMap::SOURCE_INDEX;
            }elseif($source == BTemplate::SOURCE_PRODUCT){
                $type = RouteMap::SOURCE_PRODUCT;
            }elseif($source == BTemplate::SOURCE_BLOG){
                $type = RouteMap::SOURCE_BLOG;
            }elseif($source == BTemplate::SOURCE_NEWS){
                $type = RouteMap::SOURCE_NEWS;
            }else{
                $type = 'all';
            }
            $route = RouteMap::getRoute($type,$source_id,$this->user['project_id']);
        }else{
            $type = RouteMap::SOURCE_MODULE;
            $route = RouteMap::getRoute($type,$source_id,$this->user['project_id']);
        }
        $this->addUpdateNotify($type,$route);
        return $this->curlDelRoute(['route'=>$route,'new_route'=>$route]);
    }

    /**
     * @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   :handleSaveParam
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 15:35
     */
    public function handleSaveParam($param){
        //字符串截取
        $param['head_html'] = characterTruncation($param['html'],'/<header\b[^>]*>(.*?)<\/header>/s');
        $param['main_html'] = characterTruncation($param['html'],'/<main\b[^>]*>(.*?)<\/main>/s');
        $param['footer_html'] = characterTruncation($param['html'],'/<footer\b[^>]*>(.*?)<\/footer>/s');
        $param['head_css'] = characterTruncation($param['html'],'/<style id="globalsojs-header">(.*?)<\/style>/s');
        $param['main_css'] = characterTruncation($param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s');
        $param['footer_css'] = characterTruncation($param['html'],'/<style id="globalsojs-footer">(.*?)<\/style>/s');
        if(!isset($param['is_custom'])){
            $param['is_custom'] = BTemplate::SOURCE_NO_CUSTOM;
        }
        $param['type'] = BTemplate::PAGE_HTML;//不显示整个HTML
        return $this->success($param);
    }

    /**
     * @remark :默认产品模块
     * @name   :getProductModule
     * @author :lyh
     * @method :post
     * @time   :2023/7/27 15:08
     */
    public function getModule($type){
        $mainModel = new TemplateTypeMain();
        $info = $mainModel->read(['type'=>$type]);
        return $info['main_html'];
    }

    /**
     * @remark :设置主题公共head
     * @name   :setHeadInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/10 15:32
     */
    public function setHeadFooterSave(){
        $serviceSettingModel = new ServiceSettingModel();
        DB::beginTransaction();
        try {
            $serviceSettingModel->del(['type'=>2]);
            $data = [
                ['type'=>2,'key'=>'head','values'=>$this->param['head'],'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')],
                ['type'=>2,'key'=>'footer','values'=>$this->param['footer'],'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')]
            ];
            $serviceSettingModel->insert($data);
            DB::commit();
        }catch (Exception $e){
            DB::rollBack();
            $this->fail('error');
        }
    }

    /**
     * @remark :获取主题公共head
     * @name   :getHeadInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/10 15:33
     */
    public function getHeadFooterList(){
        $serviceSettingModel = new ServiceSettingModel();
        $list = $serviceSettingModel->list(['type'=>2],'created_at');
        return $this->success($list);
    }


    /**
     * @remark :获取类型
     * @name   :getModuleType
     * @author :lyh
     * @method :any
     * @time   :2023/7/17 16:03
     */
    public function getModuleType(): array
    {
        //定义数据结构
        $data = $this->model->product_type;
        //产品,新闻,博客,一级分类数据
        $map = ['pid'=>0, 'project_id'=>$this->user['project_id']];
        $productCategory = Category::where($map)->get();
        $newCategory = NewsCategory::where($map)->get();
        $blogCategory = BlogCategory::where($map)->get();
        if (!empty($productCategory)){
            foreach ($productCategory as $item){$data["products"]["category"][] =$item;}
        }
        if (!empty($newCategory)){
            foreach ($newCategory as $item){$data["news"]["category"][] =$item;}
        }
        if (!empty($blogCategory)){
            foreach ($blogCategory as $item){$data["blogs"]["category"][] =$item;}
        }
        //返回
        return $this->success($data);
    }

    /**
     * @remark :保存html
     * @name   :savePublicTemplateHtml
     * @author :lyh
     * @method :post
     * @time   :2023/9/18 11:19
     */
    public function savePublicTemplateHtml(){
        $this->param['project_id'] = $this->user['project_id'];
        $this->param['test_model'] = 2;//未审核模版
        $this->param['operator_id'] = $this->user['manager_id'] ?? 0;
        $this->param['html'] = characterTruncation($this->param['html'],'/<style id="globalsojs-header">(.*?)<\/footer>/s');
        $this->param = $this->handleSaveParam($this->param);
        $publicTemplateModel = new Template();
        $rs = $publicTemplateModel->add($this->param);
        if($rs === false){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :获取详情模板详情
     * @name   :getDetail
     * @author :lyh
     * @method :post
     * @time   :2023/10/24 11:29
     */
    public function getDetail(){
        $bSettingModel = new Setting();
        $bSettingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        if($bSettingInfo === false){
            $this->fail('请先设置模板');
        }
        $is_custom = $this->param['is_custom'] ?? 0;//扩展模块详情模版
        $commonInfo = $this->getTypeCommonHtml($bSettingInfo['template_id'],$is_custom);
        //获取设置的默认中间部分
        $bTemplateMainModel = new BTemplateMain();
        $mainInfo = $bTemplateMainModel->read(['project_id'=>$this->user['project_id'],'type'=>$this->param['type']]);
        if($mainInfo === false){
            $main_html = $this->getModule($this->param['type']);
            $main_style = "<style id='globalsojs-styles'></style>";
        }else{
            $main_html = $mainInfo['main_html'];
            $main_style = $mainInfo['main_css'];
        }
        $html = $commonInfo['head_css'].$main_style.$commonInfo['footer_css'].$commonInfo['other'].
            $commonInfo['head_html'].$main_html.$commonInfo['footer_html'];
        $html = $this->getHeadFooter($html);//组装数据
        return $this->success($html);
    }

    /**
     * @remark :根据type获取html
     * @name   :getHeaderFooter
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 18:06
     */
    public function getTypeCommonHtml($template_id,$is_custom){
        //获取首页公共部分
        $templateCommonModel = new BTemplateCommon();
        $commonInfo = $templateCommonModel->read(['template_id'=>$template_id,'project_id'=>$this->user['project_id'],'type'=>1]);
        //判断当前项目是否有设置独立头部的权限
        if($is_custom == BTemplate::SOURCE_CUSTOM){
            return $this->success($commonInfo);
        }
        if(isset($this->user['configuration']['is_head']) && ($this->user['configuration']['is_head'] != 0)) {
            //有权限时,获取独立头部
            $commonTypeInfo = $templateCommonModel->read(['template_id'=>$template_id,'project_id'=>$this->user['project_id'],'type'=>$this->param['type']]);
            if($commonTypeInfo !== false){
                $commonInfo = $commonTypeInfo;
            }
        }
        return $this->success($commonInfo);
    }

    /**
     * @remark :保存详情模板数据
     * @name   :saveDetail
     * @author :lyh
     * @method :post
     * @time   :2023/10/24 11:53
     */
    public function saveDetail(){
        $bSettingModel = new Setting();
        $bSettingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        if($bSettingInfo === false){
            $this->fail('请先设置模板');
        }
        $is_custom = $this->param['is_custom'] ?? 0;//扩展模块详情模版
        $data = [
            'main_html'=>characterTruncation($this->param['html'],'/<main\b[^>]*>(.*?)<\/main>/s'),
            'main_css'=>characterTruncation($this->param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s'),
        ];
        $data['section_list_id'] = $this->param['section_list_id'];
        //保存中间部分
        $bTemplateMainModel = new BTemplateMain();
        $mainInfo = $bTemplateMainModel->read(['project_id'=>$this->user['project_id'],'type'=>$this->param['type'],'is_custom'=>$is_custom]);
        if($mainInfo === false){
            $data['project_id'] = $this->user['project_id'];
            $data['type'] = $this->param['type'];
            $data['is_custom'] = $is_custom;
            $bTemplateMainModel->add($data);
        }else{
            $bTemplateMainModel->edit($data,['id'=>$mainInfo['id']]);
        }
        $this->saveDetailCommonHtml($is_custom,$this->param['type'],$bSettingInfo['template_id'],$this->param['html']);
        return $this->success();
    }

    /**
     * @remark :保存详情页模版头部底部
     * @name   :saveDetailCommonHtml
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 18:12
     */
    public function saveDetailCommonHtml($is_custom,$type,$template_id,$html){
        $publicData = [
            'head_html' => characterTruncation($html,'/<header\b[^>]*>(.*?)<\/header>/s'),
            'head_css' => characterTruncation($html,'/<style id="globalsojs-header">(.*?)<\/style>/s'),
            'footer_html' => characterTruncation($html,'/<footer\b[^>]*>(.*?)<\/footer>/s'),
            'footer_css' => characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<\/style>/s'),
            'other'=>str_replace('<header','',characterTruncation($html,"/<link id=\"google-fonts-link\"(.*?)<header/s")),
        ];
        //查看当前模板是否有独立头部,有独立头部,更新独立头部,无独立头部,更新公共头部
        if($is_custom == BTemplate::SOURCE_CUSTOM){//扩展模块
            $this->user['configuration']['is_head'] = BTemplate::SOURCE_NO_CUSTOM;
        }
        $templateCommonModel = new BTemplateCommon();
        if(isset($this->user['configuration']['is_head']) && ($this->user['configuration']['is_head'] != 0)) {
            $templateCommonInfo = $templateCommonModel->read(['type'=>$type,'project_id'=>$this->user['project_id'],'template_id'=>$template_id]);
            if($templateCommonInfo === false){
                $publicData['type'] = $type;
                $publicData['project_id'] = $this->user['project_id'];
                $publicData['template_id'] = $template_id;
                $templateCommonModel->add($publicData);
            }else{
                $templateCommonModel->edit($publicData,['id'=>$templateCommonInfo['id']]);
            }
        }else{
            //更新首页头部底部
            $templateCommonModel->edit($publicData,['type'=>1,'project_id'=>$this->user['project_id'],'template_id'=>$template_id]);
        }
        return true;
    }
}