Ver Fonte

随即模板

rkljw há 4 meses atrás
pai
commit
c2362c51d6
3 ficheiros alterados com 160 adições e 17 exclusões
  1. 143 16
      app/JsonRpc/PublicRpcService.php
  2. 4 0
      app/Model/ComponentImg.php
  3. 13 1
      app/Model/Sector.php

+ 143 - 16
app/JsonRpc/PublicRpcService.php

@@ -2,6 +2,7 @@
 
 namespace App\JsonRpc;
 
+// use ___PHPSTORM_HELPERS\object;
 use _PHPStan_62c6a0a8b\OndraM\CiDetector\Env;
 use App\Model\BlackWord;
 use App\Model\Department;
@@ -2732,8 +2733,12 @@ class PublicRpcService implements PublicRpcServiceInterface
             default:
                 break;
         }
+        $cate_num = WebsiteCategory::where('website_id',$data['website_id'])
+        ->where('is_show',1)
+        ->count();
         // return Result::success($index_rule);
-        $tempalte['index'] = $this->randomPage($data,$data['page'],$index_rule);
+        // $tempalte['index'] = $this->randomWeb($data,$index_rule);
+        $tempalte['index'] = $this->randomPage($data,$cate_num,$index_rule);
         // $tempalte['class'] = $this->randomPage($data,2,$class_rule);
         // $tempalte['list'] = $this->randomPage($data,3);
         // $tempalte['article'] = $this->randomPage($data,4);
@@ -2743,12 +2748,13 @@ class PublicRpcService implements PublicRpcServiceInterface
         // $index = $this->randomPage($data);
         return Result::success($tempalte);
     }
-    public function randomPage($data,$page,$rule){
+    public function randomPage($data,$cate_num,$rule){
         // return $rule;
+        $page = $data['page'];
         $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')  
+        'sector.sector_type','sector.sector_code','sector.id','sector.sector_width','sector.component_num')  
         ->orderBy('id')
         ->get()->all();
         if(empty($sectors)){
@@ -2772,7 +2778,7 @@ class PublicRpcService implements PublicRpcServiceInterface
 
             return $result;
         }, []);
-        // return $sector_types;
+        // return [$sector_type_keys,$sector_types];
         $must_type = $rule->must_type;
         $sector_num = $rule->max_num;
         // 获取必需通栏类别与查询到的通栏类别的交集数量;
@@ -2786,7 +2792,51 @@ class PublicRpcService implements PublicRpcServiceInterface
         // return $must_type;
 
 
-        // 非必须通栏的随机处理
+        // 1.必需通栏
+        $must_type = array_intersect($all_sector_types,$must_type);
+        // return $must_type;
+        // 此代码用于从 $sectors 数组中筛选出必需的通栏数据。
+        // 1. array_keys($must_type) 获取 $must_type 数组的所有键名
+        // 2. array_flip() 将这些键名作为值,生成一个新数组
+        // 3. array_intersect_key() 比较 $sectors 和上述新数组的键名,返回键名相同的元素
+        // 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{
+        //             return '此网站首页显示栏目过少,请添加首页显示栏目!';
+        //         }                
+        //     }else{
+        //         $cate_num = $cate_num-$must_sector_catenum;
+        //     }
+        // }
+        return $must_sector;
+        // 2.非必须通栏的随机处理
         $not_must_type = array_diff($all_sector_types,$must_type);
         
         $total = count($not_must_type);
@@ -2830,17 +2880,7 @@ class PublicRpcService implements PublicRpcServiceInterface
         var_dump("rand_count",$rand_count);
         // return $random_sector;
 
-        // 必需通栏
-        $must_type = array_intersect($all_sector_types,$must_type);
-        // return $must_type;
-// 此代码用于从 $sectors 数组中筛选出必需的通栏数据。
-// 1. array_keys($must_type) 获取 $must_type 数组的所有键名
-// 2. array_flip() 将这些键名作为值,生成一个新数组
-// 3. array_intersect_key() 比较 $sectors 和上述新数组的键名,返回键名相同的元素
-// 4. array_values() 重新索引返回的数组,使其键名从 0 开始连续递增
-// 最终得到的 $must_sector 数组包含了 $sectors 中对应 $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;
@@ -3311,4 +3351,91 @@ class PublicRpcService implements PublicRpcServiceInterface
             'canvas_data' => $canvas_data,
         ];
     }
