FengR 4 mesi fa
parent
commit
f3c60db752
1 ha cambiato i file con 75 aggiunte e 55 eliminazioni
  1. 75 55
      app/JsonRpc/PublicRpcService.php

+ 75 - 55
app/JsonRpc/PublicRpcService.php

@@ -2736,6 +2736,9 @@ class PublicRpcService implements PublicRpcServiceInterface
         $cate_num = WebsiteCategory::where('website_id',$data['website_id'])
         ->where('is_show',1)
         ->count();
+        if($cate_num < 5){
+            return Result::error('此网站栏目过少,无法进行随机模板!');
+        }
         // return Result::success($index_rule);
         // $tempalte['index'] = $this->randomWeb($data,$index_rule);
         $tempalte['index'] = $this->randomPage($data,$cate_num,$index_rule);
@@ -2802,40 +2805,38 @@ class PublicRpcService implements PublicRpcServiceInterface
         // 4. array_values() 重新索引返回的数组,使其键名从 0 开始连续递增
         // 最终得到的 $must_sector 数组包含了 $sectors 中对应 $must_type 键名的所有必需通栏数据
         // 获取 $rector 数组中 component_num 字段的值
-        $sector_type1_key = isset($sector_types[1]) ? $sector_types[1] : null;
-        $sector_type1 = $sector_type1_key ? array_intersect_key($sectors, array_flip($sector_type1_key)) : [];
-        // 修正错误:array_column 第三个参数不能为数组,移除该参数
-        $componentNums = array_column($sector_type1, 'component_num');
-        // 检查数组是否为空且长度一致,避免 array_combine 报错
-        if (!empty($sector_type1_key) && !empty($componentNums) && count($sector_type1_key) === count($componentNums)) {
-            $sector_comnum = array_combine($sector_type1_key, $componentNums);
-        } else {
-            $sector_comnum = [];
-        }
-        // 获取最小值
-        // $min_component_num = min($componentNums);
-        // 获取最小值对应的键值
-        // $minKey = array_search($min_component_num, $componentNums);
-        // $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type))));
-        // $must_count = count($must_sector);
-        return $sector_comnum;
-        // if(in_array(1,$must_type) && $page == 1){
-        //     $must_key = array_search(1,$must_type); 
-        //     $must_sector_catenum = $sectors[$must_key];
-        //     if($cate_num-$must_sector_catenum == 0 ){
-        //         return $must_sector;
-        //     }else if($cate_num-$must_sector_catenum < 0 ){
-        //         if($cate_num-$min_component_num == 0){
-        //             $must_sector = $sectors['$minKey'];
-        //             return $must_sector;
-        //         }else{
+        // if(in_array(1,$must_type) || in_array(3,$must_type)){
+        //     $sector_type1_key = isset($sector_types[1]) ? $sector_types[1] : null;
+        //     $sector_type1 = $sector_type1_key ? array_intersect_key($sectors, array_flip($sector_type1_key)) : [];
+        //     // 修正错误:array_column 第三个参数不能为数组,移除该参数
+        //     $componentNums = array_column($sector_type1, 'component_num');
+        //     // 获取最小值
+        //     $min_component_num = min($componentNums);
+        //     if($cate_num == 1){
+        //         if((in_array(1,$must_type) && in_array(3,$must_type)) || (in_array(1,$must_type) && $cate_num < $min_component_num)){
         //             return '此网站首页显示栏目过少,请添加首页显示栏目!';
-        //         }                
-        //     }else{
-        //         $cate_num = $cate_num-$must_sector_catenum;
+        //         }
+        //     }
+        //     // 检查数组是否为空且长度一致,避免 array_combine 报错
+        //     if (!empty($sector_type1_key) && !empty($componentNums) && count($sector_type1_key) === count($componentNums)) {
+        //         $sector_comnum = array_combine($sector_type1_key, $componentNums);
+        //     } else {
+        //         $sector_comnum = [];
         //     }
         // }
-        return $must_sector;
+        
+        
+        // 获取最小值对应的键值
+        // $minKey = array_search($min_component_num, $componentNums);
+        $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type))));
+        $must_count = count($must_sector);
+        // return $sector_comnum;
+        $must_sector = array_column($must_sector,'sector_type');
+        // 目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量
+        if($must_count > $sector_num){
+            return $must_sector;
+        }
+        // return $must_sector;
         // 2.非必须通栏的随机处理
         $not_must_type = array_diff($all_sector_types,$must_type);
         
