VisualizationLogic.php 15.2 KB
<?php
/**
 * @remark :
 * @name   :VisualizationLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/11/15 10:09
 */

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Project\PageSetting;
use App\Models\Service\Service as ServiceSettingModel;
use App\Models\Template\BTemplate;
use App\Models\Template\BTemplateCommon;
use App\Models\Template\BTemplateMain;
use App\Models\Template\Setting;
use App\Models\Template\Template;
use App\Models\Template\TemplateTypeMain;
use App\Models\Visualization\Visualization;

class VisualizationLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new Visualization();
        $this->param = $this->requestAll;
    }

    /**
     * @remark :保存定制html
     * @name   :saveHtml
     * @author :lyh
     * @method :post
     * @time   :2023/11/15 10:12
     */
    public function saveVisualization(){
        try {
            if(isset($this->param['id']) && !empty($this->param['id'])){
                $this->model->edit($this->param,['id'=>$this->param['id']]);
            }else{
                $this->param['project_id'] = $this->user['project_id'];
                $this->model->add($this->param);
            }
        }catch (\Exception $e){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :定制页面支持可视化装修获取html
     * @name   :getHtml
     * @author :lyh
     * @method :post
     * @time   :2023/11/15 11:30
     */
    public function getHtml(){
        $TemplateInfo = $this->getWebTemplate($this->param['source'],$this->param['source_id']);
        $type = $this->getType($this->param['source'],$this->param['source_id']);
        $page_array = (array)$this->user['is_visualization']->page_array;
        if($TemplateInfo === false){
            if(in_array($type,$page_array)){//定制页面
                $info = $this->model->read(['source'=>$type],['html','source','id','project_id']);
                if($info === false){
                    $this->fail('请先上传定制代码块');
                }
                return $info['html'];
            }else{//非定制界面
                $bSettingModel = new Setting();
                $settingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
                if($settingInfo === false){
                    $this->fail('请先选择模版');
                }
                //获取公共头部底部
                $commonInfo = $this->getCommonPage($this->param['source'],$this->param['source_id'],$settingInfo['template_id']);
                if($commonInfo === false){
                    //保存一次首页头部底部
                    $ATemplateModel = new Template();
                    $aTemplateInfo = $ATemplateModel->read(['id'=>$settingInfo['template_id']]);
                    $commonInfo = $this->saveHomeHeaderFooter($settingInfo['template_id'],$aTemplateInfo);
                }
                //根据类型在获取中间部分
                $mainData = $this->getCommonMain($this->param['source'],$this->param['source_id']);
                //拼接数据
                $html = $commonInfo['head_css'].$mainData['main_css'].$commonInfo['footer_css'].$commonInfo['other'].
                    $commonInfo['head_html'].$mainData['main_html'].$commonInfo['footer_html'];
                $html = $this->getHeadFooter($html);
                return ['html'=>$html,'template_id'=>$settingInfo['template_id']];
            }
        }else{
            if($TemplateInfo['template_id'] != 0){
                $bSettingModel = new Setting();
                $settingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
                if($settingInfo === false){
                    $this->fail('请先选择模版');
                }
                //获取头部信息
                $commonInfo = $this->getCommonPage($this->param['source'],$this->param['source_id'],$settingInfo['template_id']);
                $html = $commonInfo['head_css'].$TemplateInfo['main_css'].$commonInfo['footer_css'].$commonInfo['other'].
                    $commonInfo['head_html'].$TemplateInfo['main_html'].$commonInfo['footer_html'];
                $html = $this->getHeadFooter($html);
                return ['html'=>$html,'template_id'=>$settingInfo['template_id']];
            }else{
                return $TemplateInfo['html'];
            }
        }
    }

    /**
     * @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   :getWebTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:21
     */
    public function getWebTemplate($source,$source_id){
        //查询可视化是否第一次保存
        $bTemplateModel = new BTemplate();
        return $bTemplateModel->read([
            'source'=>$source,
            'project_id'=>$this->user['project_id'],
            'source_id'=>$source_id,
        ]);
    }

    /**
     * @remark :获取类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:20
     */
    public function getType($source,$source_id){
        $type = 1;
        if($source == 2){
            if($source_id == 0){$type = 3;}else{$type = 2;}
        }
        if($source == 3){
            if($source_id == 0){$type = 5;}else{$type = 4;}
        }
        if($source == 4){
            if($source_id == 0){$type = 7;}else{$type = 6;}
        }
        return $type;
    }

    /**
     * @remark :获取设置的类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/10/21 17:29
     */
    public function getSaveType($source,$source_id){
        $type = 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 == 2) {if ($source_id != 0) {if ($pageInfo['product_details'] != 0) {$type = 2;}} else {if ($pageInfo['product_list'] != 0) {$type = 3;}}}
                if ($source == 3) {if ($source_id != 0) {if ($pageInfo['blog_details'] != 0) {$type = 4;}} else {if ($pageInfo['blog_list'] != 0) {$type = 5;}}}
                if ($source == 4) {if ($source_id != 0) {if ($pageInfo['news_details'] != 0) {$type = 6;}} else {if ($pageInfo['news_list'] != 0) {$type = 7;}}}
                if ($source == 5) {if ($pageInfo['polymerization'] != 0) {$type = 8;}}
            }
        }
        return $type;
    }

    /**
     * @remark :保存定制项目可视化
     * @name   :saveHtml
     * @author :lyh
     * @method :post
     * @time   :2023/11/15 11:47
     */
    public function saveHtml(){
        $page_array = (array)$this->user['is_visualization']->page_array;
        $type = $this->getType($this->param['source'],$this->param['source_id']);
        if(in_array($type,$page_array)){//定制页面
            $bTemplateModel = new BTemplate();
            $templateInfo = $bTemplateModel->read([
                'source'=>$this->param['source'],
                'project_id'=>$this->user['project_id'],
                'source_id'=>$this->param['source_id'],
            ]);
        }else{
            $bTemplateModel = new BTemplate();
            $templateInfo = $bTemplateModel->read([
                'source'=>$this->param['source'],
                'project_id'=>$this->user['project_id'],
                'source_id'=>$this->param['source_id'],
                'template_id'=>$this->param['template_id'],
            ]);
            $this->param['main_html'] = characterTruncation($this->param['html'],'/<main\b[^>]*>(.*?)<\/main>/s');
            $this->param['main_css'] = characterTruncation($this->param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s');
            //保存头部
            $this->saveCommonTemplate($this->param['html'],$this->param['source'],$this->param['source_id'],$this->param['template_id']);
        }
        try {
            if($templateInfo === false){
                $this->param['project_id'] = $this->user['project_id'];
                $bTemplateModel->add($this->param);
            }else{
                $bTemplateModel->edit($this->param,['source'=>$this->param['source'],'source_id'=>$this->param['source_id']]);
            }
        }catch (\Exception $e){
            $this->fail('系统错误请联系管理员');
        }
        return $this->success();

    }

    /**
     * @remark :保存头部公共数据
     * @name   :saveCommonTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/10/13 14:27
     */
    public function saveCommonTemplate($html,$source,$source_id,$template_id){
        $type = $this->getSaveType($source,$source_id);
        $templateCommonModel = new BTemplateCommon();
        $info = $templateCommonModel->read(['template_id'=>$template_id,'project_id'=>$this->user['project_id'],'type'=>$type]);
        $data = [
            '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($info === false){
            $data['template_id'] = $template_id;
            $data['project_id'] = $this->user['project_id'];
            $data['type'] = $type;
            $templateCommonModel->add($data);
        }else{
            $templateCommonModel->edit($data,['id'=>$info['id']]);
        }
        //更新所有界面的other
        $templateCommonModel->edit(['other'=>$other],['project_id'=>$this->user['project_id']]);
        return $this->success();
    }

    /**
     * @remark :获取中间公共部分
     * @name   :getCommonMain
     * @author :lyh
     * @method :post
     * @time   :2023/10/24 15:58
     */
    public function getCommonMain($source,$source_id){
        $data = [];
        if ($source == 2) {if ($source_id != 0) {$type = 2;} else {$type = 3;}}
        if ($source == 3) {if ($source_id != 0) {$type = 4;} else {$type = 5;}}
        if ($source == 4) {if ($source_id != 0) {$type = 6;} else {$type = 7;}}
        if ($source == 5) {$type = 8;}
        //查询有没有公共详情模板
        $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   :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 :根据类型获取公共头和底
     * @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 == 2) {//产品页
                    if($source_id != 0){$data['type'] = 2;if ($pageInfo['product_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = 3;if ($pageInfo['product_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == 3) {//博客页
                    if ($source_id != 0) {$data['type'] = 4;if ($pageInfo['blog_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = 5;if ($pageInfo['blog_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == 4) {//新闻页
                    if ($source_id != 0) {$data['type'] = 6;if ($pageInfo['news_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = 7;if ($pageInfo['news_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == 5) {//聚合页
                    $data['type'] = 8;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'=>1]);
        }
        return $commonInfo;
    }

    /**
     * @remark :保存首页头部底部信息
     * @name   :saveHomeHeaderFooter
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:14
     */
    public function saveHomeHeaderFooter($template_id,$aTemplateInfo){
        $data = [
            'other'=>str_replace('<header','',characterTruncation($aTemplateInfo['html'],"/<link id=\"google-fonts-link\"(.*?)<header/s")),
            'head_html'=>$aTemplateInfo['head_html'],
            'footer_html'=>$aTemplateInfo['footer_html'],
            'head_css'=>$aTemplateInfo['head_css'],
            'footer_css'=>$aTemplateInfo['footer_css'],
            'template_id'=>$template_id,
            'project_id'=>$this->user['project_id'],
            'type'=>1,
        ];
        //保存首页头部信息
        $templateCommonModel = new BTemplateCommon();
        $templateCommonModel->add($data);
        return $this->success($data);
    }
}