Przeglądaj źródła

自助建站-随机模板:随机组件、随机组件样式图

15313670163 5 miesięcy temu
rodzic
commit
ed76866d14
1 zmienionych plików z 125 dodań i 87 usunięć
  1. 125 87
      app/JsonRpc/PublicRpcService.php

+ 125 - 87
app/JsonRpc/PublicRpcService.php

@@ -2797,7 +2797,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             // -----------------------------
             // 根据随机键名提取元素(保留原键值关系)
             // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
-            var_dump("randomKeys",$randomKeys);
+            // var_dump("randomKeys",$randomKeys);
             $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
             $rand_count = count($random_sector);
         }
@@ -2822,6 +2822,8 @@ class PublicRpcService implements PublicRpcServiceInterface
         $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);
@@ -2830,10 +2832,11 @@ class PublicRpcService implements PublicRpcServiceInterface
                 $rule_repeat[$key] = $value-1;
             }
         }
+        // var_dump("repeat_num",$repeat_num);
         // return $sector_zuhe1;
 
         // 剩余通栏进行重复处理
-        $rule_repeat = (array)$rule->type_max;
+        // $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));
@@ -2841,10 +2844,13 @@ class PublicRpcService implements PublicRpcServiceInterface
         $repaet_key = 0;
         $num = 0;
         $type = [];
-        // return $repeat_type;
+        // return $rule_repeat;
         for($repaet_key; $repaet_key < $repeat_num;){
+            $num++;
             foreach($repeat_type as $key => $value){
-                if($repaet_key < $repeat_num ){
+                $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;
@@ -2854,35 +2860,31 @@ class PublicRpcService implements PublicRpcServiceInterface
                         } else {
                             $repeat_sector_key[$repaet_key] = $value[0];
                         }
-                        $type[$repaet_key] = $repeat_sector_key[$repaet_key];
+                        // $type[$repaet_key] = $repeat_sector_key[$repaet_key];
                         $repaet_key++;
                     // }
                 }else{
                     break;
                 }
-                // var_dump("rule_repeat",$rule_repeat[$key]);
             }
-            $num++;
+            
         }
         
         // var_dump("num", $num);
-        var_dump("type", $repeat_type);
-       
+        // var_dump("repeat_sector_key", $repeat_sector_key);
+        // 随机的重复通栏键值转换成真正的通栏
         $repeat_sector = [];
         foreach ($repeat_sector_key as $key => $value) {
-            if (isset($sectors[$key])) {
+            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);
-        // if($page ==1 && array_search(6,array_column($sector_zuhe1,'sector_type'))){
-        //     $sector_arr 
-        // }
-        // $specal_sector_rule = ;
         // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
         $sector_specal_sort = array_column($sector,'sector_type');
         if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort)){
@@ -2904,12 +2906,7 @@ class PublicRpcService implements PublicRpcServiceInterface
 
         }
         $sector_id = array_column($sector,'id');
-        // $sector_code = array_column($sector,'sector_code');
-        // $sector_code = array_flip($sector_code);
-        // foreach ($sector_code as &$item) {
-        //     $item = json_decode($item, true);
-        // }
-        // unset($item);
+        $data['page'] = $page;
         $component = $this->randomComponent($sector_id,$data,$sector);
 
         return ['sector_id'=>$sector_id,'component'=>$component];
@@ -2918,22 +2915,25 @@ class PublicRpcService implements PublicRpcServiceInterface
         // 获取原始查询结果
         $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')
             ->get();
-        // return $rawResults; 
         // 按sectorid和sort_id分组数据
         $groupedResults = [];
         $arr = 0;
         $component_list = [];
+        
+        // $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;
             if (!isset($groupedResults[$sectorId])) {
                 $groupedResults[$sectorId] = [];
             }
@@ -2941,7 +2941,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             $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) : [];
             // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
