BTemplateLogic.php 16.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
<?php

namespace App\Http\Logic\Bside\BTemplate;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\BlogCategory;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Service\Service as ServiceSettingModel;
use App\Models\Template\BTemplateCommon;
use App\Models\Template\Setting;
use App\Models\Template\BTemplate;
use App\Models\Template\BTemplateLog;
use App\Models\Template\Template;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

/**
 * @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,$page,$row,$order = 'created_at',$filed = ['id','name','image','url','created_at','status','deleted_status']){
        $templateModel = new Template();
        $map['deleted_status'] = 0;
        $map['status'] = 0;
        $lists = $templateModel->lists($map,$page,$row,$order,$filed);
        return $this->success($lists);
    }

    /**
     * @remark :获取当前选择使用的模板
     * @name   :getModuleTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 9:44
     */
    public function getTemplate(){
        $bSettingModel = new Setting();
        $info = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        if($info === false){
            $this->fail('请先选择模版');
        }
        $TemplateInfo = [];
        if($this->param['source'] == $this->model::SOURCE_HOME){//首页
            $TemplateInfo = $this->homeHtml($info,$this->param['source'],$this->param['source_id']);
        }
        if($this->param['source'] == $this->model::SOURCE_PRODUCT){//产品页
            $TemplateInfo = $this->productHtml($info,$this->param['source'],$this->param['source_id']);
        }
        return $this->success($TemplateInfo);
    }

    /**
     * @remark :根据参数获取数据详情
     * @name   :webTemplateInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 16:41
     */
    public function webTemplateInfo($template_id,$source,$source_id){
        //查看当前模板是否已编辑保存web_template
        $TemplateInfo = $this->model->read([
            'template_id'=>$template_id,
            'source'=>$source,
            'project_id'=>$this->user['project_id'],
            'source_id'=>$source_id,
        ]);
        return $this->success($TemplateInfo);
    }

    /**
     * @remark :处理首页数据
     * @name   :homeHtml
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 16:36
     */
    public function homeHtml($info,$source,$source_id){
        //查看当前模板是否已编辑保存web_template
        $TemplateInfo = $this->webTemplateInfo($info['template_id'],$source,$source_id);
        if($TemplateInfo === false){
            //获取模板详情
            $ATemplateModel = new Template();
            $TemplateInfo = $ATemplateModel->read(['id'=>$info['template_id']]);
        }else{
            $commonTemplateModel = new BTemplateCommon();
            $commonInfo = $commonTemplateModel->read(['template_id'=>$info['template_id'],'project_id'=>$this->user['project_id'],'type'=>$source]);
            if($commonInfo !== false){
                $TemplateInfo['html'] = $commonInfo['head_css'].$TemplateInfo['main_css'].$commonInfo['footer_css'].$commonInfo['other'].
                    $commonInfo['head_html'].$TemplateInfo['main_html'].$commonInfo['footer_html'];
            }
            $ATemplateModel = new Template();
            $ATemplateInfo = $ATemplateModel->read(['id'=>$info['template_id']],['name','image']);
            $TemplateInfo['name'] = $ATemplateInfo['name'];
            $TemplateInfo['image_link'] = getImageUrl($ATemplateInfo['image']);
        }
        $TemplateInfo['html'] = $this->getHeadFooter($TemplateInfo['html']);
        return $this->success($TemplateInfo);
    }

    /**
     * @remark :处理产品页数据
     * @name   :ProductHtml
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 16:40
     */
    public function productHtml($info,$source,$source_id){
        $homeTemplateInfo = $this->webTemplateInfo($info['template_id'],1,0);
        if($homeTemplateInfo === false){
            $this->fail('请先装修首页');
        }
        //查看当前模板是否已编辑保存web_template
        $TemplateInfo = $this->webTemplateInfo($info['template_id'],$source,$source_id);
        if($TemplateInfo === false){
            $main_html = "<main>{$this->getProductModule()}</main>";
            $main_style = "<style id='globalsojs-styles'></style>";
        }else{
            $main_html = $TemplateInfo['main_html'];
            $main_style = $TemplateInfo['main_css'];
        }
        //兼容老数据
        $commonTemplateModel = new BTemplateCommon();
        $commonInfo = $commonTemplateModel->read(['template_id'=>$info['template_id'],'project_id'=>$this->user['project_id'],'type'=>1]);
        if($commonInfo !== false){
            $html = $commonInfo['head_css'].$main_style.$commonInfo['footer_css'].$commonInfo['other'].
                $commonInfo['head_html'].$main_html.$commonInfo['footer_html'];
        }else{
            $html = preg_replace('/<style id="globalsojs-styles">(.*?)<\/style>/s', $main_style, $homeTemplateInfo['html']);
            $html = preg_replace('/<main\b[^>]*>(.*?)<\/main>/s', $main_html, $html);
        }
        $TemplateInfo['html'] = $this->getHeadFooter($html);//组装数据
        return $this->success($TemplateInfo);
    }

    /**
     * @remark :设置模板
     * @name   :setTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 9:47
     */
    public function setTemplate(){
        $bSettingModel = new Setting();
        $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(){
        //演示项目,不允许其他号码编辑
        if(($this->user['project_id'] == 1) && (!in_array($this->user['mobile'],$this->model->mobile)) && ($this->param['source'] == 1)){
            $this->fail('演示项目仅支持演示功能,无法更改首页');
        }
        //查询当前模版是否已保存
        DB::beginTransaction();
        try {
            $info = $this->webTemplateInfo($this->param['template_id'],$this->param['source'],$this->param['source_id']);
            //字符串截取
            $this->param = $this->stringProcessing($this->param);
            $this->saveCommonTemplate($this->param);
            $this->param = $this->templateSaveParam($this->param);//组装数据
            if($info === false){
                $this->model->add($this->param);
            }else{
                $this->model->edit($this->param,['id'=>$info['id']]);
            }
            //写入操作模版记录
            $this->setTemplateLog($this->param);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('系统错误,请联系管理员');
        }
        //通知更新
        $this->homeOrProduct($this->param['source'],$this->param['source_id']);
        return $this->success();
    }

    /**
     * @remark :保存头部公共数据
     * @name   :saveCommonTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/10/13 14:27
     */
    public function saveCommonTemplate($param){
        $templateCommonModel = new BTemplateCommon();
        $info = $templateCommonModel->read(['template_id'=>$param['template_id'],'project_id'=>$this->user['project_id'],'type'=>1]);
        $data = [
            'head_html'=>$param['head_html'],
            'head_css'=>$param['head_css'],
            'footer_html'=>$param['footer_html'],
            'footer_css'=>$param['footer_css'],
            'other'=>str_replace('<header','',characterTruncation($param['html'],"/<link id=\"google-fonts-link\"(.*?)<header/s")),
        ];
        if($info === false){
            $data['template_id'] = $param['template_id'];
            $data['project_id'] = $this->user['project_id'];
            $data['type'] = $param['source'];
            $templateCommonModel->add($data);
        }else{
            $templateCommonModel->edit($data,['id'=>$info['id']]);
        }
        return $this->success();
    }

    /**
     * @remark :生成记录
     * @name   :setTemplateLog
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 11:16
     */
    public function setTemplateLog($param){
        $data = [
            'template_id'=>$param['template_id'],
            'project_id'=>$this->user['project_id'],
            'operator_id'=>$this->user['id'],
            'text'=>$param['html'],
            'source'=>$param['source'],
            'source_id'=>$param['source_id'],
            'head_html'=>$param['head_html'],
            'head_css'=>$param['head_css'],
            'main_html'=>$param['main_html'],
            'main_css'=>$param['main_css'],
            'footer_html'=>$param['footer_html'],
            'footer_css'=>$param['footer_css'],
        ];
        $bTemplateLogModel = new BTemplateLog();
        return $bTemplateLogModel->add($data);
    }

    /**
     * @remark :通知首页更新
     * @name   :IsHome
     * @author :lyh
     * @method :post
     * @time   :2023/7/31 16:05
     */
    public function homeOrProduct($source,$source_id = ''){
        if($source == BTemplate::SOURCE_HOME){
            $type = 'index';
            $route = '';
            //路由映射
            RouteMap::setRoute('index', RouteMap::SOURCE_PAGE, 0, $this->user['project_id']);
        }elseif($source == BTemplate::SOURCE_PRODUCT){
            $type = RouteMap::SOURCE_PRODUCT;
            $productModel = new Product();
            $info = $productModel->read(['id'=>$source_id]);
            $route = $info['route'];
        }
        return $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>$type, 'route'=>$route]);
    }

    /**
     * @remark :处理保存模版数据
     * @name   :templateParam
     * @author :lyh
     * @method :post
     * @time   :2023/7/21 16:30
     */
    public function templateSaveParam($param){
        $param['project_id'] = $this->user['project_id'];
        if($param['source'] == 1){//首页
            $param['html'] =  characterTruncation($param['html'],'/<style id="globalsojs-header">(.*?)<\/footer>/s');
        }else{
            $param['html'] =  $param['main_html'];
        }
        return $this->success($param);
    }

    /**
     * @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   :StringProcessing
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 15:35
     */
    public function stringProcessing($param){
        //字符串截取
        $param['head_html'] = characterTruncation($param['html'],'/<header\b[^>]*>(.*?)<\/header>/s');
        $param['main_html'] = characterTruncation($param['html'],'/<main\b[^>]*>(.*?)<\/main>/s');
        $param['footer_html'] = characterTruncation($param['html'],'/<footer\b[^>]*>(.*?)<\/footer>/s');
        $param['head_css'] = characterTruncation($param['html'],'/<style id="globalsojs-header">(.*?)<\/style>/s');
        $param['main_css'] = characterTruncation($param['html'],'/<style id="globalsojs-styles">(.*?)<\/style>/s');
        $param['footer_css'] = characterTruncation($param['html'],'/<style id="globalsojs-footer">(.*?)<\/style>/s');
        return $this->success($param);
    }

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

    /**
     * @remark :设置主题公共head
     * @name   :setHeadInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/10 15:32
     */
    public function setHeadFooterSave(){
        $serviceSettingModel = new ServiceSettingModel();
        DB::beginTransaction();
        try {
            $serviceSettingModel->del(['type'=>2]);
            $data = [
                ['type'=>2,'key'=>'head','values'=>$this->param['head'],'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')],
                ['type'=>2,'key'=>'footer','values'=>$this->param['footer'],'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')]
            ];
            $serviceSettingModel->insert($data);
            DB::commit();
        }catch (Exception $e){
            DB::rollBack();
            $this->fail('error');
        }
    }

    /**
     * @remark :获取主题公共head
     * @name   :getHeadInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/10 15:33
     */
    public function getHeadFooterList(){
        $serviceSettingModel = new ServiceSettingModel();
        $list = $serviceSettingModel->list(['type'=>2],'created_at');
        return $this->success($list);
    }


    /**
     * @remark :获取类型
     * @name   :getModuleType
     * @author :lyh
     * @method :any
     * @time   :2023/7/17 16:03
     */
    public function getModuleType(): array
    {
        //定义数据结构
        $data = $this->model->product_type;
        //产品,新闻,博客,一级分类数据
        $map = ['pid'=>0, 'project_id'=>$this->user['project_id']];
        $productCategory = Category::where($map)->get();
        $newCategory = NewsCategory::where($map)->get();
        $blogCategory = BlogCategory::where($map)->get();
        if (!empty($productCategory)){
            foreach ($productCategory as $item){
                $data["products"]["category"][] =$item;
            }
        }
        if (!empty($newCategory)){
            foreach ($newCategory as $item){
                $data["news"]["category"][] =$item;
            }
        }
        if (!empty($blogCategory)){
            foreach ($blogCategory as $item){
                $data["blogs"]["category"][] =$item;
            }
        }
        //返回
        return $this->success($data);
    }

    /**
     * @remark :保存html
     * @name   :savePublicTemplateHtml
     * @author :lyh
     * @method :post
     * @time   :2023/9/18 11:19
     */
    public function savePublicTemplateHtml(){
        $this->param['project_id'] = $this->user['project_id'];
        $this->param['operator_id'] = $this->user['manager_id'] ?? 0;
        $this->param = $this->stringProcessing($this->param);
        $publicTemplateModel = new Template();
        $rs = $publicTemplateModel->add($this->param);
        if($rs === false){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }
}