Răsfoiți Sursa

随机模板

15313670163 4 luni în urmă
părinte
comite
cc31ed7e34

+ 431 - 1
app/JsonRpc/PublicRpcService.php

@@ -2920,6 +2920,436 @@ class PublicRpcService implements PublicRpcServiceInterface
         }
         return Result::success($sector);
     }
+    /**
+     * 自助建站-随机获取模板
+     */
+    public function randomWebTemplate(array $data): array
+    {
+        $web = Website::where('id',$data['website_id'])->first();
+        if(empty($web)){
+            return Result::error('此网站不存在!');
+        }
+        $template = Template:: where('template_id',$data['template_id'])->first();
+        if(empty($template)){
+            return Result::error('此皮肤不存在!');
+        }
+        $rule = TemplateRule::first();
+        $index_rule = json_decode($rule['index_rule']);
+        $class_rule = json_decode($rule['class_rule']);
+        // return Result::success($index_rule);
+        $tempalte['index'] = $this->randomPage($data,$data['page'],$index_rule);
+        // $tempalte['class'] = $this->randomPage($data,2,$class_rule);
+        // $tempalte['list'] = $this->randomPage($data,3);
+        // $tempalte['article'] = $this->randomPage($data,4);
+        // $tempalte['search'] = $this->randomPage($data,5);
+        // $tempalte['aloneList'] = $this->randomPage($data,6);
+        // $tempalte['aloneArticle'] = $this->randomPage($data,7);
+        // $index = $this->randomPage($data);
+        return Result::success($tempalte);
+    }
+    public function randomPage($data,$page,$rule){
+        // return $rule;
+        $sectors = Sector::where('sector.template_id',$data['template_id'])
+        ->where('sector.page_type','like','%'.$page.'%')
+        ->select('sector.sector_name', 'sector.sector_id','sector.pic_height','sector.sector_img',
+        'sector.sector_type','sector.sector_code','sector.id','sector.sector_width')  
+        ->orderBy('id')
+        ->get()->all();
+        if(empty($sectors)){
+            return Result::error('通栏不存在!');
+        }
+
+        // 获取每个类别的通栏数量
+        $type_num = array_count_values(array_column($sectors,'sector_type'));
+        // 此页面通栏的所有类别
+        $all_sector_types = array_keys($type_num);
+        //获取每个通栏类别在其中的位置    -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏
+        $sector_type_keys = array_column($sectors,'sector_type');
+        // 将数组处理为二维数组,值相同的合并为同一个数组,保持原有键值
+        $sector_types = array_reduce(array_keys($sector_type_keys), function ($result, $key) use ($sector_type_keys) {
+            $value = $sector_type_keys[$key];
+            if (!isset($result[$value])) {
+                $result[$value] = [];
+            }
+            $result[$value][$key] = $key;
+            $result[$value] = array_values($result[$value]);
+
+            return $result;
+        }, []);
+        // return $sector_types;
+        $must_type = $rule->must_type;
+        $sector_num = $rule->max_num;
+        // 获取必需通栏类别与查询到的通栏类别的交集数量;
+        $is_must = count(array_intersect($must_type, $all_sector_types));
+        // $is_must交集数量若是等于必需通栏类别的数量相等则不缺必需通栏
+        if( $is_must != count($must_type)){
+            return '此页面缺少必要通栏!';
+        }
+        // var_dump("sector_types",$all_sector_types);
+        // var_dump("must_type",$must_type);
+        // return $type_num;
+
+
+        // 非必须通栏的随机处理
+        $not_must_type = array_diff($all_sector_types,$must_type);
+        $total = count($not_must_type);
+        $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
+        $and_type = $rule->and_type ?? [];
+        // return $not_must_type;
+
+        if ($randomCount === 0) {
+            $random_sector = [];
+            $rand_count = 0;
+        } else {
+            // 随机获取指定数量的键名(保留原键)
+            $randomKeys = array_rand($not_must_type, $randomCount);
+            // 处理单元素情况(array_rand返回字符串,需转为数组)
+            if (!is_array($randomKeys)) {
+                $randomKeys = [$randomKeys];
+            }
 
-    
+            // 通栏关联性规则暂时无法实现---------------
+            // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组
+            // 判断 $oneDimensionalArray 是否为一维数组
+            // if (isset($and_type) && is_array($and_type) && !array_is_list(array_filter($and_type, 'is_array'))) {
+                // 判断是否存在有关联通栏的交集
+                // if (isset($and_type) && !empty($and_type) && count(array_intersect($and_type, $randomKeys)) > 0) {
+                //     // var_dump("11111",$and_type);
+                //     // var_dump("22222",$randomKeys);
+                //     // 将一维数组中的值都放到随机数组中
+                //     $randomKeys = array_merge($randomKeys, $and_type);
+                // }
+                // var_dump(count(array_intersect($and_type, $randomKeys)));
+            // }
+            // -----------------------------
+            // 根据随机键名提取元素(保留原键值关系)
+            // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
+            // var_dump("randomKeys",$randomKeys);
+            $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
+            $rand_count = count($random_sector);
+        }
+        // var_dump("randomKeys",$randomKeys);
+        // var_dump("random_sector",$random_sector);
+        var_dump("rand_count",$rand_count);
+        // return $random_type;
+
+        // 必需通栏
+        $must_type = array_intersect($all_sector_types,$must_type);
+        // return $must_type;
+        $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type))));
+        $must_count = count($must_sector);
+        // var_dump("must_sector",$must_sector);
+        var_dump("must_count",$must_count);
+        // return $must_sector;
+
+
+
+
+        // 计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
+        $repeat_num = $sector_num-$rand_count-$must_count;
+        // 组合起来通栏
+        $sector_zuhe1 = array_merge($must_sector,$random_sector);
+
+        // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
+        $rule_repeat = (array)$rule->type_max;
+        $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
+        // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
+        foreach($rule_repeat as $key => $value){
+            if(in_array($key,array_keys($sector_zuhe1_type))){
+                $rule_repeat[$key] = $value-1;
+            }
+        }
+        // var_dump("repeat_num",$repeat_num);
+        // return $sector_zuhe1;
+
+        // 剩余通栏进行重复处理
+        // $rule_repeat = (array)$rule->type_max;
+        // 取到可以重复的通栏类别
+        $rule_repaet_type =  array_keys($rule_repeat);
+        $repeat_type = array_intersect_key($sector_types, array_flip($rule_repaet_type));
+        // 获取可以随机重复的通栏
+        $repaet_key = 0;
+        $num = 0;
+        $type = [];
+        // return $rule_repeat;
+        for($repaet_key; $repaet_key < $repeat_num;){
+            $num++;
+            foreach($repeat_type as $key => $value){
+                $type[$key] = $num;
+                // $type代表每个类型的通栏数量,每个类型的通栏的数量应该≤规则限制的数量
+                if($repaet_key < $repeat_num  && $type[$key] <= $rule_repeat[$key]-1){
+                    // $max_repaert_num = $rule_repeat[$key];
+                    // if($max_repaert_num < $num ){
+                        // $arr_key = $num+$key;
+                        if (count($value) > 0) {
+                            // 若 可以随机的通栏类型存在多个通栏,随机取其一
+                            $repeat_sector_key[$repaet_key] = $value[array_rand($value)];
+                        } else {
+                            $repeat_sector_key[$repaet_key] = $value[0];
+                        }
+                        // $type[$repaet_key] = $repeat_sector_key[$repaet_key];
+                        $repaet_key++;
+                    // }
+                }else{
+                    break;
+                }
+            }
+            
+        }
+        
+        // var_dump("num", $num);
+        // var_dump("repeat_sector_key", $repeat_sector_key);
+        // 随机的重复通栏键值转换成真正的通栏
+        $repeat_sector = [];
+        foreach ($repeat_sector_key as $key => $value) {
+            if (isset($sectors[$value])) {
+                $repeat_sector[$key] = $sectors[$value];
+            }
+        }
+        $repeat_count = count($repeat_sector);
+        var_dump("repeat_count",$repeat_count);
+        // var_dump("repeat_sector", $repeat_sector);
+
+
+        $sector = array_merge($repeat_sector,$sector_zuhe1);
+        // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
+        $sector_specal_sort = array_column($sector,'sector_type');
+        if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort)){
+            $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
+            $pic_key = array_search(7,$sector_specal_sort) ?? null;
+            $toutiao = $sector[$toutiao_key];
+            $pic =  $sector[$pic_key];
+            $soty_1 = $sector[0];
+            $sort_2 = $sector[1];
+            if(!empty($toutiao_key)){
+                $sector[0] = $toutiao;
+                $sector[$toutiao_key] = $soty_1;
+                $sector[1] = $pic;
+                $sector[$pic_key] = $sort_2;
+            }else{
+                 $sector[0] = $pic;
+                $sector[$pic_key] = $sort_2;
+            }
+
+        }
+        $sector_id = array_column($sector,'id');
+        $data['page'] = $page;
+        $component[] = $this->randomComponent($sector_id,$data,$sector);
+
+        return ['sector_id'=>$sector_id,
+        // 'sector'=>$sector,
+        'component'=>$component,
+        // 'sector'=>$sector,
+    ];
+    }
+    public function randomComponent($sector_id,$data,$sector){
+        // return $sector;
+        // 获取原始查询结果
+        $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
+            ->leftJoin('component','component.component_type','sector_component.component_id')
+            ->leftJoin('component_img','component_img.component_id','component.component_type')
+            ->where('component_img.template_id','=',$data['template_id'])
+            ->select('component.*','component_img.img_name','component_img.img_id','component_img.img_url',
+            'sector_component.sort_id','sector_component.sectorid')
+            ->orderBy('sector_component.sort_id')
+            ->orderBy('component.component_type')
+            ->get();
+        // 按sectorid和sort_id分组数据
+        $groupedResults = [];
+        $arr = 0;
+        $component_list = [];
+        // return $rawResults;
+        // $groupedResults重构数组,将相同位置的组件合并,相同组件的样式合并(已经包括所有选择的组件及组件样式)
+        foreach ($rawResults as $key => $item) {
+            $sectorId = $item->sectorid;
+            $sortId = $item->sort_id;
+            $componentId = $item->component_type;
+            $component_data = $item->component_data;
+            $component_column = $item->component_column;
+            $component_type = $item->type_id;
+            $component_ad = $item->ad;
+            if (!isset($groupedResults[$sectorId])) {
+                $groupedResults[$sectorId] = [];
+            }
+            $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
+            $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
+            $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
+            $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
+            // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
+            if (!isset($componentData['componentData']) || !is_array($componentData['componentData']) ) {
+                $componentData['componentData'] = [];
+            }
+            $componentData['componentData'] += $component_listtype;
+                $component_code = $componentData;
+                $component_list[$key] = $component_code;
+                // $component_list[$key]['listType'] = $component_listtype;
+            // }
+            $sort_id = intval($sortId-1);
+            if(!isset($groupedResults[$sectorId][$sort_id])) {
+                // $groupedResults[$sectorId][$sortId] = [];
+                $com_key = 0;
+                    $groupedResults[$sectorId][$sort_id][$com_key] = [
+                    'component_type' => $item->component_type,
+                    'type_id' => $component_type,
+                    'sort' => $sortId,
+                    'images'  =>   [
+                                'img_name' => $item->img_name,
+                                'img_id' => $item->img_id,
+                                'img_url' => $item->img_url,
+                            ]
+                ];                
+                // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数)
+               
+            }else{
+                
+                if((isset($component_ids) && in_array($componentId,$component_ids))){
+                    if(isset($img_arr)){
+                        $img_arr++;
+                    }else{
+                        $img_arr = 0;
+                        if(isset($com_key)){
+                    $com_key++;
+                }
+                    }
+                    // $com_id_key = array_search($componentId,$component_ids);
+                    $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [
+                        'img_name' => $item->img_name,
+                        'img_id' => $item->img_id,
+                        'img_url' => $item->img_url,
+                    ];
+                }else{
+                    $img_arr = 0;
+                    $com_key = $com_key ?? 0;
+                    $groupedResults[$sectorId][$sort_id][$com_key] = [
+                        'component_type' => $item->component_type,
+                        'type_id' => $component_type,
+                        'sort' => $sortId,
+                        'images' => 
+                        [
+                            $img_arr =>
+                                [
+                                    'img_name' => $item->img_name,
+                                    'img_id' => $item->img_id,
+                                    'img_url' => $item->img_url,
+                                ]
+                        ]
+                    ];
+                }
+            }
+            // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
+            // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
+            if (is_scalar($sectorId) && is_scalar($sort_id)) {
+                if(!in_array($component_type,[10,12,16])){
+                    if($component_type == 15){
+                        $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad'] = $component_ad;
+                    }else{
+                        $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
+                    }
+                }else{
+                    $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = [];
+                }
+            } else {
+                // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
+                trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
+            }
+            // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
+            $component_ids[$sort_id] = $componentId;
+        }
+
+        // return $groupedResults;
+
+        $result = [];
+        // return  $sectors;
+        $y_num = 0;
+        foreach ($sector as $key => $value) {
+            // array_push($value['sort_id'],$key);
+            
+            // $sector_id_ = $value['sector_id'];
+            $sectorid = $value['id'];
+            $template_data[$key] = [
+                'sectorName' => $value['sector_id'],
+                'sort' => $key+1,
+            ]; 
+            $date = date('mdHisu' ); // 获取时分秒微秒,转换为纯数字格式
+            // $date = 11010630021;
+            if(substr($date,0,1) == 0){
+                $date = substr($date, 0, 10); // 截取前9位
+            }else{
+                $date = substr($date, 0, 9); // 截取前9位
+            }
+            $random_num = intval($date) . rand(1000, 9999); // 拼接4位随机数,组成13位随机数
+            $y_num = $y_num + $value['pic_height'];
+            $sector_width = $value['sector_width']/100;
+            $canvas_data[$key] = [
+                'i' => $random_num,
+                'x' => 0,
+                'y' => $y_num,
+                'w' => $sector_width,
+                'h' => $value['pic_height'],
+                'type' => $value['sector_id'],
+                'content' => [
+                    'sectorName' => $value['sector_id'],
+                ]
+
+            ];
+            // $sector_key[$key] = array_keys($value );
+            $sector_component = [];
+            if(isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])){
+                foreach ($groupedResults[$sectorid] as $sortId => $components) {
+                    // 通栏某一位置的随机组件
+                    if(count($components) > 1){
+                        $componentIds = count($components);
+                        $selectedComponentId = rand(0, $componentIds - 1);
+                    }else{
+                        $selectedComponentId = 0;
+                    }
+                    // var_dump($components[$selectedComponentId]);
+                    // $Component = $components[$selectedComponentId];
+                    $Component['component_type'] = $components[$selectedComponentId]['component_type'] ?? 0;
+                    $Component['sort'] = $components[$selectedComponentId]['sort'] ?? 1;
+                    // 通栏某一位置的随机组件样式图
+                    // 检查 $Component['images'] 是否存在且为数组
+                    if (isset($components[$selectedComponentId]['images']) && is_array($components[$selectedComponentId]['images'])) {
+                        $images = count($components[$selectedComponentId]['images']);
+                        $selectedImage = 0;
+                        if ($images > 1) {
+                            $selectedImage = rand(0, $images - 1);
+                        }
+                        if (isset($components[$selectedComponentId]['images'][$selectedImage])) {
+                            $Component['component_style'] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage]->img_id : $components[$selectedComponentId]['images'][$selectedImage]['img_id'];
+                        }
+                    }
+                    // var_dump($images);
+                    $Component['componentData'] = $components[$selectedComponentId]['componentData'];
+                    if($data['page'] == 1 && in_array($components[$selectedComponentId]['type_id'],[7,14,15])){
+                        $Component['componentData']['name'] = '请选择栏目';
+                    }
+                    if($data['page'] == 2 && in_array($components[$selectedComponentId]['type_id'],[7,14,15])){
+                        $Component['componentData']['name'] = '自动生成';
+                    }
+                    if($components[$selectedComponentId]['type_id'] == 11){
+                       $template_data[$key]['ad'] = $components[$selectedComponentId]['componentData']['ad'];
+                       $Component['componentData'] = [];
+                    }
+                    if($components[$selectedComponentId]['type_id'] == 15){
+                       $template_data[$key]['ad'] = $components[$selectedComponentId]['ad'];
+                    //    $components[$selectedComponentId]['componentData'] = [];
+                    }
+                    $sector_component[$sortId] = $Component;
+                    //  $sector_component[$sortId] = $selectedComponentId;
+                }
+                // var_dump("image",$image);
+            }
+            $template_data[$key]['componentList'] = $sector_component;
+            $canvas_data[$key]['content']['componentList'] = $sector_component;
+            $canvas_data[$key]['dataSort'] = $key;
+            $canvas_data[$key]['moved'] = false;
+            // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
+        }
+        
+        return [
+            'template_data' => $template_data,
+            'canvas_data' => $canvas_data,
+        ];
+    }
 }

+ 2 - 0
app/JsonRpc/PublicRpcServiceInterface.php

@@ -211,6 +211,8 @@ interface PublicRpcServiceInterface
     public function updateComponentImg(array $data): array;
     //自助建站-获取通栏类型
     public function getSectorType(array $data): array;
+    // 自助建站-随机模板
+    public function randomWebTemplate(array $data): array;
 
 
 

+ 5 - 0
app/Model/Component.php

@@ -24,4 +24,9 @@ class  Component extends Model
      * The attributes that should be cast to native types.
      */
     protected array $casts = [];
+    public function componentImgs()
+    {
+        return $this->hasMany(ComponentImg::class, 'component_id', 'component_type');
+    }
 }
+

+ 4 - 0
app/Model/SectorComponent.php

@@ -24,4 +24,8 @@ class  SectorComponent extends Model
      * The attributes that should be cast to native types.
      */
     protected array $casts = [];
+    public function component()
+    {
+        return $this->hasOne(Component::class, 'component_type', 'component_id');
+    }
 }