-            if (!isset($componentData['componentData']) || !is_array($componentData['componentData'])) {
+            if (!isset($componentData['componentData']) || !is_array($componentData['componentData']) ) {
                 $componentData['componentData'] = [];
             }
             $componentData['componentData'] += $component_listtype;
@@ -2950,82 +2950,120 @@ class PublicRpcService implements PublicRpcServiceInterface
                 // $component_list[$key]['listType'] = $component_listtype;
             // }
              $sort_id = intval($sortId-1);
-            if (!isset($groupedResults[$sectorId][$sort_id])) {
-               
+            
+            if(!isset($groupedResults[$sectorId][$sort_id])) {
                 // $groupedResults[$sectorId][$sortId] = [];
-                $groupedResults[$sectorId][$sort_id] = [
-                    'component_type' => $item->component_type,
+                $com_key = 0;
+                    $groupedResults[$sectorId][$sort_id][$com_key] = [
+                    'component_id' => $item->component_type,
+                    'type_id' => $component_type,
                     'sort_id' => $sortId,
-                    'images' => [
+                    'images'  =>   [
+                                'img_name' => $item->img_name,
+                                'img_id' => $item->img_id,
+                                'img_url' => $item->img_url,
+                            ]
+                ];                
+                // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数)
+               
+            }else{
+                if(isset($com_key)){
+                    $com_key++;
+                }
+                if((isset($component_ids) && in_array($componentId,$component_ids))){
+                    if(isset($img_arr)){
+                        $img_arr++;
+                    }
+                    $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [
                         'img_name' => $item->img_name,
                         'img_id' => $item->img_id,
                         'img_url' => $item->img_url,
-                    ]
-                ];
-                // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数)
-                if (is_scalar($sectorId) && is_scalar($sort_id)) {
-                    $groupedResults[(int)$sectorId][(int)$sort_id]['componentData'] = $component_code['componentData'];
-                } else {
-                    // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
-                    trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
+                    ];
+                }else{
+                    $img_arr = 0;
+                    $groupedResults[$sectorId][$sort_id][$com_key] = [
+                        'component_type' => $item->component_type,
+                        'type_id' => $component_type,
+                        'sort_id' => $sortId,
+
+                        'images' => 
+                        [$img_arr =>
+                            [
+                                'img_name' => $item->img_name,
+                                'img_id' => $item->img_id,
+                                'img_url' => $item->img_url,
+                            ]
+                        ]
+                        
+                    ];
                 }
+
             }
-            // 检查 $component_data 是否为 null 或空字符串,避免 json_decode 接收 null 参数
-            // if (!isset($groupedResults[$sectorId][$sortId])) {
-                
-            //     $arr++;
-            // }
-            
-            // if (!empty($item->component_img)) {
-            //     $groupedResults[$sectorId][$sortId][$componentId]['images'][] = [
-            //         'img_name' => $item->img_name,
-            //         'img_id' => $item->img_id,
-            //         'img_url' => $item->img_url,
-            //     ];
-            // }
+             if (is_scalar($sectorId) && is_scalar($sort_id)) {
+                $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
+            } else {
+                // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
+                trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
+            }
+            // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
+            $component_ids[$key] = $componentId;
         }
-        
-        // 重组数据结构为: sectorid -> sort_id -> {component, img}
-        $result = [];
-        // $sector_key = array_keys($sector_code[0]);
-        // return  $sector_code[0];
-        // foreach ($sector as $key => $value) {
-        //     // array_push($value['sort_id'],$key);
-        //     $page['template'][$key] = [
-        //         $value['sector_id'] => [
-        //             'sectorName' => $value['sector_name'],
-        //             'sectorHeight' => $value['pic_height'],
-        //         ]
-        //     ]; 
-        //     if($value['sector_type'] == 2){
 
-        //     }
-        //     // $sector_key[$key] = array_keys($value );
-        //     foreach ($rawResults as $sortId => $components) {
-        //     //     // 随机选择一个组件
-        //     //     $componentIds = array_keys($components);
-        //     //     $selectedComponentId = $componentIds[array_rand($componentIds)];
-        //     //     $selectedComponent = $components[$selectedComponentId];
-        //     //     // 随机选择一个样式
-        //     //     $images = $selectedComponent['images'];
-        //     //     $selectedImage = [];
-        //     //     if (!empty($images)) {
-        //     //         $selectedImage = $images[array_rand($images)];
-        //     //     }
+        // return $groupedResults;
+
+        $result = [];
+        $sector = [
+            0 => $sector[0],
+            1 => $sector[1],
+            2 => $sector[2],
+        ];
+        // return  $sectors;
+        foreach ($sector as $key => $value) {
+            // array_push($value['sort_id'],$key);
+           
+            $sector_id_ = $value['sector_id'];
+            $sectorid = $value['id'];
+            $sectors[$key][$sector_id_] = [
+                'sectorName' => $value['sector_name'],
+                'sectorHeight' => $value['pic_height'],
+            ]; 
+            // $sector_key[$key] = array_keys($value );
+            $sector_component = [];
+            foreach ($groupedResults[$sectorid] as $sortId => $components) {
+                // 通栏某一位置的随机组件
+                if(count($components) > 1){
+                    $componentIds = count($components);
+                    $selectedComponentId = rand(0, $componentIds - 1);
+                }else{
+                    $selectedComponentId = 0;
+                }
+                $Component = $components[$selectedComponentId];
+                // 通栏某一位置的随机组件样式图
+                // 检查 $Component['images'] 是否存在且为数组
+                if (isset($Component['images']) && is_array($Component['images'])) {
+                    $images = count($Component['images']);
+                    $selectedImage = 0;
+                    if ($images > 1) {
+                        $selectedImage = rand(0, $images - 1);
+                    }
+                    if (isset($Component['images'][$selectedImage])) {
+                        $Component['images'] = is_object($Component['images'][$selectedImage]) ? $Component['images'][$selectedImage]->img_id : $Component['images'][$selectedImage]['img_id'];
+                    }
+                }
+                if($data['page'] == 1 && in_array($Component['type_id'],[7,14,15])){
+                    $Component['componentData']['name'] = '请选择栏目';
+                }
                 
-        //     //     // 构建最终结构
-        //     //     if (!isset($result[$sectorId])) {
-        //     //         $result[$sectorId] = [];
-        //     //     }
-        //     //     $result[$sectorId][$sortId] = [
-        //     //         'component' => $selectedComponent['component'],
-        //     //         'img' => $selectedImage
-        //     //     ];
-        //     }
-        //     // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
-        // }
+                // $image[$sortId] = $components[$selectedComponentId]['images'][$selectedImage]['img_id'];
+                // $Component['component_type'] = $Component['images'][$selectedImage]['img_id'];
+                $sector_component[$sortId] = $Component;
+                // var_dump("image",$image);
+            }
+            $sectors[$key][$sector_id_]['componentList'] = $sector_component;
+            // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
+        }
         
-        return $groupedResults;
+        return $sectors;
     }
     public function getPageCode($sector,$page){
         if(empty($sector)){