Przeglądaj źródła

修改随机模板及获取友情链接列表的接口

FengR 1 miesiąc temu
rodzic
commit
bb0a834f79
2 zmienionych plików z 438 dodań i 106 usunięć
  1. 22 27
      app/JsonRpc/LinkService.php
  2. 416 79
      app/JsonRpc/PublicRpcService.php

+ 22 - 27
app/JsonRpc/LinkService.php

@@ -16,33 +16,28 @@ class LinkService implements LinkServiceInterface
      */
     public function getLinkList(array $data): array
     {
-            $result = Link::when($data, function ($query) use ($data) {
-                    if (isset($data['title']) && !empty($data['title'])) {
-                        $query->where('link.title', 'like', '%' . trim($data['title']) . '%');
-                    }
-                    if (isset($data['website_id']) && !empty($data['website_id'])) {
-                        $query->where('link.website_id', '=', $data['website_id']);
-                    }
-                })
-                ->leftJoin("website","website.id","link.website_id")
-                ->select("link.*","website.website_name")
-                ->limit($data['pageSize'])
-                ->offset(($data['page']-1)*$data['pageSize'])
-                ->orderBy("link.updated_at","desc")
-                ->paginate(intval($data['pageSize']),
-                    [
-                        'link.*',
-                        'website.website_name'
-                    ],
-                    'page', intval($data['page']));
-            if(empty($result)){
-                return Result::error("没有查到数据");
-            }
-            $result = [
-                'rows'=>$result->items(),
-                'count'=>$result->total()
-            ];
-            return Result::success($result);
+        $query = Link::when($data, function ($query) use ($data) {
+                if (isset($data['title']) && !empty($data['title'])) {
+                    $query->where('link.title', 'like', '%' . trim($data['title']) . '%');
+                }
+                if (isset($data['website_id']) && !empty($data['website_id'])) {
+                    $query->where('link.website_id', '=', $data['website_id']);
+                }
+            })
+            ->leftJoin('website', 'link.website_id', '=', 'website.id')
+            ->select("link.*",'website.website_name')
+            ->orderBy("link.updated_at","desc")
+            ->orderBy("link.id");
+        $result['count'] = $query->count();
+        $result['rows'] = $query->offset(($data['page']-1)*$data['pageSize'])
+            ->limit($data['pageSize'])
+            ->get()
+            ->all();
+        if(empty($result)){
+            return Result::error("没有查到数据");
+        }
+        
+        return Result::success($result);
     }
 
     /**

+ 416 - 79
app/JsonRpc/PublicRpcService.php

@@ -53,6 +53,7 @@ use think\controller\Rest;
 use App\Model\Aichat;
 use App\Model\AichatSession;
 use App\Model\TemplateDraftbox;
+use App\JsonRpc\WebsiteServer;
 #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class PublicRpcService implements PublicRpcServiceInterface
 {
@@ -3199,7 +3200,8 @@ class PublicRpcService implements PublicRpcServiceInterface
         }
         $cate_num = min($cate_num, 24);
         // return Result::success($data);
-        // $tempalte['index'] = $this->randomWeb($data,$index_rule);
+        $tempalte['index'] = $this->randomWeb($data,$rule);
+        return Result::success($tempalte);
         if(isset($data['page']) && !empty($data['page'])){
             $tempalte['index'] = $this->randomPage($data,$cate_num,$rule);
         }else{ 
@@ -3863,84 +3865,419 @@ 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();
-        // $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;
-    // }
+    public function randomWeb($data,$rule)
+    {
+        $page = $data['page'];
+        // 在使用 with 方法关联查询其他表时限制查询字段,不需要修改 Sector 的 model 文件,
+        // 可以直接在查询时通过 select 方法指定要查询的字段。以下是修改后的代码:
+        // 修正:去掉多余的英文分号,保持链式调用完整
+        $sectors = Sector::select('sector.id', 'sector.template_id', 'sector.page_type','sector.sector_id','sector.cat_num'
+            ,'sector.sector_name','sector.sector_type','sector.sector_width','sector.sector_height','sector.pic_height',
+            'sector.component_num')
+            ->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();
+        
+        // 将模型对象转换为数组,确保关系字段正确命名
+        $sectors = $sectors->toArray();
+        
+        // 提取并返回 component_imgs 内容,添加空安全检查
+        // return $sectors[0]['sector_components'][0]['component']['component_imgs'] ?? [];
+        // return $sectors;
+
+        $query = WebsiteCategory::where('website_id', $data['website_id']);
+        // $cate_num = $query->where('is_show', 1)->count();
+        $cat_column = [
+            'website_id' => $data['website_id'],
+            'is_show' => 1,
+            'placeid' => 1,
+            'num' => 23,
+            'pid' => 0,
+        ];
+        
+        // 临时注入 WebsiteService,后续请改为属性注入
+        // 本项目中已存在 WebsiteServer 及其 getWebsiteModelCategory 方法,直接调用
+        $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) {
+            return Result::error('此网站栏目过少,无法进行随机模板!');
+        }
+        $cate_num = min($cate_num, 24);
+        $all_sector_type = array_column($sectors, 'sector_type');
+        // return $all_sector_type;
+        $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) {
+                    // 随机取一个索引
+                    return $grouped[$type][array_rand($grouped[$type])];
+                },array_keys($grouped))
+        );
+        //      通栏类别: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]}
+        // 1. 必需通栏:同一类型只随机保留一条
+        $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);
+        if ($is_must != count(array_unique($must_type))) {
+            return '此页面缺少必要通栏!';
+        }
+        $must_sector = array_values(array_intersect_key($sectors,$must_type));
+        $must_count = count($must_sector);
+        //      目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量
+        if ($must_count > $sector_num) {
+            return $must_sector;
+        }
+        //      必需通栏中的栏目数量
+        if($page == 1){
+            $must_cat_num = array_sum(array_column($must_sector,'cat_num'));
+            if($must_cat_num>$cate_num){
+                return '栏目数量过少,无法完成必需通栏随机!';
+            }
+        }
+
+
+        // 2.非必须通栏的随机处理
+        $not_must_type = array_unique(array_diff($unique_sector_type, $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];
+            }
+            $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
+            $rand_count = count($random_sector);
+        }
+        if($page == 1){
+            $random_cat_num = array_sum(array_column($random_sector,'cat_num'));
+            $random_cat_num += $must_cat_num;
+            if($random_cat_num>$cate_num){
+                return '栏目数量过少,无法完成随机!';
+            }
+        }
+
+        // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
+        $repeat_num = $sector_num - $rand_count - $must_count;
+        // 组合起来通栏
+        $sector_zuhe1 = array_merge($must_sector, $random_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;
+                }
+            }
+            // var_dump("repeat_num", $repeat_num);
+            // 剩余通栏进行重复处理
+            $rule_repeat = (array)$rule->type_max;
+            // 取到可以重复的通栏类别
+            $rule_repaet_type =  array_keys($rule_repeat);
+            $repeat_type = array_intersect($rule_repaet_type, $unique_sector_type);
+            // 获取可以随机重复的通栏
+            $repaet_key = 0;
+            $type = [];
+            // return $rule_repaet_type;
+            // 防止无限循环,设置最大迭代次数
+            $max_iterations = 10;
+            $iteration_count = 0;
+            // 剩余的栏目数量
+            if($page == 1){
+                $sector_cate_comnum = $cate_num - $random_cat_num;
+                if($sector_cate_comnum < 0){
+                    return '栏目数量过少,无法完成随机!';
+                }
+            }       
+            
+            // -----新添加的代码-----
+            $re_sector = array_intersect($all_sector_type,$repeat_type);
+            $re_sector_key = array_keys($re_sector);
+            $every_repeattype_num = array_count_values($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;
+            // return $rule_repeat;
+            // $three_sector_key = array_search(3,$re_sector);
+            // $min_one_sector = max(array_column('cat_num',$all_sector));
+            // 随机重复通栏-----
+            // $rule_repeat['1'] = 1;
+                   //             // 找出 all_sector_type 中值为 1 的键
+            while ($repaet_key <= $repeat_num-1 && $iteration_count < $max_iterations) {
+                // 剩余通栏数量>0   若不是首页则继续;若是首页则需要判断剩余栏目数量是否>3
+                if ($repeat_num-$repaet_key > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) {
+                    foreach($repeat_type as $key => $value){
+                        $type_key = strval($value);
+                        if(!isset($re_type_keys[$value])){
+                            $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))){
+                            // var_dump("0000",array_keys($re_sector,$value));
+                            $one_sector_type = array_keys($re_sector,$value);
+                            // 若是此类通栏的数量>1  则随机取一个对应的键值
+                            if(count($one_sector_type) > 1){
+                                // 从 $one_sector_type 数组中随机取一个键名,并取其对应值
+                                $cat_key = $one_sector_type[array_rand($one_sector_type)];
+                            }else{
+                                $cat_key = $one_sector_type[0];
+                            }
+                            $repeat_sector_key[$repaet_key] = $cat_key;
+                            $repaet_key++;
+                            // var_dump("1115----------cat_key",$cat_key);
+                            if($page == 1){
+                                $sector_cate_comnum -= $all_cat_num[$cat_key];
+                            }
+                            if(!in_array($repaet_key,$re_type_keys[$value])){
+                                $re_type_keys[$value][] = $repaet_key;
+                                // var_dump("1117----------re_type_keys",$re_type_keys);
+                            }                           
+                        }else{
+                            break;
+                        }
+                    }
+                } else {
+                    break;
+                }
+                $iteration_count++;
+            }
+            // 随机的重复通栏键值转换成真正的通栏
+            $repeat_sector = [];
+            if (isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)) {
+                foreach ($repeat_sector_key as $key => $value) {
+                    if (isset($sectors[$value])) {
+                        $repeat_sector[$key] = $sectors[$value];
+                    }else{
+                        continue;
+                    }
+                }
+            }
+            $repeat_count = count($repeat_sector);
+            $sector = array_merge($repeat_sector,$sector_zuhe1);
+        }else{
+            $sector = $sector_zuhe1;
+        }
+        // var_dump("sector_ids",$sector_ids);
+        //  
+        // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
+
+        // var_dump("sector",$sector);
+
+
+        // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
+        $sector_specal_sort = array_column($sector,'sector_type');
+        if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){
+            $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
+            $pic_key = array_search(7,$sector_specal_sort) ?? null;
+            $cat_key = array_search(5,$sector_specal_sort) ?? null;
+            $toutiao = $sector[$toutiao_key];
+            $pic =  $sector[$pic_key];
+            $cat =  $sector[$cat_key];
+            $sort_1 = $sector[0];
+            $sort_2 = $sector[1];
+            $sort_3 = $sector[2];
+            if($data['page'] == 1 || $data['page'] == '1'){
+                if($toutiao_key && $pic_key){
+                    $sector[0] = $toutiao;
+                    $sector[$toutiao_key] = $sort_1;
+                    $sector[1] = $pic;
+                    $sector[$pic_key] = $sort_2;
+                }else if($pic_key){
+                    $sector[0] = $pic;
+                    $sector[$pic_key] = $sort_1;
+                }else{
+                    $sector[0] = $toutiao;
+                    $sector[$toutiao_key] = $sort_1;
+                }
+            }
+            if ($data['page'] == '2' || $data['page'] == 2 || in_array(5, $sector_specal_sort)) {
+                $sector[0] = $cat;
+                $sector[$cat_key] = $sort_1;
+                if ($toutiao_key && $pic_key) {
+                    $sector[1] = $toutiao;
+                    $sector[$toutiao_key] = $sort_2;
+                    $sector[2] = $pic;
+                    $sector[$pic_key] = $sort_3;
+                }else if ($pic_key) {
+                    $sector[1] = $pic;
+                    $sector[$pic_key] = $sort_2;
+                }else if($toutiao_key){
+                    $sector[1] = $toutiao;
+                    $sector[$toutiao_key] = $sort_2;
+                }
+            }
+        }
+        $sector_id = array_column($sector,'id');
+        $y_num = 0;
+        $cat_key = 0;
+        $ad_key = 0;
+        // return $sector_id;
+        foreach ($sector as $key => $value) {
+            // array_push($value['sort_id'],$key);
+            // $sector_id_ = $value['sector_id'];
+            $sectorid = $value['id'];
+            $template_data[$key] = [
+                'sectorName' => $value['sector_id'],
+                'sort' => $key + 1,
+                // 'com_num' => $value['component_num'],
+            ];
+            $date = date('mdHisu'); // 获取时分秒微秒,转换为纯数字格式
+            // $date = 11010630021;
+            if (substr($date, 0, 1) == 0) {
+                $date = substr($date, 0, 10); // 截取前9位
+            } else {
+                $date = substr($date, 0, 9); // 截取前9位
+            }
+            $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
+            $y_num = $y_num + $value['pic_height'];
+            $sector_width = $value['sector_width'] / 100;
+            $canvas_data[$key] = [
+                'i' => $random_num,
+                'x' => 0,
+                'y' => $y_num,
+                'w' => $sector_width,
+                'h' => $value['pic_height'],
+                'type' => $value['sector_id'],
+                'content' => [
+                    'sectorName' => $value['sector_id'],
+                ]
+            ];
+            // $componentData = [];
+            $sector_component = [];
+            for ($i = 0; $i < $value['component_num']; $i++) {
+                // $component_data = [];
+                $componentData = [];
+                $component_imgs = $value['sector_components'][$i]['component']['component_imgs'] ?? [];
+                $component_type = $value['sector_components'][$i]['component']['type_id'] ?? 0;
+                $component_data = json_decode($value['sector_components'][$i]['component']['component_data'] ?? '[]',true) ?? [];
+                $list_type = json_decode($value['sector_components'][$i]['component']['component_column'] ?? '[]',true) ?? [];
+                if(!empty($component_data) && !empty($list_type)){
+                    $componentData = $component_data['componentData'];
+                    $componentData['listType'] = $list_type['listType'];
+                }
+                if(in_array($component_type,[7,14,15,23,24]) && ($page == 1 || $page == 2)){
+                    if($page == 1){
+                        if(in_array($component_type,[23,24])){
+                            $componentData[0]['name'] = $all_cate[$cat_key]['name'];
+                            $componentData[0]['category_arr'] = $all_cate[$cat_key]['category_arr_id'];
+                            $componentData[1]['name'] = $all_cate[$cat_key+1]['name'];
+                            $componentData[1]['category_arr'] = $all_cate[$cat_key+1]['category_arr_id'];
+                            $cat_key += 2;
+                        }else{
+                            $componentData['name'] = $all_cate[$cat_key]['name'];
+                            $componentData['category_arr'] = $all_cate[$cat_key]['category_arr_id'];
+                            $cat_key ++;
+                        }
+                    }
+
+                    if($page == 2){
+                        $componentData['name'] = '自动生成';
+                    }
+                }
+                
+                if(!empty($component_imgs)){
+                    $component_imgids = array_column($component_imgs,'img_id');
+                    $component_imgid= array_rand($component_imgids);
+                }
+                if(in_array($component_type,[11,14,24])){
+                    $ad_data = json_decode($value['sector_components'][$i]['component']['ad'] ?? '[]',true) ?? [];
+                    $component_imgkey = array_search($component_imgid,$component_imgids);
+                    $ad_data['thumb'] = $component_imgs[$component_imgkey]['img_url'];
+                    if($component_type == 24){
+                        $ad[0] = $ad_data;
+                        $ad[1] = $ad_data;
+                    }else{
+                        $ad = $ad_data;
+                    }
+                    $template_data[$key]['ad'] = $ad;
+                    $canvas_data[$key]['ad'] = $ad;
+                }
+                $sector_component[$i] = [
+                    'component_type' => $value['sector_components'][$i]['component']['component_type'],
+                    'sort' => $value['sector_components'][$i]['sort_id'],
+                    'type_id' => $component_type,
+                    'page' => $page,
+                    'component_style' => $component_imgids[array_rand($component_imgids)], // 若是上个数组为[1]随机为0,原因:array_rand 在单元素数组时必然返回 0
+                    'componentData' => $componentData,
+
+                    // '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;
+        return [
+            'template_data' => $template_data,
+            'canvas_data' => $canvas_data,
+        ];
+        
+        return [
+            'all_sector'=> array_column($sectors,'sector_name'),
+            'sector'=> array_column($sector,'sector_name'),
+        ];
+    }
     /**
      * 自助建站-获取所有通栏、组件、组件样式
      */