CustomModuleContentLogic.php 16.8 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
<?php
/**
 * @remark :
 * @name   :CustomModuleContentLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/12/4 16:06
 */

namespace App\Http\Logic\Bside\CustomModule;

use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\CustomModule\CustomModuleExtend;
use App\Models\CustomModule\CustomModuleExtentContent;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

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

    /**
     * @remark :获取当前数据详情
     * @name   :getCustomModuleInfo
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 16:10
     */
    public function getContentInfo(){
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        $info['extend'] = $this->getExtendInfo($info['module_id'],$info['id']);
        return $this->success($info);
    }

    /**
     * @remark :获取扩展字段详情
     * @name   :getExtendInfo
     * @author :lyh
     * @method :post
     * @time   :2023/11/14 9:45
     */
    public function getExtendInfo($module_id,$content_id){
        $extendModel = new CustomModuleExtend();
        $list = $extendModel->list(['module_id'=>$module_id],'id',['id','type','key','title']);
        if(empty($list)){
            return [];
        }
        $extendContentModel = new CustomModuleExtentContent();
        foreach ($list as $k=>$v){
            $info = $extendContentModel->read(['key'=>$v['key'],'content_id'=>$content_id]);
            if($info === false){
                if($v['type'] == 3 || $v['type'] == 4){
                    $v['values'] = [];
                }else{
                    $v['values'] = '';
                }
            }else{
                $v = $this->setTypValues($v,$info);
            }
            $list[$k] = $v;
        }
        return $this->success($list);
    }

    /**
     * @remark :扩展字段根据type返回类型
     * @name   :setTypValues
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 14:43
     */
    public function setTypValues($v,$info){
        if($v['type'] == 3){
            $arr = json_decode($info['values']);
            foreach ($arr as $k1=>$v1){
                $v1 = (array)$v1;
                $v1['url'] = getImageUrl($v1['url'],$this->user['storage_type'],$this->user['project_location']);
                $arr[$k1] = $v1;
            }
            $v['values'] = $arr;
        }elseif($v['type'] == 4){
            $arr1 = json_decode($info['values']);
            foreach ($arr1 as $k1=>$v1){
                $v1 = (array)$v1;
                $v1['url'] = getFileUrl($v1['url'] ?? $v1,$this->user['storage_type'],$this->user['project_location'],$this->user['file_cdn'] ?? 0);
                $arr1[$k1] = $v1;
            }
            $v['values'] = $arr1;
        }else{
            $v['values'] = $info['values'];
        }
        return $this->success($v);
    }

    /**
     * @remark :保存数据
     * @name   :ModuleSave
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:47
     */
    public function contentSave(){
        $extend = $this->handleExtent();
        $this->param = $this->handleParam($this->param);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $id = $this->contentEdit();
        }else{
            $id = $this->contentAdd();
        }
        //保存扩展字段
        $this->saveExtendInfo($id,$extend);
        return $this->success(['id'=>$id]);
    }

    /**
     * @remark :处理扩展字段
     * @name   :handleExtent
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 15:06
     */
    public function handleExtent(){
        //扩展字段
        $extend = [];
        if(!empty($this->param['extend'])){
            $extend = $this->param['extend'];
            unset($this->param['extend']);
        }
        return $extend;
    }

    /**
     * @remark :添加数据
     * @name   :contentAdd
     * @author :lyh
     * @method :post
     * @time   :2023/12/7 15:04
     */
    public function contentAdd(){
        //验证当前name是否已添加
        $info = $this->model->read(['name'=>$this->param['name'],'module_id'=>$this->param['module_id']]);
        if($info !== false){
            $this->fail('当前数据名称已存在');
        }
//        try {
            $this->param['sort'] = $this->setNewsSort();
            $id = $this->model->addReturnId($this->param);
            $route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_MODULE,
                $id, $this->user['project_id']);
            $this->addUpdateNotify(RouteMap::SOURCE_MODULE,$route);
            $this->curlDelRoute(['new_route'=>$route]);
            $this->edit(['route' => $route], ['id' => $id]);
