TemplateController.php 4.9 KB
<?php

namespace App\Http\Controllers\Bside;


use App\Enums\Common\Code;
use App\Http\Logic\Bside\TemplateLogic;
use App\Http\Requests\Bside\TemplateRequest;
use App\Models\Template\ATemplate;
use App\Models\Template\ATemplateHtml;
use App\Models\Template\BSetting;
use App\Models\Template\BTemplate;
use App\Models\Template\BTemplateData;
use Illuminate\Validation\Rule;


/**
 * 模板
 * @author:dc
 * @time 2023/5/9 14:00
 * Class TemplateController
 * @package App\Http\Controllers\Bside
 */
class TemplateController extends BaseController
{


    /**
     * 模板列表
     * @return \Illuminate\Http\JsonResponse
     * @author:dc
     * @time 2023/5/9 14:20
     */
    public function index(){

        $limit = intval($this->param['limit']??20);


        // 读取列表
        $data   =   ATemplate::_bAll($limit)->toArray();




        return $this->success($data);
    }


    /**
     * 当前使用的模板
     * @author:dc
     * @time 2023/5/9 15:19
     */
    public function info(){

        // 保存更新
        if($this->isPost()){
            $template_id = intval($this->param['template_id']??0);
            // 是否存在模板
            if($template_id && ATemplate::_bFind($template_id)){
                BSetting::_save($this->user['project_id'],$template_id);
            }else{
                return $this->response('无法使用不存在的模板',Code::SYSTEM_ERROR);
            }
        }

        // 读取我的模板
        $conf = BSetting::_get($this->user['project_id']);
        // 读取模板信息
        $data = ATemplate::_bFind($conf['template_id']);

        return $this->success([
            'template_id'    =>  $data['id']??0,
            'name'    =>  $data['name']??'',
            'thumb'    =>  $data['thumb']??'',
            'time'  =>  $conf['time']
        ]);
    }


    /**
     * 保存模板
     * @author:dc
     * @time 2023/5/10 10:53
     */
    public function save(){

        $html = '<header id="globalso-header" class="web_head sticky-top py-1 py-md-0" style="background-color: #318fff;">asdf</header>';

        // 替换 header
        $html = preg_replace("/<header(.*)id=\"globalso-header\"(.*)>([\s\S]*)<\/header>/iU",'',$html);
        $html = preg_replace("/<main(.*)id=\"globalso-main\"(.*)>([\s\S]*)<\/main>/iU",'',$html);
        $html = preg_replace("/<footer(.*)id=\"globalso-footer\"(.*)>([\s\S]*)<\/footer>/iU",'',$html);






    }


    /**
     * 数据源
     * @return \Illuminate\Http\JsonResponse
     * @author:dc
     * @time 2023/5/11 10:47
     */
    public function get_type(){
        return $this->success(ATemplateHtml::$sourceMap);
    }


    /**
     * 获取 编辑html
     * @author:dc
     * @time 2023/5/11 9:33
     */
    public function get_html(){
        $source =   $this->param['source']??'';
        $source_id  =   $this->param['source_id']??0;

        $data = TemplateLogic::instance()->first($source,$source_id);


        return $this->response('',Code::SUCCESS,['html'=>$data['html']]);

    }

    /**
     * 保存
     * @author:dc
     * @time 2023/5/11 11:00
     */
    public function save_html(TemplateRequest $request){

        $data   =   $request->validated();

        $data['data_source']    =   $data['source'];
        $data['data_source_id']    =   $data['source_id'];

        unset($data['source']);
        unset($data['source_id']);

        TemplateLogic::instance()->save($data);

        return $this->response('保存成功');

    }

    /**
     * 状态修改
     * @param TemplateRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\AsideGlobalException
     * @throws \App\Exceptions\BsideGlobalException
     * @author:dc
     * @time 2023/5/15 14:43
     */
    public function status(TemplateRequest $request){

        $data   =   $request->validated();

        TemplateLogic::instance()->status($data['source'],$data['source_id'],$data['status']);

        return $this->response('保存成功');

    }


    /**
     * 自定义块
     * @author:dc
     * @time 2023/5/10 14:55
     */
    public function customChunk(){
//
//        $html   =   $this->param['html']??[];
//        // 那个页面 的
//        $type   =   $this->param['type']??'';
//
//        if(!is_array($html)){
//            return $this->response('参数异常',Code::SYSTEM_ERROR);
//        }
//
//        // 项目id
//        $project_id =   $this->user['project_id'];
//        // 当前模板
//        $template_id = BSetting::_get($project_id)['template_id'];
//
//        // 验证这个模板是否存在
//        if(!$type || !ATemplateHtml::_typeExist($template_id,$type)){
//            return $this->response('页面类型错误',Code::SYSTEM_ERROR);
//        }
//
//
//        $html =  view("template.{$template_id}.{$type}")->render();
//
//
//        return $this->response('',Code::SUCCESS,$html);
////        $data   =   BTemplateData::_insert();
//
//


    }






}