+    // public function randomWeb($data,$index_rule)
+    // {
+    //     // 在使用 with 方法关联查询其他表时限制查询字段,不需要修改 Sector 的 model 文件,
+    //     // 可以直接在查询时通过 select 方法指定要查询的字段。以下是修改后的代码:
+    //     $component_img = Sector::select('sector.id', 'sector.template_id', 'sector.page_type','sector.sector_id','sector.component_num'
+    //     ,'sector.sector_name','sector.sector_type')
+    //         ->where('sector.template_id', $data['template_id'])
+    //         ->where('sector.page_type', 'like', '%' . $data['page'] . '%')
+    //         ->with([
+    //             'sectorComponents' => function ($query) use ($data) {
+    //                 $query->select('sectorid', 'sort_id', 'component_id')
+    //                     ->with([
+    //                         'component' => function ($q) use ($data) {
+    //                             $q->select('id', 'component_type', 'component_data', 'component_code', 'component_column', 'ad','type_id')
+    //                             ->with([
+    //                                 'componentImgs' => function ($subQ) use ($data) {
+    //                                     $subQ
+    //                                     ->where('template_id', $data['template_id'])
+    //                                     // ->select('id', 'img_id', 'img_name', 'img_url', 'template_id')
+    //                                         ;
+    //                                 }
+    //                             ]);
+    //                         }
+    //                     ]);
+    //             }
+    //         ])
+    //         ->get()
+    //         ->all();
+    //     // 通栏类别:1:资讯类:2:广告类;3:混合类;4:搜索框类;5:导航类;6:头条类;
+    //     // 7:轮播图类;8:静态资源类;9:单页(列表)类;10:单页(详情)类;11:列表类;12:详情类;13:栏目轮播图类;',
+    //     // {"max_num": 10, "min_num": 6, "and_type": [6, 7], 
+    //     // "type_max": {"1": 20, "2": 4, "3": 3}, "must_type": [1, 2]}
+    //     $rule = is_array($index_rule) ? $index_rule : (array)$index_rule;
+    //     // return gettype($rule);
+    //     $must_num = count($rule['must_type']);
+    //     $sector_type = array_column($component_img,'sector_type');
+    //     $notmust_sectortype = count(array_diff(array_unique(array_column($component_img,'sector_type')),$rule['must_type']));
+    //     $notmust_num = random_int(0,$notmust_sectortype);
+    //     $min_num = $notmust_sectortype+$must_num;
+    //     $max_num = $rule['max_num'];
+    //     $index_num = random_int($min_num, $max_num);
+    //     $repeat_num = $index_num - $must_num - $notmust_num;
+    //     $category_num = WebsiteCategory::where('website_id',$data['website_id'])
+    //     ->where('is_show',1)
+    //     ->get()
+    //     ->all();
+    //     $category_num = count($category_num);
+    //     // ->count();
+    //     $sectorids = array_column($component_img,'id');
+    //     // $components = SectorComponent::whereIn('sector_component.sectorid',$sectorids)
+    //     //     ->leftJoin('component','component.component_type','sector_component.component_id')
+    //     //     ->select('sector_component.sectorid','sector_component.sort_id','component.component_type','component.type_id')
+    //     //     ->get()
+    //     //     ->all();
+    //     // $num = 0;
+    //     // $sector_ids = [];
+    //     // $sector_sortids = [];
+    //     // foreach($components as $key => $value){
+    //     //     if(!in_array($value['sectorid'],$sector_ids)){
+    //     //         $num = 0;
+    //     //     }
+    //     //     if($value['type_id'] == 7 && !in_array($value['sort_id'],$sector_sortids[$value['sectorid']])){
+    //     //         $num++;
+    //     //     }
+    //     //      $sector_typenum[$value['sectorid']]['num'] = $num;
+    //     //     $sector_typenum[$value['sectorid']][$value['sort_id']] = $num;
+    //     //     $sector_ids[$key] = $value['sectorid'];
+    //     //     $sector_sortids[$value['sectorid']][$key] = $value['sort_id'];
+    //     // }
+    //     foreach($sector_type as $key => $value){
+    //         if($value == 1){
+    //                 $sector_type[$key] = 1;
+    //         }
+    //     }
+    //     return [
+    //         'index_num' => $index_num,
+    //         'min_num' => $min_num,
+    //         '$sector_type' => $sector_type,
+    //         'notmust_sectortype' => $notmust_sectortype,
+    //         'must_num' => $must_num,
+    //         'notmust_num' => $notmust_num,
+    //         'repeat_num' => $repeat_num,
+    //         // '$sector_typenum' => $sector_typenum,
+    //     ];
+    //     // return $component_img;
+    // }
+
 }

+ 4 - 0
app/Model/ComponentImg.php

@@ -26,4 +26,8 @@ class ComponentImg extends Model
     protected array $casts = [];
 
     protected array $guarded = [];
+     public function componentImgs()
+    {
+        return $this->belongsTo(ComponentImg::class, 'component_id', 'component_type');
+    }
 }

+ 13 - 1
app/Model/Sector.php

@@ -24,4 +24,16 @@ class  Sector extends Model
      * The attributes that should be cast to native types.
      */
     protected array $casts = [];
-}
+
+    /**
+     * 定义与 sectorComponents 的关联关系
+     *
+     * @return \Hyperf\Database\Model\Relations\HasMany|\Hyperf\Database\Model\Relations\BelongsTo|\Hyperf\Database\Model\Relations\HasOne
+     * 注意:这里需要根据实际业务确定具体的关联类型,示例中暂未明确指定
+     */
+    public function sectorComponents()
+    {
+        // 请根据实际业务逻辑修改关联类型,如 hasMany、belongsTo、hasOne 等
+        return $this->hasMany(SectorComponent::class, 'sectorid', 'id');
+    }
+}