CustomTemplateService.php 3.8 KB
<?php
/**
 * @remark :
 * @name   :CustomTemplateService.php
 * @author :lyh
 * @method :post
 * @time   :2024/3/11 16:15
 */

namespace App\Services\Html;

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\Setting;

class CustomTemplateService
{
    /**
     * @remark :获取当前自定义界面详情
     * @name   :customTemplateInfo
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 16:23
     */
    public function getHtml($projectInfo,$custom_id){
        $info = $this->model->read(['id'=>$custom_id]);
        if($info === false){
            return false;
        }
        if($info['is_visualization'] == 0 || $info['is_visualization'] == 1){
            $html = $this->getBodyHeaderFooter($projectInfo,$info['html'],$info['html_style']);
            $info['html'] = $this->getHeadFooter($html);
        }
        return ['html'=>$info['html']];
    }

    /**
     * @remark :获取body的详情
     * @name   :getBodyHeaderFooter
     * @author :lyh
     * @method :post
     * @time   :2023/7/21 18:08
     */
    public function getBodyHeaderFooter($projectInfo,$preg_html,$html_style){
        if(empty($preg_html)){
            $preg_html = "<main></main>";
            $html_style = "<style id='globalsojs-styles'></style>";
        }
        //获取设置的默认模版
        $bSettingModel = new Setting();
        $info = $bSettingModel->read(['project_id'=>$projectInfo['id']]);
        if($info === false){
            return false;
        }
        //获取type类型
        $commonInfo = $this->getCommonPage($projectInfo,$info['template_id']);
        $html = $commonInfo['head_css'].$html_style.$commonInfo['footer_css'].$commonInfo['other'].
            $commonInfo['head_html'].$preg_html.$commonInfo['footer_html'];
        return ['html'=>$html];
    }

    /**
     * @remark :根据类型获取公共头和底
     * @name   :getCommonPage
     * @author :lyh
     * @method :post
     */
    public function getCommonPage($projectInfo,$template_id){
        $is_head = $projectInfo['deploy_build']['configuration']['is_head'] ?? BTemplate::IS_NO_HEADER;
        if(isset($is_head) && ($is_head != 0)) {
            //查看页面是否设置自定义头部底部
            $pageSettingModel = new PageSetting();
            $pageInfo = $pageSettingModel->read(['project_id' => $projectInfo['id']]);
            if ($pageInfo !== false) {
                $commonTemplateModel = new BTemplateCommon();
                if ($pageInfo['page_list'] != 0) {
                    //使用独立头和底
                    $commonInfo = $commonTemplateModel->read(['template_id' => $template_id, 'project_id' => $projectInfo['id'], 'type' => 9]);
                }
            }
        }
        if(!isset($commonInfo) || $commonInfo === false){
            //获取首页公共的头部和底部
            $commonTemplateModel = new BTemplateCommon();
            $commonInfo = $commonTemplateModel->read(['template_id'=>$template_id,'project_id'=>$projectInfo['id'],'type'=>1]);
        }
        return $commonInfo;
    }

    /**
     * @remark :拼接获取公共头部底部
     * @name   :getHeadFooter
     * @author :lyh
     * @method :post
     */
    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;
    }
}