CustomModuleContentController.php 8.2 KB
<?php
/**
 * @remark :
 * @name   :CustomModuleContentController.php
 * @author :lyh
 * @method :post
 * @time   :2023/12/4 15:55
 */

namespace App\Http\Controllers\Bside\CustomModule;

use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\CustomModule\CustomModuleContentLogic;
use App\Models\CustomModule\CustomModule;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
use App\Models\User\User;

class CustomModuleContentController extends BaseController
{
    /**
     * @remark :获取自定义模块列表
     * @name   :ModuleList
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:43
     */
    public function lists(CustomModuleContent $customModuleContent){
        $this->request->validate([
            'module_id'=>['required'],
        ],[
            'module_id.required' => 'module_id不能为空',
        ]);
        $this->map['project_id'] = $this->user['project_id'];
        $lists = $customModuleContent->lists($this->map,$this->page,$this->row,$this->order = ['sort','id']);
        if(!empty($lists)){
            $template_id = $this->getModuleTemplateId($this->param['module_id']);
            $data = $this->getAllCategoryName();
            foreach ($lists['list'] as $k=>$v){
                $v['url'] = $this->getUrl($v);
                $v = $this->getHandleImageFile($v);
                $v['category_name'] = $this->categoryName($v['category_id'],$data);
                $v['operator_name'] = (new User())->getName($v['operator_id']);
                $v['is_renovation']  =  $this->getIsRenovation($v['module_id'],BTemplate::IS_DETAIL,$template_id,$v['id'],BTemplate::IS_CUSTOM);
                $lists['list'][$k] = $v;
            }
        }
        $this->response('success',Code::SUCCESS,$lists);
    }

    /**
     * @remark :扩展模块获取模版id
     * @name   :getTemplateId
     * @author :lyh
     * @method :post
     * @time   :2024/1/31 16:47
     */
    public function getModuleTemplateId($module_id){
        $template_id = 0;
        $moduleModel = new CustomModule();
        $info = $moduleModel->read(['id'=>$module_id]);
        if($info['detail_customized'] != 1){
            $bSettingModel = new Setting();
            $info = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
            $template_id = $info['template_id'];
        }
        return $this->success($template_id);
    }

    /**
     * @remark :获取时处理视频,图片,文件
     * @name   :getHandleImageFile
     * @author :lyh
     * @method :post
     * @time   :2024/1/31 15:48
     */
    public function getHandleImageFile($v){
        if(!empty($v['image'])){
            $v['image_link'] = getImageUrl($v['image'],$this->user['storage_type'] ?? 0,$this->user['project_location']);
        }
        if(!empty($v['video'])){
            $v['video']['url'] = getFileUrl($v['video']['url'],$this->user['storage_type'] ?? 0,$this->user['project_location']);
            $v['video']['video_image'] = getImageUrl($v['video']['video_image'],$this->user['storage_type'] ?? 0,$this->user['project_location']);
        }
        return $this->success($v);
    }

    /**
     * @remark :获取连接
     * @name   :getUrl
     * @author :lyh
     * @method :post
     * @time   :2024/1/29 14:56
     */
    public function getUrl($v){
        $routeMapModel = new RouteMap();
        if(!empty($v) && !empty($v['category_id'])){
            $categoryIdArr = $v['category_id'];
            $cate_id = (int)array_shift($categoryIdArr);
            $routeInfo = $routeMapModel->read(['source'=>RouteMap::SOURCE_MODULE_CATE,'source_id'=>$cate_id]);
            $v['path'] = ($routeInfo['route'] ?? '');
            if(!empty($v['path'])){
                $v['path'] = $v['path'].'/';
            }
        }
        return $this->user['domain'].$v['path'].$v['route'];
    }

    /**
     * @remark :获取所有分类名称
     * @name   :getAllCategoryName
     * @author :lyh
     * @method :post
     * @time   :2023/12/8 15:47
     */
    public function getAllCategoryName(){
        $categoryModel = new CustomModuleCategory();
        $list = $categoryModel->list(['status'=>0],'id',['id','name']);
        $data = [];
        foreach ($list as $v){
            $data[$v['id']] = $v['name'];
        }
        return $data;
    }

    /**
     * @remark :获取分类名称
     * @name   :categoryName
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:58
     */
    public function categoryName($category_id,$data){
        $category_name = '';
        if(!empty($category_id) && !empty($data)){
            foreach ($category_id as $v){
                if(isset($data[$v])){
                    $category_name .= $data[$v].',';
                }
            }
            $category_name = trim($category_name,',');
        }
        return $category_name;
    }

    /**
     * @remark :添加/编辑内容时获取分类
     * @name   :getCategoryList
     * @author :lyh
     * @method :post
     * @time   :2023/12/7 15:19
     */
    public function getCategoryList(){
        $categoryModel = new CustomModuleCategory();
        $list = $categoryModel->list(['project_id'=>$this->user['project_id'],'module_id'=>$this->param['module_id']],['id','name']);
        $menu = [];
        if(!empty($list)){
            foreach ($list as $v){
                if($v['pid'] == 0){
                    $v['sub'] = _get_child($v['id'],$list);
                    $menu[]   = $v;
                }
            }
        }
        $this->response('success',Code::SUCCESS,$menu);
    }

    /**
     * @remark :获取当前数据详情
     * @name   :info
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 16:09
     */
    public function info(CustomModuleContentLogic $logic){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $info = $logic->getContentInfo();
        $info = $this->getHandleImageFile($info);
        $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * @remark :保存数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:45
     */
    public function save(CustomModuleContentLogic $logic){
        $this->request->validate([
            'name'=>['required'],
            'route'=>['required'],
            'module_id'=>['required']
        ],[
            'name.required' => '分类名称不能为空',
            'route.required' => '分类路由不能为空',
            'module_id.required' => '所选模块id不能为空'
        ]);
        $logic->contentSave();
        $this->response('success');
    }

    /**
     * @remark :删除数据
     * @name   :del
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 16:14
     */
    public function del(CustomModuleContentLogic $logic){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $logic->contentDel();
        $this->response('success');
    }

    /**
     * @remark :排序
     * @name   :sort
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 17:46
     */
    public function sort(CustomModuleContentLogic $logic){
        $this->request->validate([
            'id'=>['required'],
            'sort'=>['required']
        ],[
            'id.required' => 'ID不能为空',
            'sort.required' => '排序不能为空',
        ]);
        $logic->contentSort();
        $this->response('success');
    }

    /**
     * @remark :批量排序
     * @name   :allSort
     * @author :lyh
     * @method :post
     * @time   :2024/1/11 9:46
     */
    public function allSort(CustomModuleContentLogic $logic){
        $logic->setAllSort();
        $this->response('success');
    }

    /**
     * @remark :复制扩展模块
     * @name   :copyModuleContent
     * @author :lyh
     * @method :post
     * @time   :2024/4/28 16:31
     */
    public function copyModuleContent(CustomModuleContentLogic $logic){
        $logic->copyModuleContentInfo();
        $this->response('success');
    }
}