@@ -2903,6 +2904,9 @@ class PublicRpcService implements PublicRpcServiceInterface
             if(count(array_intersect($sector_zuhe1,$must_sector)) != count($must_sector)){
                 return '此页面缺少必要通栏!';
             }
+            if($cate_num == 5){
+                return $sector_zuhe1;
+            }
             // 剩余通栏进行重复处理
             // $rule_repeat = (array)$rule->type_max;
             // 取到可以重复的通栏类别
@@ -2923,38 +2927,54 @@ class PublicRpcService implements PublicRpcServiceInterface
             //     'repeat_type' => $repeat_type,
             //     'must_type' => $must_type,
             // ];
+            $sector_comnum = $cate_num-4;
+            $com_num = 0;
+            // return $cate_num;
+            // 随机重复通栏
             while($repaet_key < $repeat_num && $iteration_count < $max_iterations){
                 $num++;
-                foreach($repeat_type as $key => $value){
-                    if(!isset($type[$key])){
-                        $type[$key] = 0;
-                    }else{
-                        $type[$key]++;
-                    }
-                    // $type代表每个类型的通栏数量,每个类型的通栏的数量应该≤规则限制的数量
-                    if($repaet_key < $repeat_num  && $type[$key] <= $rule_repeat[$key]){
-                        // $max_repaert_num = $rule_repeat[$key];
-                        // if($max_repaert_num < $num ){
-                        //     // $arr_key = $num+$key;
-                            if (count($value) > 1) {
-                                // 若 可以随机的通栏类型存在多个通栏,随机取其一
-                                $repeat_sector_key[$repaet_key] = $value[array_rand($value)];
-                            } else {
-                                $repeat_sector_key[$repaet_key] = $value[0];
+                if($sector_comnum > 0){
+                    foreach($repeat_type as $key => $value){
+                        if(!isset($type[$key])){
+                            $type[$key] = 0;
+                        }else{
+                            $type[$key]++;
+                        }
+                        // $type代表每个类型的通栏数量,每个类型的通栏的数量应该≤规则限制的数量
+                        
+                        if($repaet_key < $repeat_num  && $type[$key] <= $rule_repeat[$key]){
+                            // 若是资讯类则剩余的栏目至少要有三个;若是混合类则剩余的栏目至少要有一个;
+                            // $sector_comnum:剩余的栏目数量(目前是栏目数量-4)
+                            // $com_num:重复通栏的栏目数量
+                            if(($key == 1 && $sector_comnum - $com_num >= 3) || ($key == 3 && $sector_comnum - $com_num >= 1) || ($key != 1 && $key != 3)){
+                                if (count($value) > 1) {
+                                    // 若 可以随机的通栏类型存在多个通栏,随机取其一
+                                    $repeat_sector_key[$repaet_key] = $value[array_rand($value)];
+                                } else {
+                                    $repeat_sector_key[$repaet_key] = $value[0];
+                                }
+                                $rk = $repeat_sector_key[$repaet_key];
+                                $rk_sector = $sectors[$rk];
+                                $com_num += $rk_sector['component_num'];
+                                // $component_num = $rk_sector['component_num'];
+                                $type[$repaet_key] = $repeat_sector_key[$repaet_key];
+                                $repaet_key++;
                             }
-                            $type[$repaet_key] = $repeat_sector_key[$repaet_key];
-                            $repaet_key++;
-                        // }
+                        }else{
+                            break;
+                        }
                     }
-                    // else{
-                    //     break;
-                    // }
+                }else{
+                    break;
                 }
+               
                 // var_dump("num",  $type[$key]);
                 // var_dump("repeat_sector_key", $repeat_sector_key);
                 $iteration_count++;
             }
-            // return $repeat_sector_key;
+            // return [
+            //     $repeat_sector_key,$com_num,$sector_comnum
+            // ];
             // var_dump("num", $num);
             // var_dump("repeat_sector_key", $repeat_sector_key);
             // 随机的重复通栏键值转换成真正的通栏
@@ -2970,7 +2990,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             $repeat_count = count($repeat_sector);
             var_dump("repeat_count",$repeat_count);
             // var_dump("repeat_sector", $repeat_sector);
-            //  return ($data['page'] == '2' || $data['page'] == 2);
+            //  return $repeat_sector;
             
             $sector = array_merge($repeat_sector,$sector_zuhe1);
         }else{