BTemplateLogLogic.php 5.6 KB
<?php
/**
 * @remark :
 * @name   :BTemplateLogLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/8/23 11:54
 */

namespace App\Http\Logic\Bside\BTemplate;

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

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

    /**
     * @remark :回滚版本
     * @name   :saveRollbackVersion
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:55
     */
    public function saveRollbackVersion(){
        //获取当前数据详情
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('error');
        }
        $bTemplateModel = new BTemplate();
        if($info['template_id'] == 0){//定制项目
            //TODO::还原头部+底部
            $bTemplateModel->edit(['html'=>$info['text']],['template_id'=>$info['template_id'],'source'=>$info['source'],'source_id'=>$info['source_id']]);
            return $this->success();
        }
        //演示项目,不允许其他号码编辑
        if(($this->user['project_id'] == 1) && (!in_array($this->user['mobile'],$bTemplateModel->mobile)) && ($info['source'] == 1)){
            $this->fail('演示项目仅支持演示功能,无法更改首页');
        }
        //获取当前项目的模版
        $bSettingModel = new Setting();
        $settingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        if($settingInfo === false){
            $this->fail('请先选择模版');
        }
        //切换模版
        if($info['template_id'] != $settingInfo['template_id']){
            $bSettingModel->edit(['template_id'=>$info['template_id']],['id'=>$settingInfo['id']]);
        }
        try {
            $data = $this->setParam($info);
            $bTemplateModel->edit($data,['template_id'=>$info['template_id'],'source'=>$info['source'],'source_id'=>$info['source_id']]);
            $commonData = $this->setCommonParam($info);
            $commonTemplateModel = new BTemplateCommon();
            $commonTemplateModel->edit($commonData,['template_id'=>$info['template_id'],'type'=>1,'project_id'=>$this->user['project_id']]);
            //更新所有界面的other
            if(!empty($info['other'])){
                $commonTemplateModel->edit(['other'=>$info['other']],['project_id'=>$this->user['project_id'],'template_id'=>$info['template_id']]);
            }else{
                $footer_other = str_replace('<header','',characterTruncation($info['text'],'/<style id="globalsojs-footer">(.*?)<header/s'));
                $other = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', '', $footer_other);
                $commonTemplateModel->edit(['other'=>$other],['project_id'=>$this->user['project_id'],'template_id'=>$info['template_id']]);
            }
        }catch (\Exception $e){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :设置回滚参数
     * @name   :setParam
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 14:18
     */
    public function setParam($info){
        $data = [
            'main_html'=>$info['main_html'],
            'main_css'=>$info['main_css'],
        ];
        return  $this->success($data);
    }

    /**
     * @remark :设置回滚公共参数参数
     * @name   :setParam
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 14:18
     */
    public function setCommonParam($info){
        $data = [
            'head_html'=>$info['head_html'],
            'head_css'=>$info['head_css'],
            'footer_html'=>$info['footer_html'],
            'footer_css'=>$info['footer_css']
        ];
        return  $this->success($data);
    }

    /**
     * @remark :获取数据详情
     * @name   :templateLogInfo
     * @author :lyh
     * @method :post
     * @time   :2023/9/19 14:42
     */
    public function templateLogInfo(){
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('当前数据不存在,或已被删除');
        }
        if($this->user['is_customized'] != BTemplate::IS_VISUALIZATION){
            if(empty($info['other'])){
                $footer_other = str_replace('<header','',characterTruncation($info['text'],'/<style id="globalsojs-footer">(.*?)<header/s'));
                $info['other'] = preg_replace('/<style id="globalsojs-footer">(.*?)<\/style>/s', '', $footer_other);
            }
            $html = $info['head_css'].$info['main_css'].$info['footer_css'].$info['other'].
                $info['head_html'].$info['main_html'].$info['footer_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'];
                }
            }
        }else{
            $page_array = (array)$this->user['is_visualization']->page_array;//获取所有定制界面
            if (in_array(1, $page_array)) {//首页是定制界面
                $html = $info['text'];
            }
        }
        return $this->success(['html'=>$html]);
    }

}