ReplaceHtmlLogic.php 3.1 KB
<?php
/**
 * @remark :
 * @name   :ReplaceHtmlLogic.php
 * @author :lyh
 * @method :post
 * @time   :2024/5/8 10:03
 */

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Template\BTemplate;
use App\Models\Template\TemplateReplaceHtml;
use App\Models\Template\TemplateReplaceHtmlLog;

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

    /**
     * @remark :替换可视化的html代码(按类型)
     * @name   :replaceHtml
     * @author :lyh
     * @method :post
     * @time   :2024/5/7 15:52
     */
    public function replaceHtml(){
        $type = $this->getCustomizedType($this->param['type'], $this->param['is_list']);//获取定制界面类型
        //查看当前页面是否定制,是否开启可视化
        $page_array = (array)$this->user['is_visualization']->page_array;//获取所有定制界面
        if (in_array($type, $page_array)) {//当前页面是定制界面
            $this->param['template_id'] = 0;
        }
        $replaceId = $this->saveReplaceHtml($this->param);
        //TODO::生成一条任务记录
        //查询当前所有装修的
        $condition = ['source'=>$this->param['type'],'is_custom'=>$this->param['is_custom'],'is_list'=>$this->param['is_list'],
            'template_id'=>$this->param['template_id']];
        $list = $this->model->list($condition);
        foreach ($list as $v){
            if($v['type'] == 0){
                $main_html = str_replace($this->param['old_html'],$this->param['html'],$v['main_html']);
                $this->model->edit(['main_html'=>$main_html],['id'=>$v['id']]);
            }else{
                $html = str_replace($this->param['old_html'],$this->param['html'],$v['html']);
                $this->model->edit(['html'=>$html],['id'=>$v['id']]);
            }
            $this->saveReplaceHtmlLog($replaceId,$v['id']);
        }
        return $this->success();
    }

    /**
     * @remark :生成一条记录
     * @name   :saveTemplateLog
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 9:23
     */
    public function saveReplaceHtml($data,$template_id){
        $logData = [
            'type'=>$data['type'],
            'is_custom'=>$data['is_custom'],
            'is_list'=>$data['is_list'],
            'template_id'=>$template_id,
            'old_html'=>$data['old_html'],
            'html'=>$data['html'],
        ];
        return $this->model->addReturnId($logData);
    }

    /**
     * @remark :保存每条替换记录
     * @name   :saveReplaceHtmlLog
     * @author :lyh
     * @method :post
     * @time   :2024/5/8 9:37
     */
    public function saveReplaceHtmlLog($replace_id,$replace_template_id){
        $logData = [
            'replace_id'=>$replace_id,
            'replace_template_id'=>$replace_template_id,
            'uid'=>$this->user['manager_id'],
        ];
        $replaceHtmlModel = new TemplateReplaceHtmlLog();
        return $replaceHtmlModel->add($logData);
    }
}