200, 'msg' => 'success', 'data' => [ 'time' => $time, 'data' => $data, ] ]; //mysql直接查询连接数 $activeConnections = Db::select("SHOW STATUS LIKE 'Threads_connected'"); // $activeConnectionsCount = $activeConnections[0]->Value ?? 0; // $users = Db::select('SELECT * FROM article;'); return ['code' => 200, 'msg' => 'success', 'data' => $activeConnections]; } public function indexData(array $data): array { $time1 = microtime(true); $data = json_decode($data['template'], true); $wetbsite_id = $data['base']['website_id'] ?? 2; //设置缓存 $websiteInfoCacheKey = "awebsite:category:{$wetbsite_id}"; $websiteInfo = $this->redis->get($websiteInfoCacheKey); //false; if ($websiteInfo === false) { // Redis 中没有缓存,从数据库中查询 $websiteInfo = Db::table('website_category')->where('website_id', $wetbsite_id)->get()->toArray(); // 将查询结果存入 Redis,设置缓存时间为 3600 秒(1 小时) $this->redis->setex($websiteInfoCacheKey, 3600, json_encode($websiteInfo)); } else { // 从 Redis 中获取的数据需要反序列化 $websiteInfo = json_decode($websiteInfo, true); } $websiteInfo = Db::table('website_category')->where('website_id', $wetbsite_id)->get()->toArray(); //取出website var_dump($websiteInfo, 'websiteInfo'); //取出category_id 对应的数据 $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id'); // 取出category_id 对应的aLIas_pinyin $categoryIds = array_column($websiteInfo, 'category_id'); $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin'); $cat_arr = array_combine($categoryIds, $aliasPinyins); // var_dump($cat_arr, 'cat_arr'); //根据 category_arr_id 组合出 aLIas_pinyin $catiall = []; //一级所有子级的记录 $cat_1st_arr = []; foreach ($websiteInfo as $key => $value) { //算出路由拼音 $category_arr_id = json_decode($value->category_arr_id); $pinyin_str = ''; foreach ($category_arr_id as $k => $v) { $pinyin_str .= $cat_arr[$v] . '/'; } $pinyin_str = rtrim($pinyin_str, '/'); $catiall[$value->category_id][] = $pinyin_str; // $cat_id = $value->category_id; $websiteInfoIndexed[$value->category_id]->pinyin = $pinyin_str; // 算出一级 并且算出子级 if ($value->pid == 0) { $cat_1st_arr[$value->category_id] = []; } else { $cat_1st_arr[$value->pid][] = $value->category_id; } } $time2 = microtime(true); $time_ = ($time2 - $time1); var_dump($time2, $time1, $time_, '返回路由需要多少时间'); //获取 $getpage = $data['base']['getpage'] ?? 'index'; // index = 首页 // class = 频道页 // list = 列表页 // article = 详情页 // search = 搜索页 // aboutList = 底部导航列表页 // aboutArticle = 底部导航详情页 // return $cat_1st_arr; // return $cat_1st_arr; // var_dump($cat_1st_arr, 'cat_1st_arr');//一级所有子级的记录 // var_dump($catiall, 'catiall');//拼音 $templateData = []; switch ($getpage) { case 'index': $componetList = $data['template']['index'][0]['componentList']; //计算一级的所有子级;组成一个数据,key是一级的category_id,value是所有的子级数据,加上个二级的pinyin属性,放的是catiall的数据 foreach ($componetList as $key => $value) { //取出componentData $componentData = $value['componentData']; // $page = $componentData['page']; // $pageSize = $componentData['pageSize']; $listType = $componentData['listType']; $category_id = $componentData['category_id']; $imgSize = $componentData['imgSize']; $textSize = $componentData['textSize']; $level = $componentData['level']; $child = $componentData['child']; //获取子级 if ($child) { } $child_id = $child['id']; $child_imgSize = $child['imgSize']; $child_textSize = $child['textSize']; // 拼音就是路由---- unset($listType['pinyin']); //查询查询数据,返回到data字段里,根据这几个 if ($category_id > 0) { // 第一次查询:imgurl 不为空的数据 $imgArticles = Db::table('article') // ->whereIn('catid', [$category_id]) ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id])) ->where('status', 1) ->whereNotNull('imgurl') ->limit($imgSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0]; } // 第二次查询:imgurl 为空的数据 $textArticles = Db::table('article') // ->whereIn('catid', [$category_id]) ->whereIn('catid', $temp_arr = array_merge([$category_id], $cat_1st_arr[$category_id])) ->where('status', 1) ->whereNull('imgurl') ->limit($textSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0]; } } var_dump($level, 'level'); if ($level > 0) { // 取出一级的所有子级 //取出所有的子级的imgurl 不为空的数据 $imgArticles = Db::table('article') // ->where('level', 'like', "%$level%") // ->whereJsonContains('level', [$level]) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level]) ->where('status', 1) ->whereIn('catid', $categoryIds) ->whereNotNull('imgurl') ->limit($imgSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } //取出所有的子级的imgurl 为空的数据 $textArticles = Db::table('article') ->where('status', 1) // ->where('level', 'like', "%$level%") // ->whereJsonContains('level', [$level]) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level]) ->whereIn('catid', $categoryIds) ->whereNull('imgurl') ->limit($textSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //将查询到的数据放到componentData里 $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['category_id'] = $category_id; $componetList[$key]['componentData']['data']['textnum'] = $textArticles; $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles; //判断child if ($child_id > 0) { //取出子级的imgurl 不为空的数据 $child_imgArticles = Db::table('article') ->where('catid', $child_id) ->whereNotNull('imgurl') ->limit($child_imgSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //取出子级的imgurl 为空的数据 $child_textArticles = Db::table('article') ->where('catid', $child_id) ->whereNull('imgurl') ->limit($child_textSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //将查询到的数据放到componentData里 $componetList[$key]['componentData']['data']['child']['alias'] = $websiteInfoIndexed[$child_id]->alias; $componetList[$key]['componentData']['data']['child']['pinyin'] = $websiteInfoIndexed[$child_id]->pinyin; $componetList[$key]['componentData']['data']['child']['category_id'] = $child_id; $componetList[$key]['componentData']['data']['child']['textnum'] = $child_textArticles; $componetList[$key]['componentData']['data']['child']['imgnum'] = $child_imgArticles; //取出自己 $all_childcat = $cat_1st_arr[$category_id]; $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed) { // 从 $websiteInfoIndexed 中获取对应的数据 $info = $websiteInfoIndexed[$v]; // 返回一个包含所需信息的数组 return [ 'pinyin' => $info->pinyin, 'alias' => $info->alias, 'category_id' => $info->category_id, 'aLIas_pinyin' => $info->aLIas_pinyin, 'pid' => $info->pid, 'cat_arr_id' => $info->category_arr_id ?? ['出错'], ]; }, $all_childcat); $componetList[$key]['componentData']['data']['child']['all_childcat'] = $processedChildCat ?? []; } } //将修改后的数据放到template里 $data['template']['index'][0]['componentList'] = $componetList; break; case 'class': $componetList = $data['template']['class']['data'][0]['componentList']; $class_count = count($componetList); $parent_id = $data['template']['class']['parent_id'] ?? 0; //获取子级; $child = $cat_1st_arr[$parent_id]; //获取子级 // var_dump($child, 'child'); //如果$child的数量小于$class_count,就填充一样多的数据给child,如果大于就截取一样的数量给child if (count($child) < $class_count) { $child = array_pad($child, $class_count, end($child) ?? 0); } elseif (count($child) > $class_count) { $child = array_slice($child, 0, $class_count); } foreach ($componetList as $key => $value) { $componentData = $value['componentData']; //取出imgSize和textSize,获取相应的文章, $category_id = $child[$key] ?? 0; $imgSize = $componentData['imgSize'] ?? 0; $textSize = $componentData['textSize'] ?? 0; $listType = $componentData['listType'] ?? []; var_dump($listType, 'before unset listType'); // 定义要移除的元素数组 $valuesToRemove = ['category_name', 'pinyin']; // 循环移除每个元素 foreach ($valuesToRemove as $value) { while (($key = array_search($value, $listType)) !== false) { // 移除该元素 unset($listType[$key]); } } var_dump($listType, 'listType'); //child $child = $componentData['child'] ?? []; $data_class = []; //查询imgurl不为空的数据 $imgArticles = Db::table('article') ->where('catid', $category_id) ->whereNotNull('imgurl') ->limit($imgSize) // ->select($listType) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } //查询imgurl为空的数据 $textArticles = Db::table('article') ->where('catid', $category_id) ->whereNull('imgurl') ->limit($textSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } $data_class['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id; $data_class['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id; $data_class['category_id'] = $category_id; $data_class['textnum'] = $textArticles; $data_class['imgnum'] = $imgArticles; $data['data'] = $data_class; } break; case 'list': $componetListdata = $data['template']['list']['data']; $category_id = $data['template']['list']['category_id'] ?? 0; foreach ($componetListdata as $k => $v) { if (!isset($v['componentList']) || !is_array($v['componentList'])) { continue; // 如果 componentList 不存在或不是数组,跳过当前循环 } $componentList = &$data['template']['list']['data'][$k]['componentList']; var_dump(count($componentList), 'componentList111'); foreach ($componentList as $key => $value) { $componentData = $value['componentData']; if (isset($value['componentData']['category_id'])) { $value['componentData']['category_id'] = $category_id; $page = $componentData['papageTypege']['page'] ?? 1; $pageSize = $componentData['papageTypege']['pageSize'] ?? 10; $listType = $componentData['listType']; $articals = Db::table('article') ->where('catid', $category_id) ->where('status', 1) ->orderBy('updated_at', 'desc') ->get(); // ->paginate($pageSize, ['*'], 'page', $page); //拼接上拼音 foreach ($articals as $k => $v) { $articals[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } $value['componentData']['data'] = $articals; } var_dump(isset($value['componentData']['level']), $key, '---------------------------------------'); if (isset($value['componentData']['level'])) { //取出imgSize和textSize,获取相应的文章, $imgSize = $componentData['imgnum'] ?? 0; $textSize = $componentData['textnum'] ?? 0; $listType = $componentData['listType'] ?? []; // 定义要移除的元素数组 $valuesToRemove = ['category_name', 'pinyin']; // 循环移除每个元素 foreach ($valuesToRemove as $value) { while (($key = array_search($value, $listType)) !== false) { // 移除该元素 unset($listType[$key]); } } //查询imgurl不为空的数据 $imgArticles = Db::table('article') ->where('catid', $category_id) ->whereNotNull('imgurl') ->limit($imgSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } //查询imgurl为空的数据 $textArticles = Db::table('article') ->where('catid', $category_id) ->whereNull('imgurl') ->limit($textSize) ->orderBy('updated_at', 'desc') ->get() ->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } //将查询到的数据放到componentData里 $componetList[$key]['componentData']['data']['alias'] = $websiteInfoIndexed[$category_id]->alias ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['pinyin'] = $websiteInfoIndexed[$category_id]->pinyin ?? 'aliasnull' . $category_id; $componetList[$key]['componentData']['data']['category_id'] = $category_id; $componetList[$key]['componentData']['data']['textnum'] = $textArticles; $componetList[$key]['componentData']['data']['imgnum'] = $imgArticles; //将修改后的数据放到template里 // $data['template']['list']['data'][0]['componentList'] = $componetList; } } } break; case 'article': $article_id = 51434; //假设这是文章ID $componetListdata = &$data['template']['article']['data']; var_dump($categoryIds, '----------------'); // $category_id = $data['template']['article']['category_id'] ?? 0; foreach ($componetListdata as $k => $v) { foreach ($v['componentList'] as $key => $value) { $componentData = $value['componentData']; if (isset($value['componentData']['article_id'])) { $value['componentData']['article_id'] = $article_id; // $page = $componentData['papageTypege']['page'] ?? 1; // $pageSize = $componentData['papageTypege']['pageSize'] ?? 10; $listType = $componentData['listType']; //查询数据 $articals = Db::table('article') ->where('id', $article_id) ->where('status', 1) ->leftJoin('article_data', 'article.id', '=', 'article_data.article_id') ->select('article.*', 'article_data.content') ->orderBy('updated_at', 'desc') ->get(); //拼接上拼音 $value['componentData']['data'] = $articals; } if (isset($value['componentData']['level'])) { //:1:头条 2:轮播图 3:推荐图 4:最新新闻;5:推荐新闻;6:热点资讯 7:自动关联 //4 update 5 30天之内 6 库中 $level = $componentData['level'] ?? 0; $imgSize = $componentData['imgnum'] ?? 0; $textSize = $componentData['textnum'] ?? 0; if (in_array($level, [1, 2, 3, 6])) { //查询imgurl不为空的数据 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level]) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } //查询imgurl为空的数据 $textArticles = Db::table('article')->where('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) ->whereRaw('MATCH(level_text) AGAINST(? IN NATURAL LANGUAGE MODE)', [$level]) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } else if ($level == 4) { //查询imgurl不为空的数据 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 if (!empty($imgArticles)) { foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //查询imgurl为空的数据 $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 var_dump('---4------'); if (!empty($textArticles)) { foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } else if ($level == 5) { var_dump($value['componentData']['level'], 'level'); //查询imgurl不为空的数据 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) //最近三十天 ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days'))) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 if (!empty($imgArticles)) { foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } //查询imgurl为空的数据 $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) ->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-30 days'))) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 var_dump('---5------'); if (!empty($textArticles)) { foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } else if ($level == 7) { //获取artical的keyword $keword = DB::table('artical')->where('id', $article_id)->select('keyword')->get(); $kewordl = $keword[0]->keyword; $kewordl = explode(',', $kewordl); //筛选出符合条件的article来 $imgArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNotNull('imgurl') ->where('status', 1) // ->whereJsonContains('keyword', $kewordl) ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl]) ->limit($imgSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($imgArticles as $k => $v) { $imgArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } $textArticles = Db::table('article')->whereIn('catid', $categoryIds)->whereNull('imgurl') ->where('status', 1) // ->whereJsonContains('keyword', $kewordl) ->whereRaw('MATCH(keyword) AGAINST(? IN NATURAL LANGUAGE MODE)', [$kewordl]) ->limit($textSize)->orderBy('updated_at', 'desc')->get()->toArray(); //拼接上拼音 foreach ($textArticles as $k => $v) { $textArticles[$k]->pinyin = $catiall[$v->catid][0] ?? '====='; } } } $value['componentData']['data']['imgnum'] = $imgArticles ?? []; $value['componentData']['data']['textnum'] = $textArticles ?? []; } } break; case 'search': break; case 'aboutList': $fact_id = 7; $componetList = &$data['template']['aboutList']['componentData']; $componetList['fact_id'] = $fact_id; $content = Db::table('footer_content') ->where('fcat_id', $fact_id) ->orderBy('updated_at', 'desc') ->get() ->toArray(); $componetList['data'] = $content; break; case 'aboutArticle': $fact_id = 7; $componetList = &$data['template']['aboutList']['componentData']; $componetList['fact_id'] = $fact_id; $content = Db::table('footer_content') ->where('id', $fact_id) ->orderBy('updated_at', 'desc') ->get() ->toArray(); $componetList['data'] = $content; break; default: $componetList = []; break; } return $data; } public function indexData1(array $data): array { $time1 = microtime(true); // 解析模板数据 $templateData = json_decode($data['template'], true); $website_id = $templateData['base']['website_id'] ?? 2; $componentList = $templateData['template']['index'][0]['componentList']; // 1️⃣ 获取网站分类信息 $websiteInfo = Db::table('website_category') ->where('website_id', $website_id) ->get() ->toArray(); // 构建索引和拼音映射 $websiteInfoIndexed = array_column($websiteInfo, null, 'category_id'); $categoryIds = array_column($websiteInfo, 'category_id'); $aliasPinyins = array_column($websiteInfo, 'aLIas_pinyin'); $cat_arr = array_combine($categoryIds, $aliasPinyins); // 构建 catiall 和 cat_1st_arr $catiall = []; $cat_1st_arr = []; foreach ($websiteInfo as $item) { $arrId = json_decode($item->category_arr_id); $pinyin_str = implode('/', array_map(fn($id) => $cat_arr[$id], $arrId)); $pinyin_str = rtrim($pinyin_str, '/'); $catiall[$item->category_id][] = $pinyin_str; $websiteInfoIndexed[$item->category_id]->pinyin = $pinyin_str; if ($item->pid == 0) { $cat_1st_arr[$item->category_id] = []; } else { $cat_1st_arr[$item->pid][] = $item->category_id; } } // 2️⃣ 提前聚合所有需要查询的 category_id 和限制参数 $allCatIds = []; $imgLimits = []; $txtLimits = []; $childImgLimits = []; $childTxtLimits = []; foreach ($componentList as $item) { $componentData = $item['componentData']; $category_id = $componentData['category_id'] ?? 0; if ($category_id > 0) { $allCatIds[] = $category_id; $imgLimits[$category_id] = $componentData['imgSize'] ?? 0; $txtLimits[$category_id] = $componentData['textSize'] ?? 0; if (!empty($cat_1st_arr[$category_id])) { $allCatIds = array_merge($allCatIds, $cat_1st_arr[$category_id]); } } // 子级 $child = $componentData['child'] ?? null; if ($child && isset($child['id']) && $child['id'] > 0) { $child_id = $child['id']; $childImgLimits[$child_id] = $child['imgSize'] ?? 0; $childTxtLimits[$child_id] = $child['textSize'] ?? 0; } } $allCatIds = array_unique(array_filter($allCatIds)); // 3️⃣ 一次性查询所有 article 数据 $allArticles = Db::table('article') ->whereIn('catid', $allCatIds) ->where('status', 1) ->get() ->toArray(); // 构建按 imgurl 分类的文章集合 $articlesByCat = ['img' => [], 'txt' => []]; foreach ($allArticles as $article) { if (!empty($article->imgurl)) { $articlesByCat['img'][$article->catid][] = $article; } else { $articlesByCat['txt'][$article->catid][] = $article; } } // 4️⃣ 使用协程并发处理每个组件 $results = []; foreach ($componentList as $key => $value) { $componentKey = $key; $componentData = $value['componentData']; Coroutine::create(function () use (&$results, $componentKey, $componentData, $websiteInfoIndexed, $cat_1st_arr, $catiall, $articlesByCat, $imgLimits, $txtLimits, $childImgLimits, $childTxtLimits) { $result = []; $category_id = $componentData['category_id'] ?? 0; $imgLimit = $imgLimits[$category_id] ?? 0; $txtLimit = $txtLimits[$category_id] ?? 0; if ($category_id > 0) { // 图文数据 $imgArticles = array_slice($articlesByCat['img'][$category_id] ?? [], 0, $imgLimit); foreach ($imgArticles as &$v) { $v->pinyin = $catiall[$v->catid][0] ?? ''; } // 纯文字数据 $textArticles = array_slice($articlesByCat['txt'][$category_id] ?? [], 0, $txtLimit); foreach ($textArticles as &$v) { $v->pinyin = $catiall[$v->catid][0] ?? ''; } $result['imgnum'] = $imgArticles; $result['textnum'] = $textArticles; $result['alias'] = $websiteInfoIndexed[$category_id]['alias'] ?? ''; $result['pinyin'] = $websiteInfoIndexed[$category_id]['pinyin'] ?? ''; $result['category_id'] = $category_id; } // 子级处理 $child = $componentData['child'] ?? null; if ($child && isset($child['id']) && $child['id'] > 0) { $child_id = $child['id']; $c_imgLimit = $childImgLimits[$child_id] ?? 0; $c_txtLimit = $childTxtLimits[$child_id] ?? 0; $childImg = array_slice($articlesByCat['img'][$child_id] ?? [], 0, $c_imgLimit); foreach ($childImg as &$v) { $v->pinyin = $catiall[$v->catid][0] ?? ''; } $childText = array_slice($articlesByCat['txt'][$child_id] ?? [], 0, $c_txtLimit); foreach ($childText as &$v) { $v->pinyin = $catiall[$v->catid][0] ?? ''; } $result['child'] = [ 'alias' => $websiteInfoIndexed[$child_id]['alias'] ?? '', 'pinyin' => $websiteInfoIndexed[$child_id]['pinyin'] ?? '', 'category_id' => $child_id, 'textnum' => $childText, 'imgnum' => $childImg, 'all_childcat' => array_map(fn($v) => $websiteInfoIndexed[$v], $cat_1st_arr[$category_id] ?? []), ]; } $results[$componentKey] = $result; }); } // 等待所有协程完成(生产环境应使用 WaitGroup) usleep(200000); // 示例中简单等待 // 5️⃣ 回填结果到 componentList foreach ($componentList as $key => &$component) { $componentData = $component['componentData']; $category_id = $componentData['category_id'] ?? 0; if (isset($results[$key])) { $component['componentData']['data'] = $results[$key]; } } // 6️⃣ 返回最终数据 $templateData['template']['index'][0]['componentList'] = $componentList; return $templateData; } }