Przeglądaj źródła

自助建站-随机模板

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

+ 256 - 20
app/JsonRpc/PublicRpcService.php

@@ -2703,27 +2703,263 @@ class PublicRpcService implements PublicRpcServiceInterface
         if(empty($template)){
             return Result::error('此皮肤不存在!');
         }
-        $index = $this->randomPage($data);
-        return Result::success($index);
-    }
-    public function randomPage($data){
         $rule = TemplateRule::first();
-        $index_rule = json_decode($rule['index_rule'],true);
-        // $zeroKeysPerItem = array_map(function ($item) {
-        // $zeroValues = array_filter($item, function ($value) {
-        //     return $value === 0;
-        // });
-        $index_sector_num = $index_rule[0];
-        $index_rule = json_encode($index_rule);
-
-        // 输出结果
-        // print_r($zeroKeysPerItem);
-        // $index_num = array_search(0, array_map($index_rule, $array ?? []));
-        $sectors = Sector::where('template_id',$data['template_id'])
-        ->where('page_type','like','%1%')
-        ->limit($index_sector_num)->get();
+        $index_rule = json_decode($rule['index_rule']);
+        // $class_rule = json_decode($rule['class_rule']);
+        // return Result::success($index_rule);
+        $tempalte['index'] = $this->randomPage($data,1,$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')  
+        ->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;
+            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_intersect_key($sectors, array_flip($randomKeys));
+            // $random_sector = array_intersect_key($sectors, array_flip($random_type));
+            $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($sector_type_keys,$must_type);
+        $must_sector = 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;
+        var_dump("rand_count",$rand_count);
+        var_dump("must_sector",$must_count);
+        var_dump("repeat_num",$repeat_num);
+
+
+        // 剩余通栏进行重复处理
+        $rule_repeat = $rule->type_max;
+        // 取到可以重复的通栏类别
+        $rule_repaet_type =  array_keys((array)$rule_repeat);
+        $repeat_type = array_intersect($all_sector_types,$rule_repaet_type);
+        // 获取可以随机重复的通栏
+        $repeat_sector = array_intersect_key($sectors, array_flip($repeat_type));
+
+
+        // $change_type_max = array_intersect($random_type,$repaet_type);
         
-        return $sectors;
+        // var_dump("repeat_type_num",$repeat_type_num);
+        // for($repeat_num;$repeat_num == 0;$repeat_num--){
+            
+        // }
+        // $count = count($filteredTypes);
+        // $all_sector_type = array_merge($random_sector,$must_sector);
+        // $type_maxnum = $rule->type_max;
+        // // 将 $type_maxnum 转换为数组类型
+        // $type_maxnum_array = (array)$type_maxnum;
+        // // return gettype($type_maxnum_array);
+        // $max_type = array_keys($type_maxnum_array);
+        // if(count(array_intersect($max_type,$must_type))>0 || count(array_intersect($max_type,$randomKeys))>0){
+        //     $rand_count = count($random_sector)+count($must_type)-1;
+        // }
+        // foreach($type_maxnum_array as $key => $value){
+        //     if($sector_num-$rand_count > 0 && ){
+        //         $repeat_sector[$key] = rand();
+        //     } 
+        // }
+        // $max_ad = 0;
+        return $sector_type_keys;
+    }
+    public function getPageCode($sector,$page){
+        if(empty($sector)){
+            return Result::error('通栏不存在!');
+        }
+        // $sectors_key = array_keys(array_diff($com_num, [1]));
+        // return Result::success($sector);
+        $num = 0;
+        $array_key = 0;
+        foreach($sector as $key => $value){
+            $sector_id = $value['sector_id'];
+            $sectors[$key] = $value['sector_id'];
+            // if(isset($comlist_key) && !empty($comlist_key)){
+            //     $sector_arr_key[$key] = array_keys($comlist_key);
+            // }
+            if(in_array($sector_id,$sectors_key) && in_array($sector_id,$sectors)){
+                $array_key = array_search($sector_id,$sectors); 
+                if(isset($sectors) && !in_array($sector_id,$sectors)){
+                    $array_key++;
+                }
+                $num++;
+                $array_key++;
+            }else{
+                $array_key++;
+            }
+            // var_dump($array_key);
+            // var_dump(in_array($sector_id,$sectors_key));
+            $comlist_key[$array_key][$sector_id]['sectorName'] = $value['sector_name'];
+            $comlist_key[$array_key][$sector_id]['sectorHeight'] = $value['pic_height'];
+            $comlist_key[$array_key][$sector_id]['sectorImg'] = $value['sector_img'];
+            $comlist_key[$array_key][$sector_id]['sort'] = $array_key;
+            $componentList['component_style'] =  1;
+            $componentList['sort'] =  $value['sort_id'];
+            // 1:资讯;2:广告;3:静态;
+            $component_data = empty($value['component_data']) ? null : (json_decode($value['component_data']) ?? []);
+            // 检查 $value['component_column'] 是否为非空字符串,避免传递 null 给 json_decode
+            $listType = empty($value['component_column']) ? null : (json_decode($value['component_column']) ?? []);;
+            // $page[$key][$setor_id]['componentList'] = 
+            // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;8:列表类组件;9:详情类组件;
+            // 10:二级导航栏类组件;11:广告类;12:静态资源类;13:单页导航类;14:广告资讯混合类;15:滚动图文;16:搜索框类;17:单页列表类;18:单页详情类;
+            if(in_array($value['type_id'],[1,2,3,4,5,6,7,8,9,15,17,18])){
+                $type = 1;
+                $componentList['component_type'] = $type;
+                if(in_array($value['type_id'],[7,15]) && $page == 1){
+                    $component_data->componentData->name = '请选择导航..';
+                }else if(in_array($value['type_id'],[7,15]) && $page == 2){
+                    $component_data->componentData->name = '自动生成';
+                }else if($page == 3){
+                    if($value['type_id'] == 4){
+                        $component_data->componentData->name = '最新资讯';
+                    }
+                    if($value['type_id'] == 6){
+                        $component_data->componentData->name = '热点精选';
+                    }
+                }else{
+                    unset($component_data->componentData->name);
+                }
+                $componentData = $component_data->componentData;
+                $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
+
+                // $page_listType = is_object($listType) ? (array)$listType->listType ?? [] : $listType['listType'] ?? [];
+                $componentList['componentData']['listType'] = $listType->listType;
+            }else if(in_array($value['type_id'],[10,12,13,16])){
+                $type = 3;
+                $componentList['component_type'] = $type;
+                $componentList['componentData']=(object)[];
+            }else if($value['type_id']== 11){
+                $type = 2;
+                $componentList['component_type'] = $type;
+                $componentList['componentData']=(object)[];
+                $ad = json_decode($value['ad']) ?? [];
+                if(is_object($ad)){
+                    $ad->thumb = $value['img_url'];
+                }else{
+                    $ad['thumb'] = $value['img_url'];
+                }
+                $comlist_key[$array_key][$sector_id]['ad'] =is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
+                // 在 $page[$key][$setor_id] 中增加一个键值为 ad 的数组
+                // $page[$key][$setor_id]['ad'] = is_object($ad) ? (array)$ad->ad ?? [] : $ad['ad'] ?? [];
+                
+            }else if($value['type_id'] == 14){
+                $type = 1;
+                $componentList['component_type'] = $type;
+                if($page == 1){
+                    $component_data->componentData->name = '请选择导航..';
+                }
+                $componentData = $component_data->componentData;
+                $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
+                $componentList['componentData']['listType'] = $listType->listType;
+                $ad = json_decode($value['ad']) ?? [];
+                $comlist_key[$array_key][$sector_id]['ad'] =is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
+
+            }else{
+
+            }
+            if( in_array($sector_id,$sectors_key)){
+                $comlist_key[$array_key][$sector_id]['componentList'][$num-1] = $componentList;
+                $comlist_key[$array_key][$sector_id]['componentList'] = array_values($comlist_key[$array_key][$sector_id]['componentList']);
+
+            }else{
+                $comlist_key[$array_key][$sector_id]['componentList'] = $componentList;
+            }
+            $keys[$key] = $key;
+        }
+        //  return Result::success($sector_arr_key);
+        // $page[$keys] = array_column($page,$sectors_key[0]);
+        $comlist_key = array_values($comlist_key);
+        // $sector_page = json_encode($comlist_key);
+        if(empty($comlist_key)){
+            return Result::error('通栏不存在!');
+        }
+        return ($comlist_key);
     }
-    
 }