VisualizationLogic.php 19.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
<?php
/**
 * @remark :
 * @name   :VisualizationLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/11/15 10:09
 */

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
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\BTemplateLog;
use App\Models\Template\BTemplateMain;
use App\Models\Template\Setting;
use App\Models\Template\Template;
use App\Models\Template\TemplateTypeMain;
use App\Models\Visualization\Visualization;

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

    /**
     * @remark :获取代码块
     * @name   :getVisualizationInfo
     * @author :lyh
     * @method :post
     * @time   :2023/11/17 14:44
     */
    public function getVisualizationInfo(){
        $data = $this->getSource($this->param['type']);
        $source = $data['source'];
        $source_id = $data['source_id'];
        $type = $this->getType($source,$source_id);
        $typeArray = [1,3,5,7];//单页数据
        if(in_array($type,$typeArray)){
            $bTemplateModel = new BTemplate();
            $info = $bTemplateModel->read(['source'=>$source,'source_id'=>$source_id,'template_id'=>0]);
            if($info === false){
                $html = '';
            }else{
                $html = $info['html'];
        }
        }else{//模块数据
            $bTemplateMainModel = new BTemplateMain();
            $info = $bTemplateMainModel->read(['type'=>$type]);
            if($info === false){
                $html = '';
            }else{
                $html = $info['main_html'];
            }
        }
        return $this->success(['html'=>$html]);
    }

    public function getSource($type){
        $source_id = 0;
        if ($type == 2){$source = 2;$source_id = 1;
        }elseif ($type == 3){$source = 2;
        }elseif ($type == 4){$source = 3;$source_id = 1;
        }elseif ($type == 5){$source = 3;
        }elseif ($type == 6){$source = 4;$source_id = 1;
        }elseif ($type == 7){$source = 4;
        }else{$source = 1;}
        return ['source'=>$source,'source_id'=>$source_id];
    }

    /**
     * @remark :保存定制html
     * @name   :saveHtml
     * @author :lyh
     * @method :post
     * @time   :2023/11/15 10:12
     */
    public function saveVisualization(){
        try {
            $sourceData = $this->getSource($this->param['type']);
            $source = $sourceData['source'];
            $source_id = $sourceData['source_id'];
            $type = $this->param['type'];
            $typeArray = [1,3,5,7];//单页数据
            if(in_array($type,$typeArray)){
                $bTemplateModel = new BTemplate();
                $templateInfo = $bTemplateModel->read(['source'=>$source,'source_id'=>$source_id,'template_id'=>0]);
                if($templateInfo === false){
                    $data = [
                        'html'=>$this->param['html'],
                        'project_id'=>$this->user['project_id'],
                        'source'=>$source,
                        'source_id'=>$source_id,
                    ];
                    $bTemplateModel->add($data);
                }else{
                    $bTemplateModel->edit(['html'=>$this->param['html']],['id'=>$templateInfo['id']]);
                }
            }else{//模块数据
                $bTemplateMainModel = new BTemplateMain();
                $mainInfo = $bTemplateMainModel->read(['type'=>$type]);
                if($mainInfo === false){
                    $mainData = [
                        'project_id'=>$this->user['project_id'],
                        'type'=>$type,
                        'main_html'=>$this->param['html']
                    ];
                    $bTemplateMainModel->add($mainData);
                }else{
                    $bTemplateMainModel->edit(['main_html'=>$this->param['html']],['id'=>$mainInfo['id']]);
                }
            }
        }catch (\Exception $e){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :定制页面支持可视化装修获取html
     * @name   :getHtml
     * @author :lyh
     * @method :post
     * @time   :2023/11/15 11:30
     */
    public function getHtml(){
        $type = $this->getType($this->param['source'],$this->param['source_id']);//获取类型
        $page_array = (array)$this->user['is_visualization']->page_array;//获取定制界面
        //查看当前类型是否是定制界面
        if(in_array($type,$page_array)){//是定制界面
            if(in_array($type,[1,3,5,7])){//单页
                $templateInfo = $this->getWebTemplate($this->param['source'],$this->param['source_id']);//查看当前定制单页是否有代码块
                if($templateInfo === false){
                    $this->fail('请先上传定制代码块');
                }
                return ['html'=>$templateInfo['html']];
            }else{//模块页
                $templateInfo = $this->getWebTemplate($this->param['source'],$this->param['source_id']);//查看当前页面是否可视化
                if($templateInfo === false){//获取代码块
                    $bTemplateMainModel = new BTemplateMain();
                    $mainInfo = $bTemplateMainModel->read(['type'=>$type]);
                    if($mainInfo === false){
                        $this->fail('请先上传定制代码块');
                    }
                    return ['html'=>$mainInfo['main_html']];
                }
                return ['html'=>$templateInfo['html']];
            }
        }else{//非定制界面
            $bSettingModel = new Setting();
            $settingInfo = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
            if($settingInfo === false){
                $this->fail('请先选择模版');
            }
            $templateInfo = $this->getWebTemplate($this->param['source'],$this->param['source_id'],$settingInfo['template_id']);//查看当前页面是否可视化
            if($templateInfo === false){
                //根据类型在获取中间部分
                $mainData = $this->getCommonMain($this->param['source'],$this->param['source_id']);
            }else{
                $mainData = [
                    'main_html'=>$templateInfo['main_html'],
                    'main_css'=>$templateInfo['main_css']
                ];
            }
            //获取公共头部底部
            $commonInfo = $this->getCommonPage($this->param['source'],$this->param['source_id'],$settingInfo['template_id']);
            //拼接数据
            $html = $commonInfo['head_css'].$mainData['main_css'].$commonInfo['footer_css'].$commonInfo['other'].
                $commonInfo['head_html'].$mainData['main_html'].$commonInfo['footer_html'];
            $html = $this->getHeadFooter($html);
            return ['html'=>$html,'template_id'=>$settingInfo['template_id']];
        }
    }

    /**
     * @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   :getWebTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:21
     */
    public function getWebTemplate($source,$source_id,$template_id = 0){
        //查询可视化是否第一次保存
        $bTemplateModel = new BTemplate();
        $param = [
            'source'=>$source,
            'project_id'=>$this->user['project_id'],
            'source_id'=>$source_id,
            'template_id'=>$template_id
        ];
        return $bTemplateModel->read($param);
    }

    /**
     * @remark :获取类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:20
     */
    public function getType($source,$source_id){
        $type = 1;
        if($source == 2){
            if($source_id == 0){$type = 3;}else{$type = 2;}
        }
        if($source == 3){
            if($source_id == 0){$type = 5;}else{$type = 4;}
        }
        if($source == 4){
            if($source_id == 0){$type = 7;}else{$type = 6;}
        }
        return $type;
    }

    /**
     * @remark :获取设置的类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/10/21 17:29
     */
    public function getSaveType($source,$source_id){
        $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 ($source == 2) {if ($source_id != 0) {if ($pageInfo['product_details'] != 0) {$type = 2;}} else {if ($pageInfo['product_list'] != 0) {$type = 3;}}}
                if ($source == 3) {if ($source_id != 0) {if ($pageInfo['blog_details'] != 0) {$type = 4;}} else {if ($pageInfo['blog_list'] != 0) {$type = 5;}}}
                if ($source == 4) {if ($source_id != 0) {if ($pageInfo['news_details'] != 0) {$type = 6;}} else {if ($pageInfo['news_list'] != 0) {$type = 7;}}}
                if ($source == 5) {if ($pageInfo['polymerization'] != 0) {$type = 8;}}
            }
        }
        return $type;
    }

    /**
     * @remark :保存定制项目可视化
     * @name   :saveHtml
     * @author :lyh
     * @method :post
     * @time   :2023/11/15 11:47
     */
    public function saveHtml(){
        if(!isset($this->user['is_visualization']->page_array)){
            $this->fail('当前为定制项目,请先选择定制界面');
        }
        $page_array = (array)$this->user['is_visualization']->page_array;
        $type = $this->getType($this->param['source'],$this->param['source_id']);
        try {
            if(in_array($type,$page_array)){//定制页面
                $bTemplateModel = new BTemplate();
                $templateInfo = $bTemplateModel->read([
                    'source'=>$this->param['source'],
                    'project_id'=>$this->user['project_id'],
                    'source_id'=>$this->param['source_id'],
                ]);
                if($templateInfo === false){
                    $this->param['project_id'] = $this->user['project_id'];
                    $bTemplateModel->add($this->param);
                }else{
                    $bTemplateModel->edit(['html'=>$this->param['html']],['source'=>$this->param['source'],'source_id'=>$this->param['source_id']]);
                }
            }else{
                $bTemplateModel = new BTemplate();
                $templateInfo = $bTemplateModel->read([
                    'source'=>$this->param['source'],
                    'project_id'=>$this->user['project_id'],
                    'source_id'=>$this->param['source_id'],
                    'template_id'=>$this->param['template_id'],
                ]);
                $this->param['main_html'] = characterTruncation($this->param['html'],'/<main\b[^>]*>(.*?)<\/main>/s');
                $this->param['main_css'] = characterTruncation($this->param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s');
                //保存头部
                $this->saveCommonTemplate($this->param['html'],$this->param['source'],$this->param['source_id'],$this->param['template_id']);
                if($templateInfo === false){
                    $this->param['project_id'] = $this->user['project_id'];
                    $bTemplateModel->add($this->param);
                }else{
                    $bTemplateModel->edit($this->param,['source'=>$this->param['source'],'source_id'=>$this->param['source_id']]);
                }
                $this->setTemplateLog($this->param['template_id'],$this->param['html'],$this->param['source'],$this->param['source_id']);
            }
        }catch (\Exception $e){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();

    }

    /**
     * @remark :生成记录
     * @name   :setTemplateLog
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:16
     */
    public function setTemplateLog($template_id,$html,$source,$source_id){
        $data = [
            'template_id'=>$template_id,
            'project_id'=>$this->user['project_id'],
            'operator_id'=>$this->user['id'],
            'text'=>$html,
            'source'=>$source,
            'source_id'=>$source_id,
            'main_html'=>characterTruncation($this->param['html'],'/<main\b[^>]*>(.*?)<\/main>/s'),
            'main_css'=>characterTruncation($this->param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s'),
            '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'));
        $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,$source,$source_id,$template_id){
        $type = $this->getSaveType($source,$source_id);
        $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'],'template_id'=>$template_id]);
        return $this->success();
    }

    /**
     * @remark :获取中间公共部分
     * @name   :getCommonMain
     * @author :lyh
     * @method :post
     * @time   :2023/10/24 15:58
     */
    public function getCommonMain($source,$source_id){
        $data = [];
        if ($source == 2) {if ($source_id != 0) {$type = 2;} else {$type = 3;}}
        if ($source == 3) {if ($source_id != 0) {$type = 4;} else {$type = 5;}}
        if ($source == 4) {if ($source_id != 0) {$type = 6;} else {$type = 7;}}
        if ($source == 5) {$type = 8;}
        //查询有没有公共详情模板
        $mainInfo = $this->model->read(['type'=>$type]);
        if($mainInfo === false){
            $data['main_html'] = $this->getModule($type);
            $data['main_css'] = "<style id='globalsojs-styles'></style>";
        }else{
            $data['main_html'] = $mainInfo['main_html'];
            $data['main_css'] = $mainInfo['main_css'];
        }
        return $data;
    }

    /**
     * @remark :默认产品模块
     * @name   :getProductModule
     * @author :lyh
     * @method :post
     * @time   :2023/7/27 15:08
     */
    public function getModule($type){
        //获取公共主题头部底部
        $mainModel = new TemplateTypeMain();
        $info = $mainModel->read(['type'=>$type]);
        return $info['main_html'];
    }

    /**
     * @remark :根据类型获取公共头和底
     * @name   :getCommonPage
     * @author :lyh
     * @method :post
     * @time   :2023/10/21 16:55
     */
    public function getCommonPage($source,$source_id,$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();
                $data = [
                    'template_id' => $template_id,
                    'project_id' => $this->user['project_id']
                ];
                if ($source == BTemplate::SOURCE_PRODUCT) {//产品页
                    if($source_id != 0){$data['type'] = 2;if ($pageInfo['product_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = 3;if ($pageInfo['product_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == BTemplate::SOURCE_BLOG) {//博客页
                    if ($source_id != 0) {$data['type'] = 4;if ($pageInfo['blog_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = 5;if ($pageInfo['blog_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == BTemplate::SOURCE_NEWS) {//新闻页
                    if ($source_id != 0) {$data['type'] = 6;if ($pageInfo['news_details'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
                    else {$data['type'] = 7;if ($pageInfo['news_list'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}}
                if ($source == BTemplate::SOURCE_KEYWORD) {//聚合页
                    $data['type'] = 8;if ($pageInfo['polymerization'] != 0) {$commonInfo = $commonTemplateModel->read($data);}}
            }
        }
        //获取首页公共的头部和底部
        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;
    }
}