CustomTemplateLogic.php 15.3 KB
<?php

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Project\PageSetting;
use App\Models\RouteMap\RouteMap;
use App\Models\Service\Service as ServiceSettingModel;
use App\Models\Template\BCustomTemplate;
use App\Models\Template\BTemplateCommon;
use App\Models\Template\BTemplateLog;
use App\Models\Template\Setting;
use App\Models\Template\BTemplate;

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

    /**
     * @remark :获取自定义模块列表
     * @name   :lists
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 15:46
     */
    public function customTemplateLists($map,$page,$row,$order = 'created_at'){
        $filed = ['id','name','status','url','title','keywords','description','project_id','is_upgrade','six_read','is_visualization','created_at','updated_at'];
        $map['deleted_status'] = 0;
        $map['project_id'] = $this->user['project_id'];
        $lists = $this->model->lists($map,$page,$row,$order,$filed);
        return $this->success($lists);
    }

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

    /**
     * @remark :保存自定义界面
     * @name   :customTemplateSave
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 16:21
     */
    public function customTemplateSave(){
        $this->param['url'] = str_replace_url($this->param['url']);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $is_upgrade = $this->param['is_upgrade'] ?? 0;//1:5.0数据 0:6.0
            $six_read = $this->param['six_read'] ?? 0;//5.0数据时,是否按6.0显示
            if($is_upgrade == 0 || $six_read == 1) {
                $this->param['url'] = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_PAGE, $this->param['id'], $this->user['project_id']);
            }
            $this->editCustomRoute($this->param['url']);
            $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        }else{
            if($this->param['url'] == $this->model::NOT_FOUND_PAGE_URL){
                $this->fail('404页面已存在');
            }
            $this->param['project_id'] = $this->user['project_id'];
            $id = $this->model->addReturnId($this->param);
            $route = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_PAGE, $id, $this->user['project_id']);
            $this->addUpdateNotify(RouteMap::SOURCE_PAGE,$route);
            $rs = $this->model->edit(['url'=>$route],['id'=>$id]);
        }
        if($rs === false){
            $this->fail('保存失败,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :可视化保存html
     * @name   :saveHtml
     * @author :lyh
     * @method :post
     * @time   :2023/9/16 17:13
     */
    public function saveHtml(){
        $html = $this->param['html'];
        $info = $this->model->read(['id'=>$this->param['id']],['id','is_visualization','url']);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        if($info['is_visualization'] == 0 || $info['is_visualization'] == 1){//非定制项目+可视化页面
            //获取设置的默认模版
            $bSettingModel = new Setting();
            $bSettingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
            if($bSettingInfo === false){
                $this->fail('请先选择模版');
            }
            $this->saveCommonTemplate($html,$bSettingInfo['template_id']);
            $this->param['html'] = characterTruncation($html,'/<main\b[^>]*>(.*?)<\/main>/s');
            $this->param['html_style'] = characterTruncation($html,'/<style id="globalsojs-styles">(.*?)<\/style>/s');
            $this->setTemplateLog($bSettingInfo['template_id'],$html,$this->param['id']);
        }
        $rs = $this->model->edit($this->param,['id'=>$this->param['id'],'project_id'=>$this->user['project_id']]);
        if($rs === false){
            $this->fail('系统错误,请联系管理');
        }
        //通知
        $this->addUpdateNotify(RouteMap::SOURCE_PAGE,$info['url']);
        $this->curlDelRoute(['route'=>$info['url'],'new_route'=>$info['url']]);
        return $this->success();
    }

    /**
     * @remark :生成记录
     * @name   :setTemplateLog
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:16
     */
    public function setTemplateLog($template_id,$html,$source_id){
        $data = [
            'template_id'=>$template_id,
            'project_id'=>$this->user['project_id'],
            'operator_id'=>$this->user['id'],
            'text'=>$html,
            'source'=>9,
            'source_id'=>$source_id,
            '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'),
            'main_html' => characterTruncation($html,'/<main\b[^>]*>(.*?)<\/main>/s'),
            'main_css' => characterTruncation($html,'/<style id="globalsojs-styles">(.*?)<\/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   :saveCommonTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/10/13 14:27
     */
    public function saveCommonTemplate($html,$template_id){
        $type = $this->getType();
        $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'),
        ];
        $footer_other = str_replace('<header','',characterTruncation($html,'/<style id="globalsojs-footer">(.*?)<header/s'));
        $other = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', '', $footer_other);
        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   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/10/21 17:29
     */
    public function getType(){
        $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 ($pageInfo['page_list'] != 0) {
                    $type = 9;
                }
            }
        }
        return $type;
    }

    /**
     * @remark :查看路由是否更新
     * @name   :editProduct
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:02
     */
    public function editCustomRoute($route){
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info['url'] == $this->model::NOT_FOUND_PAGE_URL){
            if($route != '404'){
                $this->fail('不可删除, 只可以修改tdk');
            }
        }
        $this->addUpdateNotify(RouteMap::SOURCE_PAGE,$route);
        $this->curlDelRoute(['route'=>$info['url'],'new_route'=>$route]);
        return true;
    }

    /**
     * @remark :删除自定义界面
     * @name   :customTemplateDel
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 16:21
     */
    public function customTemplateDel(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在或者已被删除');
        }
        if($info['url'] == $this->model::NOT_FOUND_PAGE_URL){
            $this->fail('404页面不可删除');
        }
        try {
            if($info['status'] != 2){
                $this->model->edit(['status'=>2],['id'=>$this->param['id']]);
            }else {
                $this->delRoute($this->param['id']);
                $this->model->del(['id' => $this->param['id']]);
            }
        }catch (\Exception $e){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :删除路由
     * @name   :delRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:50
     */
    public function delRoute($id)
    {
        //删除路由映射
        RouteMap::delRoute(RouteMap::SOURCE_PAGE, $id, $this->user['project_id']);
        //生成一条删除路由记录
        $info = $this->model->read(['id' => $id], ['id', 'url']);
        $this->curlDelRoute(['route'=>$info['url']]);
        return $this->success();
    }

    /**
     * @remark :获取body的详情
     * @name   :getBodyHeaderFooter
     * @author :lyh
     * @method :post
     * @time   :2023/7/21 18:08
     */
    public function getBodyHeaderFooter($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'=>$this->user['project_id']]);
        if($info === false){
            $this->fail('请先设置模版');
        }
        //获取type类型
        $commonInfo = $this->getCommonPage($info['template_id']);
        $html = $commonInfo['head_css'].$html_style.$commonInfo['footer_css'].$commonInfo['other'].
            $commonInfo['head_html'].$preg_html.$commonInfo['footer_html'];
        return $this->success($html);
    }

    /**
     * @remark :根据类型获取公共头和底
     * @name   :getCommonPage
     * @author :lyh
     * @method :post
     * @time   :2023/10/21 16:55
     */
    public function getCommonPage($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();
                if ($pageInfo['page_list'] != 0) {
                    //使用独立头和底
                    $commonInfo = $commonTemplateModel->read(['template_id' => $template_id, 'project_id' => $this->user['project_id'], 'type' => 9]);
                }
            }
        }
        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   :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   :getStatusNumber
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 17:40
     */
    public function getStatusNumber(){
        //三种状态 0:草稿 1:发布 2:回收站
        $data = ['dra'=>0,'pub'=>1,'del'=>2,'tal'=>3];
        foreach ($data as $k => $v){
            if($v == 3){
                $data[$k] = $this->model->where(['deleted_status'=>0,'project_id'=>$this->user['project_id']])->count();
            }else{
                $data[$k] = $this->model->where(['status'=>$v,'deleted_status'=>0,'project_id'=>$this->user['project_id']])->count();
            }
        }
        return $this->success($data);
    }

    /**
     * @remark :单页面还原
     * @name   :saveRollbackVersion
     * @author :lyh
     * @method :post
     * @time   :2024/4/23 14:00
     */
    public function saveRollbackVersion(){
        //获取当前数据记录详情
        $log = new BTemplateLog();
        $logInfo = $log->read(['id'=>$this->param['id']]);
        if($logInfo === false){
            $this->fail('未获取到当前记录');
        }
        //获取当前数据详情
        $info = $this->model->read(['id'=>$logInfo['source_id']],['is_visualization']);
        if($info['is_visualization'] == 0 || $info['is_visualization'] == 1){
            //还原头部底部
            $type = $this->getType();
            //还原头部+底部
            $commonData = [
                'head_html'=>$logInfo['head_html'],
                'head_css'=>$logInfo['head_css'],
                'footer_html'=>$logInfo['footer_html'],
                'footer_css'=>$logInfo['footer_css']
            ];
            $commonTemplateModel = new BTemplateCommon();
            $commonTemplateModel->edit($commonData,['template_id'=>$logInfo['template_id'],'type'=>$type,'project_id'=>$this->user['project_id']]);
        }
        $this->model->edit(['html'=>$logInfo['main_html'],'html_style'=>$logInfo['main_css']],['id'=>$logInfo['source_id']]);
        return $this->success();
    }
}