//        }catch (\Exception $e){
//            $this->fail('系统错误,请联系管理员');
//        }
        return $id;
    }

    /**
     * @remark :设置最新产品的sort排序
     * @name   :setNewsSort
     * @author :lyh
     * @method :post
     * @time   :2023/12/25 9:27
     */
    public function setNewsSort(){
        $info = $this->model->orderBy('sort','desc')->first();
        if(empty($info)){
            return 1;
        }
        $sort = $info['sort']+1;
        return $sort;
    }

    /**
     * @remark :编辑数据
     * @name   :contentEdit
     * @author :lyh
     * @method :post
     * @time   :2023/12/7 15:04
     */
    public function contentEdit(){
        $is_upgrade = $this->param['is_upgrade'] ?? 0;//5.0数据 1:6.0
        $six_read = $this->param['six_read'] ?? 0;//是否按6.0显示
        if(!isset($this->param['category_id'])){
            $this->param['category_id'] = '';
        }
        if($is_upgrade == 0 || $six_read == 1) {
            $this->param['route'] = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_MODULE, $this->param['id'], $this->user['project_id']);
        }
        $this->editRoute($this->param['id'],$this->param['route']);
        $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('系统错误,请连续管理员');
        }
        return $this->param['id'];
    }

    /**
     * @name   :(参数处理)paramProcessing
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:30
     */
    public function handleParam($param)
    {
        $param['operator_id'] = $this->user['id'];
        if(!isset($param['id']) || empty($param['id'])){
            $param['project_id'] = $this->user['project_id'];
        }
        if(isset($param['category_id']) && !empty($param['category_id'])){
            $param['category_id'] = $this->getCategory($param['category_id']);
        }
        if(isset($param['image']) && !empty($param['image'])){
            $param['image'] = str_replace_url($param['image']);
        }
        if(isset($param['og_image'])){
            $param['og_image'] = str_replace_url($param['og_image'] ?? '');
        }
        if(isset($param['video'])){
            $param['video']['url'] = str_replace_url($param['video']['url']);
            $param['video']['video_image'] = str_replace_url($param['video']['video_image']);
            $param['video'] = Arr::a2s($param['video'] ?? []);
        }
        return $this->success($param);
    }

    /**
     * @remark :查看是否编辑路由
     * @name   :editCategoryRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 11:05
     */
    public function editRoute($id, $route)
    {
        //生成一条删除路由记录
        $info = $this->model->read(['id' => $id], ['id', 'route']);
        if($info !== false){
            $this->addUpdateNotify(RouteMap::SOURCE_MODULE,$route);
            $this->curlDelRoute(['old_route'=>$info['route'],'new_route'=>$route]);
        }
        return true;
    }

    /**
     * @remark :处理分类
     * @name   :getLastCategory
     * @author :lyh
     * @method :post
     * @time   :2023/10/20 9:02
     */
    public function getCategory($category){
        $str = '';
        foreach ($category as $v){
            $str .= $v.',';
        }
        return !empty($str) ? ','.$str : '';
    }

    /**
     * @remark :保存扩展字段
     * @name   :saveExtend
     * @author :lyh
     * @method :post
     * @time   :2023/11/9 15:02
     */
    public function saveExtendInfo($content_id,$extend){
        //先删除以前的数据
        $extendInfoModel = new CustomModuleExtentContent();
        $extendInfoModel->del(['content_id'=>$content_id]);
        if(empty($extend)) {
            return $this->success();
        }
        foreach ($extend as $v){
            if(empty($v['values'])){
                continue;
            }
            $v = $this->saveHandleExtend($v,$content_id);
            $extendInfoModel->add($v);
        }
        return $this->success();
    }

    /**
     * @remark :保存扩展字段时处理数据
     * @name   :saveHandleExtend
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 15:11
     */
    public function saveHandleExtend(&$v,$content_id){
        unset($v['title']);
        if($v['type'] == 3){
            foreach ($v['values'] as $k1=>$v1){
                $v1['url'] = str_replace_url($v1['url']);
                $v['values'][$k1] = $v1;
            }
            $v['values'] = json_encode($v['values']);
        }elseif ($v['type'] == 4){
            foreach ($v['values'] as $k1=>$v1){
                $v1['url'] = str_replace_url($v1['url']);
                $v['values'][$k1] = $v1;
            }
            $v['values'] = json_encode($v['values']);
        }
        $v['project_id'] = $this->user['project_id'];
        $v['content_id'] = $content_id;
        $v['module_id'] = $this->param['module_id'];
        return $this->success($v);
    }

    /**
     * @remark :删除数据
     * @name   :ModuleDel
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:47
     */
    public function contentDel(){
        DB::beginTransaction();
        try {
            foreach ($this->param['id'] as $id) {
                //删除当前扩展字段
                $this->delRoute($id);
                $this->delContentExtend($id);
                $this->model->del(['id' => $id]);
            }
            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :删除路由
     * @name   :delRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:50
     */
    public function delRoute($id)
    {
        RouteMap::delRoute(RouteMap::SOURCE_MODULE, $id, $this->user['project_id']);
        //通知
        $info = $this->model->read(['id' => $id], ['id', 'route','category_id']);
        $categoryIdArr = $info['category_id'];
        $cate_id = (int)array_shift($categoryIdArr);
        $routeMapModel = new RouteMap();
        $routeInfo = $routeMapModel->read(['source'=>RouteMap::SOURCE_MODULE_CATE,'source_id'=>$cate_id]);
        $path = ($routeInfo['route'] ?? '');
        $this->curlDelRoute(['old_route'=>$info['route'],'path'=>$path]);
        return $this->success();
    }

    /**
     * @remark :排序
     * @name   :contentSort
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 17:47
     */
    public function contentSort(){
        $this->model->edit(['sort' => $this->param['sort']], ['id'=>$this->param['id']]);
        return $this->success();
    }

    /**
     * @remark :
     * @name   :delContentExtend
     * @author :lyh
     * @method :post
     * @time   :2023/12/18 11:43
     */
    public function delContentExtend($id){
        $contentExtendModel = new CustomModuleExtentContent();
        $contentExtendModel->del(['content_id'=>$id]);
        return $this->success();
    }

    /**
     * @remark :设置批量排序
     * @name   :setAllSort
     * @author :lyh
     * @method :post
     * @time   :2024/1/10 15:40
     */
    public function setAllSort(){
        foreach ($this->param['data'] as $k => $v){
            $this->model->edit(['sort'=>$v['sort']],['id'=>$v['id']]);
        }
        return $this->success();
    }

    /**
     * @remark :复制扩展模块内容页
     * @name   :copyModuleContentInfo
     * @author :lyh
     * @method :post
     * @time   :2024/4/28 16:32
     */
    public function copyModuleContentInfo(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        $param = $this->setContentParams($info);
        $save_id = $this->model->insertGetId($param);
        $route = RouteMap::setRoute($param['route'], RouteMap::SOURCE_MODULE, $save_id, $this->user['project_id']);
        $this->edit(['route' => $route], ['id' => $save_id]);
        $this->copyTemplate($this->param['id'],$info['project_id'],$save_id,$info['module_id']);
        return $this->success(['id'=>$save_id]);
    }

    /**
     * @remark :字段处理
     * @name   :setContentParams
     * @author :lyh
     * @method :post
     * @time   :2024/4/28 16:33
     */
    public function setContentParams($info){
        return [
            'name'=>$info['name'].'-copy',
            'status'=>$info['status'],
            'sort'=>$info['sort'],
            'content'=>$info['content'],
            'remark'=>$info['remark'],
            'route'=>$info['route'],
            'image'=>$info['image'],
            'project_id'=>$info['project_id'],
            'operator_id'=>$this->user['id'],
            'module_id'=>$info['module_id'],
            'category_id'=>!empty($info['category_id']) ? ','.Arr::arrToSet($info['category_id']).',' : '',
            'video'=>Arr::a2s($info['video']),
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s')
        ];
    }

    /**
     * @remark :同步模版数据
     * @name   :copyTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 15:53
     */
    public function copyTemplate($id,$project_id,$save_id,$module_id){
        $BTemplateModel = new BTemplate();
        $list = $BTemplateModel->list(['source'=>$module_id,'source_id'=>$id,'is_list'=>BTemplate::IS_DETAIL,'is_custom'=>BTemplate::IS_CUSTOM,'project_id'=>$project_id]);
        if(!empty($list)){
            $data = [];
            foreach ($list as $v){
                $data[] = $this->setTemplateParams($v,$project_id,$save_id);
            }
            $rs = $BTemplateModel->insert($data);
            if($rs === false){
                $this->fail('error');
            }
        }
        return $this->success();
    }

    /**
     * @remark :组装模版数据
     * @name   :setTemplateParams
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 15:54
     */
    public function setTemplateParams($v,$project_id,$save_id){
        $param = [
            'html'=>$v['html'],
            'project_id'=>$project_id,
            'source'=>$v['source'],
            'source_id'=>$save_id,
            'template_id'=>$v['template_id'],
            'section_list_id'=>$v['section_list_id'],
            'main_html'=>$v['main_html'],
            'main_css'=>$v['main_css'],
            'is_custom'=>$v['is_custom'],
            'is_list'=>$v['is_list'],
            'type'=>$v['type'],
            'created_at'=>$v['created_at'],
            'updated_at'=>$v['updated_at']
        ];
        return $this->success($param);
    }

    /**
     * @remark :批量设置产品分类及状态
     * @name   :batchSetCategory
     * @author :lyh
     * @method :post
     * @time   :2023/8/15 17:53
     */
    public function batchSetCategory(){
        if(!isset($this->param['category_id']) || empty($this->param['category_id'])){
            $this->fail('请选择分类');
        }
        try {
            if(!isset($this->param['is_cover']) || ($this->param['is_cover'] == 1)){
                $category_ids = ','.implode(',',$this->param['category_id']).',';
                $this->model->edit(['category_id'=>$category_ids],['id'=>['in',$this->param['id']]]);
            }else{
                foreach ($this->param['id'] as $id){
                    //获取当前产品的分类
                    $contentInfo = $this->model->read(['id'=>$id],['id','category_id']);
                    $category_ids_arr = array_values(array_unique(array_merge($contentInfo['category_id'],$this->param['category_id'])));
                    $category_ids = ','.implode(',',$category_ids_arr).',';
                    $this->model->edit(['category_id'=>$category_ids],['id'=>$id]);
                }
            }
        }catch (\Exception $e){
            $this->fail('设置分类失败,请联系管理员');
        }
        return $this->success();
    }
}