Bläddra i källkod

Merge branch 'web_sannong_fr'

15313670163 1 vecka sedan
förälder
incheckning
ba6750c3a1
1 ändrade filer med 13 tillägg och 29 borttagningar
  1. 13 29
      app/JsonRpc/NewsService.php

+ 13 - 29
app/JsonRpc/NewsService.php

@@ -1446,25 +1446,22 @@ class NewsService implements NewsServiceInterface
      */
     public function getWebsiteAllArticle(array $data): array
     {
+        $startTime = microtime(true); // 记录开始时间
         try {
             // 解析输入数据
             $input = [
                 'id' => $data['id'] ?? '',
                 'website_id' => $data['website_id'] ?? 0
             ];
-            
             // 验证必要参数
             if (empty($input['website_id'])) {
                 return Result::error("缺少网站ID参数", 0);
             }
-            
             // 将 JSON 字符串转换为 PHP 数组
             $items = json_decode($input['id'], true);
-            
             if (json_last_error() !== JSON_ERROR_NONE || !is_array($items)) {
                 return Result::error("无效的JSON格式", 0);
             }
-            
             // 提取所有需要查询的分类ID
             $parentCatIds = [];
             $childCatIds = [];
@@ -1476,7 +1473,6 @@ class NewsService implements NewsServiceInterface
                         $parentCatIds[] = $parts[0];
                     }
                 }
-                
                 if (isset($item['child']) && $item['child'] && $item['child'] !== 'undefined') {
                     $parts = explode(',', $item['child']);
                     if (count($parts) === 3) {
@@ -1484,15 +1480,12 @@ class NewsService implements NewsServiceInterface
                     }
                 }
             }
-            
             // 去重分类ID
             $parentCatIds = array_unique($parentCatIds);
             $childCatIds = array_unique($childCatIds);
-            
             // 批量查询分类信息
             $websiteCondition = ['website_id' => $input['website_id']];
             $categories = $this->getCategoriesByIds(array_merge($parentCatIds, $childCatIds), $websiteCondition);
-            
             // 处理每个项目
             $result = [];
             foreach ($items as $item) {
@@ -1504,25 +1497,19 @@ class NewsService implements NewsServiceInterface
                     'textnum' => [],
                     'child' => []
                 ];
-                
                 // 处理父级栏目
                 if (isset($item['parent']) && $item['parent'] && $item['parent'] !== 'undefined') {
                     $parts = explode(',', $item['parent']);
-                    
                     if (count($parts) !== 3) {
                         return Result::error("父级栏目:缺少必需参数错误", 0);
                     }
-                    
                     list($parentCatId, $parentImgNum, $parentTextNum) = $parts;
-                    
                     // 从已查询的分类中获取信息
                     $category = $categories[$parentCatId] ?? null;
-                    
                     if ($category) {
                         $resultItem['alias'] = $category->alias;
                         $resultItem['category_id'] = $category->category_id;
                         $resultItem['pinyin'] = $category->alias_pinyin;
-                        
                         // 查询图片新闻
                         if ($parentImgNum > 0) {
                             $resultItem['imgnum'] = $this->getArticles(
@@ -1532,7 +1519,6 @@ class NewsService implements NewsServiceInterface
                                 true
                             );
                         }
-                        
                         // 查询文字新闻
                         if ($parentTextNum > 0) {
                             $resultItem['textnum'] = $this->getArticles(
@@ -1547,16 +1533,12 @@ class NewsService implements NewsServiceInterface
                 // 处理子级栏目
                 if (isset($item['child']) && $item['child'] && $item['child'] !== 'undefined') {
                     $parts = explode(',', $item['child']);
-                    
                     if (count($parts) !== 3) {
                         return Result::error("子级栏目:缺少必需参数错误", 0);
                     }
-                    
                     list($childCatId, $childImgNum, $childTextNum) = $parts;
-                    
                     // 从已查询的分类中获取信息
                     $childCategory = $categories[$childCatId] ?? null;
-                    
                     if ($childCategory) {
                         $childData = [
                             'alias' => $childCategory->alias,
@@ -1566,12 +1548,13 @@ class NewsService implements NewsServiceInterface
                             'imgnum' => [],
                             'textnum' => []
                         ];
-                        // 查询此层级的所有子栏目
-                        $childData['all_childcat'] = $this->getChildCategories(
-                            $childCategory->pid,
-                            $input['website_id']
-                        );
-                        
+                        if(isset($childCategory->pid) && $childCategory->pid != 0){
+                            // 查询此层级的所有子栏目
+                            $childData['all_childcat'] = $this->getChildCategories(
+                                $childCategory->pid,
+                                $input['website_id']
+                            );
+                        }
                         // 查询子栏目图片新闻
                         if ($childImgNum > 0) {
                             $childData['imgnum'] = $this->getArticles(
@@ -1590,14 +1573,16 @@ class NewsService implements NewsServiceInterface
                                 false
                             );
                         }
-                        
                         $resultItem['child'] = $childData;
                     }
                 }
-                
                 $result[] = $resultItem;
+                // 检查是否超时
+                $elapsedTime = microtime(true) - $startTime;
+                if ($elapsedTime > 5) {
+                    return Result::error("请求处理时间已超过5秒,已经达到".$elapsedTime, 0);
+                }
             }
-            
             return Result::success($result);
         } catch (\Exception $e) {
             return Result::error("处理请求时出错: " . $e->getMessage(), 0);
@@ -1656,7 +1641,6 @@ class NewsService implements NewsServiceInterface
             )
             ->orderBy('updated_at', 'desc')
             ->limit($limit);
-            
         if ($isImage) {
             $query->where('imgurl', '!=', '');
         }