ソースを参照

修改b端接口:随机模板的接口;

FengR 1 週間 前
コミット
b050361e8f
1 ファイル変更71 行追加36 行削除
  1. 71 36
      app/JsonRpc/PublicRpcService.php

+ 71 - 36
app/JsonRpc/PublicRpcService.php

@@ -3989,7 +3989,9 @@ class PublicRpcService implements PublicRpcServiceInterface
 
         // 将模型对象转换为数组,确保关系字段正确命名
         $sectors = $sectors->toArray();
-
+        if(empty($sectors)){
+            return '此网站没有 通栏!';
+        }
         // 提取并返回 component_imgs 内容,添加空安全检查
         // return $sectors[0]['sector_components'][0]['component']['component_imgs'] ?? [];
         // return $sectors;
@@ -4006,12 +4008,6 @@ class PublicRpcService implements PublicRpcServiceInterface
         $websiteServer = new \App\JsonRpc\WebsiteService();
         $all_cate_data = $websiteServer->getWebsiteModelCategory($cat_column);
         $all_cate = $all_cate_data['data'] ?? [];
-
-        // $all_cate = $query->where('is_show', 1)
-        //     // ->where('pid', 0)
-        //     ->orderBy('sort')
-        //     ->orderBy('updated_at', 'desc')
-        //     ->get()->toArray();
         $cate_num = count($all_cate);
         // return $all_cate;
         if ($cate_num < 5) {
@@ -4020,7 +4016,6 @@ class PublicRpcService implements PublicRpcServiceInterface
         $all_cate = $all_cate->toArray();
         $del_catid = 0;
         foreach ($all_cate as $k => $v) {
-            $cat_arr_id = json_decode($v['category_arr_id'], true) ?? [];
             if (!empty($v['web_url']) || $v['type'] != 1 || $v['pid'] == $del_catid) {
                 $del_catid = $v['category_id'] ?? '';
                 unset($all_cate[$k]);
@@ -4028,12 +4023,17 @@ class PublicRpcService implements PublicRpcServiceInterface
         }
         $all_cate = array_values($all_cate);
         $cate_num = min($cate_num, 24);
-        $all_sector_type = array_column($sectors, 'sector_type');
+        if(isset($sectors[0]['sector_type'])){
+            $all_sector_type = array_column($sectors, 'sector_type');
+        }else{
+            return '此网站没有 通栏类型!';
+        }
         // return $all_cate;
         $grouped = array_reduce(array_keys($sectors), function ($carry, $idx) use ($sectors) {
             $carry[$sectors[$idx]['sector_type']][] = $idx;
             return $carry;
         }, []);
+        // 以通栏分类为索引 随机取一个通栏索引作为其值
         $unipue_sector_keys = array_combine(
             array_keys($grouped),
             array_map(function ($type) use ($grouped) {
@@ -4049,6 +4049,7 @@ class PublicRpcService implements PublicRpcServiceInterface
         $rule_must_type = $rule->must_type;
         $sector_num = $rule->max_num;
         $unique_sector_type = array_flip($unipue_sector_keys);
+        //      必须通栏的类型
         $must_type = array_unique(array_intersect($unique_sector_type, $rule_must_type));
         //      先求交集,去重后统计
         $is_must = count($must_type);
@@ -4071,6 +4072,7 @@ class PublicRpcService implements PublicRpcServiceInterface
 
 
         // 2.非必须通栏的随机处理
+        //      非必需通栏的类型
         $not_must_type = array_unique(array_diff($unique_sector_type, $must_type));
         $total = count($not_must_type);
         $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
@@ -4089,7 +4091,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
             $rand_count = count($random_sector);
         }
-        if ($page == 1) {
+        if ($page == 1 && $rand_count > 0) {
             $random_cat_num = array_sum(array_column($random_sector, 'cat_num'));
             $random_cat_num += $must_cat_num;
             if ($random_cat_num > $cate_num) {
@@ -4100,13 +4102,15 @@ class PublicRpcService implements PublicRpcServiceInterface
         // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
         $repeat_num = $sector_num - $rand_count - $must_count;
         // 组合起来通栏
-        $sector_zuhe1 = array_merge($must_sector, $random_sector);
+        if($rand_count > 0){
+            $sector_zuhe1 = array_merge($must_sector, $random_sector);
+        }else{
+            $sector_zuhe1 = $must_sector;
+        }
         $sector_zuhe1_type = array_column($sector_zuhe1, 'sector_type');
         $sector_ids = array_column($sector_zuhe1, 'id');
         if ($repeat_num > 0) {
             $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;
@@ -4126,7 +4130,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             $max_iterations = 10;
             $iteration_count = 0;
             // 剩余的栏目数量
-            if ($page == 1) {
+            if ($page == 1 && $rand_count > 0) {
                 $sector_cate_comnum = $cate_num - $random_cat_num;
                 if ($sector_cate_comnum < 0) {
                     return '栏目数量过少,无法完成随机!';
@@ -4136,15 +4140,11 @@ class PublicRpcService implements PublicRpcServiceInterface
             // -----新添加的代码-----
             $re_sector = array_intersect($all_sector_type, $repeat_type);
             $re_sector_key = array_keys($re_sector);
-            $every_repeattype_num = array_count_values($re_sector);
+            $every_repeattype_num = count($re_sector);
+
             // $re_sector_key = array_keys($re_sector,1);
             $repaet_key = 0;
             $re_type_keys = [];
-
-
-            $one_sector_key = array_search(1, $re_sector);
-            $two_sector_key = array_search(2, $re_sector);
-            $three_sector_key = array_search(3, $re_sector);
             $all_cat_num = array_column($sectors, 'cat_num');
             $repeat_sector_key = [];
             // $rule_repeat['1'] = 1;
@@ -4163,7 +4163,7 @@ class PublicRpcService implements PublicRpcServiceInterface
                             $re_type_keys[$value] = [];
                         }
                         // 剩余的通栏数量>0  并且 剩余的此类通栏数量>0
-                        if ($repeat_num - $repaet_key > 0 && $rule_repeat[$type_key] - count($re_type_keys[$value]) > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) {
+                        if ($repeat_num - $repaet_key > 0 && $rule_repeat[$type_key] - count($re_type_keys[$value])-1 > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) {
                             // var_dump("0000",array_keys($re_sector,$value));
                             $one_sector_type = array_keys($re_sector, $value);
                             // 若是此类通栏的数量>1  则随机取一个对应的键值
@@ -4233,12 +4233,15 @@ class PublicRpcService implements PublicRpcServiceInterface
                     $sector[$toutiao_key] = $sort_1;
                     $sector[1] = $pic;
                     $sector[$pic_key] = $sort_2;
+                    $place_count = 2;
                 } else if ($pic_key) {
                     $sector[0] = $pic;
                     $sector[$pic_key] = $sort_1;
+                    $place_count = 1;
                 } else {
                     $sector[0] = $toutiao;
                     $sector[$toutiao_key] = $sort_1;
+                    $place_count = 1;
                 }
             }
             if ($data['page'] == '2' || $data['page'] == 2 || in_array(5, $sector_specal_sort)) {
@@ -4249,21 +4252,66 @@ class PublicRpcService implements PublicRpcServiceInterface
                     $sector[$toutiao_key] = $sort_2;
                     $sector[2] = $pic;
                     $sector[$pic_key] = $sort_3;
+                    $place_count = 2;
                 } else if ($pic_key) {
                     $sector[1] = $pic;
                     $sector[$pic_key] = $sort_2;
+                    $place_count = 1;
                 } else if ($toutiao_key) {
                     $sector[1] = $toutiao;
                     $sector[$toutiao_key] = $sort_2;
+                    $place_count = 1;
+                }
+            }
+        }
+        //涉及广告的位置及数量处理
+        // 提取所有 sector_type 值
+        $sector_finsh_type = array_column($sector, 'sector_type');
+        // 统计值为 2 的数量
+        // 使用 array_filter 过滤出值为 2 的元素,再用 count 计算数量
+        $count_type_2 = count(array_filter($sector_finsh_type, fn($v) => $v == 2));
+        $count_not_type_2 = count($sector_finsh_type) - $count_type_2;
+        // 查找所有值为 2 的键名(可能多个)
+        $type_2_keys = array_keys($sector_finsh_type, 2);
+        $ad_sector = $sector[$type_2_keys[0]];
+        foreach ($type_2_keys as $k => $v) {
+            unset($sector[$v]);
+        }
+        $all_sector = [];
+        $idx = 0;
+        $ad = 0;
+        // return $rule;
+        // 将 $rule->type_max 转为数组后再取值,避免把 stdClass 当数组用
+        $typeMax = (array)($rule->type_max ?? []);
+
+        $rull_adtype = $typeMax[2] ?? 0;
+        $sector_num = count($sector);
+        $sectorMax = $rule->max_num ?? [];
+        foreach ($sector as $key => $value) {
+            // 若是 6、7 直接放入
+            if (in_array($value['sector_type'], [6, 7])) {
+                $all_sector[$idx++] = $value;
+            } else {
+                // 其余通栏:隔一个加一个广告通栏
+                if ($idx % 2 == 1 && isset($ad_sector) && $ad < $rull_adtype && $sector_num < $sectorMax) {
+                    $all_sector[$idx++] = $ad_sector;
+                    $sector_num++;
+                    $ad++;
                 }
+                $all_sector[$idx++] = $value;
+            }
+            if(empty($sector) && in_array(2, $rule_must_type)){
+                $all_sector[0] = $ad_sector;
             }
         }
+        // -------------未完待续-----------------
+
         $sector_id = array_column($sector, 'id');
         $y_num = 0;
         $cat_key = 0;
         $ad_key = 0;
-        // return $sector_id;
-        foreach ($sector as $key => $value) {
+        // return $all_sector;
+        foreach ($all_sector as $key => $value) {
             // array_push($value['sort_id'],$key);
             // $sector_id_ = $value['sector_id'];
             $sectorid = $value['id'];
@@ -4380,31 +4428,18 @@ class PublicRpcService implements PublicRpcServiceInterface
                     // 'type_id' => $value['sector_components'][$i]['component']['type_id'],
                 ];
             }
-
-            // $sector_key[$key] = array_keys($value );
-            // $sector_component = [
-            //    'component_type' $value['sector_components'];
-
-            // ];
             $template_data[$key]['componentList'] = $sector_component;
             $canvas_data[$key]['content']['componentList'] = $sector_component;
-
             // $canvas_data[$key]['ad'] = $key;
             $canvas_data[$key]['dataSort'] = $key;
             $canvas_data[$key]['moved'] = false;
             // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
         }
-        // return $canvas_data;
         $datas['component'][0] = [
             'template_data' => $template_data,
             'canvas_data' => $canvas_data,
         ];
         return $datas;
-
-        // return [
-        //     'all_sector'=> array_column($sectors,'sector_name'),
-        //     'sector'=> array_column($sector,'sector_name'),
-        // ];
     }