InitHtmlLogic.php 12.0 KB
<?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\RouteMap\RouteMap;
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\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:代表分类列表模块
        //获取设置的默认中间部分
        $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'];
        }
        $commonInfo = $this->getTypeCommonHtml($template_id,$this->param['type'],$is_custom,$is_list);        //获取头部
        $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 :拼接获取公共头部底部
     * @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 = [
                'main_html'=>characterTruncation($this->param['html'],'/<main\b[^>]*>(.*?)<\/main>/s'),
                '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 = [
                'main_html'=>characterTruncation($this->param['html'],'/<main\b[^>]*>(.*?)<\/main>/s'),
                '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->saveDetailCommonHtml($this->param['html'],$this->param['type'],$template_id,$is_custom,$is_list);
        $route = RouteMap::getRoute('all',0,$this->user['project_id']);
        $this->curlDelRoute(['route'=>$route,'new_route'=>$route]);
        return $this->success();
    }
    /**
     * @remark :保存详情页模版头部底部
     * @name   :saveDetailCommonHtml
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 18:12
     */
    public function saveDetailCommonHtml($html,$type,$template_id,$is_custom,$is_list){
        $publicData = $this->handleCommonParam($html);
        $templateCommonModel = new BTemplateCommon();
        //查看当前模板是否有独立头部,有独立头部,更新独立头部,无独立头部,更新公共头部
        $is_head = $this->user['configuration']['is_head'] ?? 0;
        if($is_custom == BTemplate::IS_CUSTOM){//todo::扩展模块无独立头部底部
            $is_head = BTemplate::IS_NO_HEADER;
        }
        if($is_head == BTemplate::IS_HEADER) {
            //有独立头部,更新独立头部
            $commonType = $this->getHeaderType($type,$is_list);
            $templateCommonInfo = $templateCommonModel->read(['project_id'=>$this->user['project_id'],'template_id'=>$template_id,'type'=>$commonType]);
            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'=>BTemplate::SOURCE_HOME,'project_id'=>$this->user['project_id'],'template_id'=>$template_id]);
        }
        return $this->success();
    }

    /**
     * @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   :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 :根据type获取头部html
     * @name   :getHeaderFooter
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 18:06
     */
    public function getTypeCommonHtml($template_id,$type,$is_custom,$is_list){
        //判断当前项目是否有设置独立头部的权限
        $is_head = $this->user['configuration']['is_head'] ?? 0;
        if($is_custom == BTemplate::IS_CUSTOM){//todo::拓展模块默认取首页
            $is_head = BTemplate::IS_NO_HEADER;
        }
        //获取首页公共部分
        $templateCommonModel = new BTemplateCommon();
        if($is_head == BTemplate::IS_HEADER) {
            //有独立头部,获取独立头部
            $commonType = $this->getHeaderType($type,$is_list);
            $commonInfo = $templateCommonModel->read(['template_id'=>$template_id,'project_id'=>$this->user['project_id'],'type'=>$commonType]);
            if($commonInfo !== false){
                return $this->success($commonInfo);
            }
        }
        //首页头底
        $commonInfo = $templateCommonModel->read(['template_id'=>$template_id,'project_id'=>$this->user['project_id'],'type'=>BTemplate::SOURCE_HOME]);
        return $this->success($commonInfo);
    }

    /**
     * @remark :独立头部获取头部底部类型
     * @name   :getHeaderType
     * @author :lyh
     * @method :post
     * @time   :2023/12/27 11:36
     */
    public function getHeaderType($type,$is_list){
        $resultType = BTemplate::SOURCE_HOME;
        if($type == BTemplate::SOURCE_PRODUCT){
            if($is_list == BTemplate::IS_LIST){
                $resultType = BTemplate::TYPE_PRODUCT_LIST;
            }else{
                $resultType = BTemplate::TYPE_PRODUCT_DETAIL;
            }
        }
        if($type == BTemplate::SOURCE_BLOG){
            if($is_list == BTemplate::IS_LIST){
                $resultType = BTemplate::TYPE_BLOG_LIST;
            }else{
                $resultType = BTemplate::TYPE_BLOG_DETAIL;
            }
        }
        if($type == BTemplate::SOURCE_NEWS){
            if($is_list == BTemplate::IS_LIST){
                $resultType = BTemplate::TYPE_NEWS_LIST;
            }else{
                $resultType = BTemplate::TYPE_NEWS_DETAIL;
            }
        }
        return $this->success($resultType);
    }

    /**
     * @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;
        $bTemplateMainModel = new BTemplateMain();
        $info = $bTemplateMainModel->read(['type'=>$this->param['type'],'is_list'=>$is_list]);
        if($info === false){
            $html = '';
        }else{
            $html = $info['main_html'];
        }
        return $this->success(['html'=>$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;
            $bTemplateMainModel = new BTemplateMain();
            $mainInfo = $bTemplateMainModel->read(['type'=>$this->param['type'],'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']
                ];
                $bTemplateMainModel->add($mainData);
            }else{
                $bTemplateMainModel->edit(['main_html'=>$this->param['html']],['id'=>$mainInfo['id']]);
            }
        }catch (\Exception $exception){
            $this->fail('保存失败,请联系开发人员');
        }
        return $this->success();
    }
}