BTemplateLogic.php 12.7 KB
<?php

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\BlogCategory;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Service\Service as ServiceSettingModel;
use App\Models\Template\BSetting;
use App\Models\Template\BTemplate;
use App\Models\Template\Template;

/**
 * @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'] = 0;
        $map['status'] = 0;
        $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(){
        $bSettingModel = new BSetting();
        $info = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        $TemplateInfo = [];
        if($info !== false){
            if($this->param['source'] == $this->model::SOURCE_HOME){//首页
                $TemplateInfo = $this->homeHtml($info,$this->param['source'],$this->param['source_id']);
            }
            if($this->param['source'] == $this->model::SOURCE_PRODUCT){//产品页
                $TemplateInfo = $this->productHtml($info,$this->param['source'],$this->param['source_id']);
            }
        }
        return $this->success($TemplateInfo);
    }

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

    /**
     * @remark :处理首页数据
     * @name   :homeHtml
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 16:36
     */
    public function homeHtml($info,$source,$source_id){
        //查看当前模板是否已编辑保存web_template
        $TemplateInfo = $this->webTemplateInfo($info['template_id'],$source,$source_id);
        if($TemplateInfo === false){
            //获取模板详情
            $ATemplateModel = new Template();
            $TemplateInfo = $ATemplateModel->read(['id'=>$info['template_id']]);
        }else{
            //渲染首页数据
            $ATemplateModel = new Template();
            $ATemplateInfo = $ATemplateModel->read(['id'=>$info['template_id']]);
            $TemplateInfo['name'] = $ATemplateInfo['name'];
            $TemplateInfo['image_link'] = getImageUrl($ATemplateInfo['image']);
        }
        $TemplateInfo['html'] = $this->getHeadFooter($TemplateInfo['html']);
        return $this->success($TemplateInfo);
    }

    /**
     * @remark :处理产品页数据
     * @name   :ProductHtml
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 16:40
     */
    public function productHtml($info,$source,$source_id){
        //查看当前模板是否已编辑保存web_template
        $TemplateInfo = $this->webTemplateInfo($info['template_id'],$source,$source_id);
        if($TemplateInfo === false){
            //获取首页数据
            $homeTemplateInfo = $this->webTemplateInfo($info['template_id'],1,0);
            if($homeTemplateInfo === false){
                $this->fail('请先装修首页');
            }
            $html = preg_replace('/<style id="globalsojs-styles">(.*?)<\/style>/s', "<style id='globalsojs-styles'></style>", $homeTemplateInfo['html']);
            $html = preg_replace('/<main\b[^>]*>(.*?)<\/main>/s', "<main>{$this->getProductModule()}</main>", $html);
        }else{
            $homeTemplateInfo = $this->webTemplateInfo($info['template_id'],1,0);
            if($homeTemplateInfo === false){
                $this->fail('请先装修首页');
            }
            $html = preg_replace('/<style id="globalsojs-styles">(.*?)<\/style>/s', $TemplateInfo['main_css'], $homeTemplateInfo['html']);
            $html = preg_replace('/<main\b[^>]*>(.*?)<\/main>/s',$TemplateInfo['main_html'] , $html);
        }
        $TemplateInfo['html'] = $this->getHeadFooter($html);//组装数据
        return $this->success($TemplateInfo);
    }

    /**
     * @remark :设置模板
     * @name   :setTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 9:47
     */
    public function setTemplate(){
        $bSettingModel = new BSetting();
        $info = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        if($info === false){
            $param = [
                'project_id'=>$this->user['project_id'],
                'template_id'=>$this->param['template_id'],
            ];
            $rs = $bSettingModel->add($param);
        }else{
            $rs = $bSettingModel->edit(['template_id'=>$this->param['template_id']],['project_id'=>$this->user['project_id']]);
        }
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @remark :保存修改后的模版
     * @name   :templateSave
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 11:05
     */
    public function templateSave(){
        //查询当前模版是否已保存
        $info = $this->webTemplateInfo($this->param['template_id'],$this->param['source'],$this->param['source_id']);
        if($info === false){
            //字符串截取
            $this->StringProcessing();
            $this->templateSaveParam($this->param['template_id']);
            $rs = $this->model->add($this->param);
        }else{
            $this->StringProcessing();
            $this->templateSaveParam($this->param['template_id']);
            $rs = $this->model->edit($this->param,['id'=>$info['id']]);
        }
        if($rs === false){
            $this->fail('error');
        }
        //通知更新
        $this->homeOrProduct($this->param['source'],$this->param['source_id']);
        return $this->success();
    }

    /**
     * @remark :通知首页更新
     * @name   :IsHome
     * @author :lyh
     * @method :post
     * @time   :2023/7/31 16:05
     */
    public function homeOrProduct($source,$source_id = ''){
        if($source == BTemplate::SOURCE_HOME){
            $type = 'index';
            $route = '';
        }elseif($source == BTemplate::SOURCE_PRODUCT){
            $type = RouteMap::SOURCE_PRODUCT;
            $productModel = new Product();
            $info = $productModel->read(['id'=>$source_id]);
            $route = $info['route'];
        }
        return $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>$type, 'route'=>$route]);
    }

    /**
     * @remark :处理保存模版数据
     * @name   :templateParam
     * @author :lyh
     * @method :post
     * @time   :2023/7/21 16:30
     */
    public function templateSaveParam($template_id){
        $this->param['project_id'] = $this->user['project_id'];
        if($this->param['source'] == 1){//首页
            $this->param['html'] =  characterTruncation($this->param['html'],'/<style id="globalsojs-header">(.*?)<\/footer>/s');
        }else{
            $this->param['html'] =  $this->param['main_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   :StringProcessing
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 15:35
     */
    public function StringProcessing(){
        //字符串截取
        $this->param['head_html'] = characterTruncation($this->param['html'],'/<header\b[^>]*>(.*?)<\/header>/s');
        $this->param['main_html'] = characterTruncation($this->param['html'],'/<main\b[^>]*>(.*?)<\/main>/s');
        $this->param['footer_html'] = characterTruncation($this->param['html'],'/<footer\b[^>]*>(.*?)<\/footer>/s');
        $this->param['head_css'] = characterTruncation($this->param['html'],'/<style id="globalsojs-header">(.*?)<\/style>/s');
        $this->param['main_css'] = characterTruncation($this->param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s');
        $this->param['footer_css'] = characterTruncation($this->param['html'],'/<style id="globalsojs-footer">(.*?)<\/style>/s');
    }



    /**
     * @remark :获取类型
     * @name   :getModuleType
     * @author :lyh
     * @method :any
     * @time   :2023/7/17 16:03
     */
    public function getModuleType(): array
    {
        //前端会回传:products:1,news:2,blogs:3,productCategory:4
        //定义数据结构
        $data = [
            "products"=>[
                "category"=>[
                    [
                        "id"=>"all",
                        "title"=>"全部",
                    ],
                    [
                        "id"=>"hot",
                        "title"=>"热销产品",
                    ],
                    [
                        "id"=>"recommend",
                        "title"=>"推荐产品",
                    ],
                ],
                "imageType"=>[
                    [
                        "id"=>1,
                        "title"=>"产品图片",
                    ],[
                        "id"=>2,
                        "title"=>"产品分类图片",
                    ],[
                        "id"=>3,
                        "title"=>"产品图标",
                    ]
                ],
            ],
            "news"=>[
                "category"=>[
                    [
                        "id"=>"all",
                        "name"=>"全部",
                    ],
                    [
                        "id"=>"new",
                        "name"=>"最新",
                    ],
                ],
            ],
            "blogs"=>[
                "category"=>[
                    [
                        "id"=>"all",
                        "name"=>"全部",
                    ],
                    [
                        "id"=>"new",
                        "name"=>"最新",
                    ],
                ],
            ]
        ];
        //产品,新闻,博客,一级分类数据
        $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 :默认产品模块
     * @name   :getProductModule
     * @author :lyh
     * @method :post
     * @time   :2023/7/27 15:08
     */
    public function getProductModule(){
        //获取公共主题头部底部
        $serviceSettingModel = new ServiceSettingModel();
        $info = $serviceSettingModel->read(['type'=>3]);
        return $info['values'];
    }
}