BTemplateLogic.php 3.7 KB
<?php

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Template\Template;
use App\Models\Template\BSetting;
use App\Models\Template\BTemplate;

/**
 * @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,$order = 'created_at',$filed = ['*']){
        $templateModel = new Template();
        $lists = $templateModel->list($map,$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){
            //查看当前模板是否已编辑保存
            $TemplateInfo = $this->model->read(['template_id'=>$info['template_id'],'project_id'=>$this->user['project_id']]);
            if($TemplateInfo === false){
                //获取模板详情
                $ATemplateModel = new Template();
                $TemplateInfo = $ATemplateModel->read(['id'=>$info['template_id']]);
            }
        }
        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(){
        //字符串截取
        $this->param['head_html'] = characterTruncation($this->param['html'],'<header>','</header>');
        $this->param['main_html'] = characterTruncation($this->param['html'],'<main>','</main>');
        $this->param['footer_html'] = characterTruncation($this->param['html'],'<footer>','</footer>');
        $this->param['head_css'] = characterTruncation($this->param['html'],'<style id="vvvebjs-header">','</style>');
        $this->param['main_css'] = characterTruncation($this->param['html'],'<style id="vvvebjs-style">','</style>');
        $this->param['footer_css'] = characterTruncation($this->param['html'],'<style id="vvvebjs-footer">','</style>');
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        }else{
            $this->param['project_id'] = $this->user['project_id'];
            $rs = $this->model->add($this->param);
        }
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }
}