Browse Source

还原获取首页所有新闻的接口

15313670163 1 day ago
parent
commit
5f9d3c2429
1 changed files with 175 additions and 169 deletions
  1. 175 169
      app/JsonRpc/NewsService.php

+ 175 - 169
app/JsonRpc/NewsService.php

@@ -980,178 +980,184 @@ class NewsService implements NewsServiceInterface
         return Result::success($category);
         return Result::success($category);
     } 
     } 
     /**
     /**
-    * 模块新闻加强版 
-    * @param array $data
-    * @return array
-   */
-    public function getWebsiteAllArticle(array $data): array
-    {
-        
-        // $redisKey = 'website_cache';
-        // $this->redis->hSet($redisKey, $data['website_id'], json_encode($data));
-        // $result = $this->redis->sadd($redisKey);
-         // 使用缓存键
-        // $cacheKey = $data['website_id'];
-        
-        //  // 尝试从缓存获取数据
-         $container = \Hyperf\Context\ApplicationContext::getContainer();
-         $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
-         // 修正传入的字符串,将单引号替换为双引号
-         $input['id'] = $data['id'];
-         $input['website_id'] = $data['website_id'];
-         
-         // 将 JSON 字符串转换为 PHP 数组
-         $items = json_decode($input['id'], true);
-         
-         if (!is_array($items)) {
-             return Result::error("无效的JSON格式", 0);
-         }
-         
-         $website = [
-             'website_id' => $input['website_id'],
-         ];
-         foreach ($items as $key=>$item) {
-            if (isset($item['parent']) && $item['parent'] !== "") {
-                 $parentParams = explode(',', $item['parent']);
-                 if(isset($parentParams) && $parentParams[0]!== ""){
-                     $parentCatId[$key] = $parentParams[0];
-                 }
-            }else{
-                return Result::error("缺少必需参数错误", 0);
+  * 模块新闻加强plus版 
+  * @param array $data
+  * @return array
+  */
+  public function getWebsiteAllArticle(array $data): array
+  {
+      // 修正传入的字符串,将单引号替换为双引号
+      $input['id'] = $data['id'];
+      $input['website_id'] = $data['website_id'];
+      // 将 JSON 字符串转换为 PHP 数组
+      $data = json_decode($input['id'], true);
+      // 使用 array_map 处理每个元素
+      $result = array_map(function ($item) use ($input) {
+        // 检查parent元素是否存在且不是undefined
+        if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "" ) {
+          $count = explode(',', $item['parent']);
+          if(count($count)!=3){
+            return Result::error("参数错误",0);
+          }
+          list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
+          $website = [
+            'website_id' => $input['website_id'],
+          ];
+          // 查询栏目名称
+          $categroy = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin','type']);
+          // 查询图片新闻
+          if(isset($parentImgNum) && $parentImgNum!=0 && !empty($categroy)){
+            $imgArticles = Article::where('article.status', 1)
+            ->where(function($query) use ($parentCatId) {
+              $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
+                  ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
+            })
+            ->where(function ($query) use ($website) {
+              $query->where(function ($subQuery) use ($website) {
+                $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 1")
+                  ->orWhereNull("ignore_ids");
+              });
+            })
+            ->where('imgurl', '!=', '')
+            ->leftJoin('website_category', function($join) use ($website) {
+              $join->on('article.catid', '=', 'website_category.category_id')
+                ->where('website_category.website_id', '=', $website['website_id']);
+            })
+            ->select(
+              'article.id','article.title','article.imgurl','article.author','article.updated_at',
+              'article.introduce','article.islink','article.linkurl','article.copyfrom','article.cat_arr_id',
+              'article.catid','website_category.alias as category_name',
+            )
+            ->orderBy('updated_at', 'desc')
+            ->limit($parentImgNum)
+            ->get();  
+            //调取封装路由的方法,处理新闻路由  
+            if(!empty($imgArticles)){
+              $imgArticles = $this->processArticles($imgArticles , $website);
             }
             }
-
-        }
-        $pid = WebsiteCategory::whereIn('category_id',$parentCatId)->where($website)->pluck('pid')->toArray();
-        // return Result::success($pid);
-        if(!empty($pid)){
-            $pid = array_values(array_unique($pid));
-            if(count($pid)==1){
-                $cacheKey = $data['website_id'].','.$pid[0];
-            }else{
-                return Result::error("参数传递错误", 0);
+          }
+          // 查询文字新闻
+          if(isset($parentTextNum) && $parentTextNum!=0 && !empty($categroy)){ 
+            $textArticles = [];
+            $textArticles = Article::where('article.status', 1)
+              ->where(function($query) use ($parentCatId) {
+                $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
+                    ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
+              })
+              ->where(function ($query) use ($website) {
+                $query->where(function ($subQuery) use ($website) {
+                  $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 1")
+                    ->orWhereNull("ignore_ids");
+                });
+              })
+              ->leftJoin('website_category', function($join) use ($website) {
+                $join->on('article.catid', '=', 'website_category.category_id')
+                  ->where('website_category.website_id', '=', $website['website_id']);
+              })
+              ->select(
+                'article.id','article.title','article.author','article.updated_at','article.cat_arr_id',
+                'article.introduce','article.islink','article.linkurl','article.copyfrom',
+                'article.catid','website_category.alias as category_name',
+              )
+              ->orderBy('updated_at', 'desc')
+              ->limit($parentTextNum)
+              ->get();
+            if(!empty($textArticles)){
+              $textArticles = $this->processArticles($textArticles , $website);
             }
             }
+          }
+        }
+        $resultItem = [
+          'alias' => $categroy ? $categroy->alias : null,
+          'category_id' => $categroy ? $categroy->category_id : null,
+          'pinyin' => $categroy ? $categroy->aLIas_pinyin : null,
+          'imgnum' => $imgArticles ?? [],
+          'textnum' => $textArticles ?? [],
+        ];
+        if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
+          list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
+          $childCategory = WebsiteCategory::where($website)
+            ->where("category_id", $childCatId)
+            ->whereRaw("JSON_CONTAINS(category_arr_id, '".intval($parentCatId)."') = 1")
+            ->first(['alias', 'category_id','aLIas_pinyin','pid','type','category_arr_id as cat_arr_id']);
+          if (!empty($childCategory)) {
+            $childCategory = $this->processArticles($childCategory, $website);
+            // 查询此层级的所有子栏目
+            $all_childcat = WebsiteCategory::where($website)
+            ->where("pid", $childCategory['pid'])
+            ->whereRaw("JSON_CONTAINS(category_arr_id, '".intval($parentCatId)."') = 1")
+            ->get(['alias', 'category_id','aLIas_pinyin','pid','type','category_arr_id as cat_arr_id']);
+            $all_childcat = $this->processArticles($all_childcat, $website);
+            // 查询子栏目图片新闻
+            if( isset($childImgNum) && $childImgNum!=0 ){
+              $childImgArticles = Article::where('catid', $childCatId)
+              ->where('status', 1)
+              ->where(function ($query) use ($website) {
+                $query->where(function ($subQuery) use ($website) {
+                  $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 1");
+                })->orWhereNull("ignore_ids");
+              })
+              ->select(
+                'article.id',
+                'article.title',
+                'article.imgurl',
+                'article.author',
+                'article.updated_at',
+                'article.introduce',
+                'article.islink',
+                'article.linkurl',
+                'article.catid',
+                'article.cat_arr_id',
+                'article.copyfrom',)
+              ->where('imgurl', '!=', '')
+                ->orderBy('updated_at', 'desc')
+                ->limit($childImgNum)
+                ->get();
+              $childImgArticles = $this->processArticles($childImgArticles, $website);
+            }
+            // 查询子栏目文字新闻
+            if( isset($childTextNum) && $childTextNum!=0 ){
+              $childTextArticles = Article::where('catid', $childCatId)
+                ->where('status', 1)
+                ->where(function ($query) use ($website) {
+                  $query->where(function ($subQuery) use ($website) {
+                    $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 1");
+                  })->orWhereNull("ignore_ids");
+                })
+                ->select(
+                  'article.id',
+                  'article.title',
+                  'article.author',
+                  'article.updated_at',
+                  'article.introduce',
+                  'article.islink',
+                  'article.linkurl',
+                  'article.catid',
+                  'article.cat_arr_id',
+                  'article.copyfrom',)
+                ->orderBy('updated_at', 'desc')
+                ->limit($childTextNum)
+                ->get();
+              $childTextArticles = $this->processArticles($childTextArticles, $website); 
+            }
+          }
+          $resultItem['child'] = [
+            'alias' => $childCategory ? $childCategory->alias : null,
+            'category_id' => $childCategory ? $childCategory->category_id : null,
+            'type' => $childCategory ? $childCategory->type : null,
+            'pinyin' => $childCategory ? $childCategory->pinyin : null,
+            'all_childcat' => $all_childcat ?? [],
+            'imgnum' => $childImgArticles ?? [],
+            'textnum' => $childTextArticles ?? [],
+          ]; 
+        }else{
+          $resultItem['child'] = [];
         }
         }
-        // return Result::success($cacheKey);
-         if ($cachedData = $cache->get($cacheKey)) {
-             return Result::success(unserialize($cachedData));
-         }
-        try {
-            
-
-            // 提前缓存常用查询结果,减少重复查询
-            $categoryCache = [];
-            $childCategoryCache = [];
-
-            // 使用 array_map 处理每个元素
-            $result = array_map(function ($item) use ($website, &$categoryCache, &$childCategoryCache) {
-                $resultItem = [
-                    'alias' => null,
-                    'category_id' => null,
-                    'pinyin' => null,
-                    'imgnum' => [],
-                    'textnum' => [],
-                    'child' => []
-                ];
-                
-                // 处理父级栏目
-                if (isset($item['parent']) && $item['parent'] !== 'undefined' && $item['parent'] !== "") {
-                    $parentParams = explode(',', $item['parent']);
-                    
-                    if (count($parentParams) !== 3) {
-                        return Result::error("父级栏目:缺少必需参数错误", 0);
-                    }
-                    
-                    list($parentCatId, $parentImgNum, $parentTextNum) = $parentParams;
-                    $cacheKey = "parent_{$parentCatId}_{$website['website_id']}";
-
-                    if (!isset($categoryCache[$cacheKey])) {
-                        // 查询栏目名称
-                        $categoryCache[$cacheKey] = WebsiteCategory::where('category_id', $parentCatId)
-                            ->where($website)
-                            ->first(['alias', 'category_id', 'aLIas_pinyin', 'type']);
-                    }
-                    
-                    $category = $categoryCache[$cacheKey];
-
-                    if (!empty($category)) {
-                        $resultItem['alias'] = $category->alias;
-                        $resultItem['category_id'] = $category->category_id;
-                        $resultItem['pinyin'] = $category->aLIas_pinyin;
-                        
-                        // 查询图片新闻
-                        if (!empty($parentImgNum)) {
-                            $resultItem['imgnum'] = $this->fetchArticles($parentCatId, $website, (int)$parentImgNum, true);
-                        }
-                        
-                        // 查询文字新闻
-                        if (!empty($parentTextNum)) {
-                            $resultItem['textnum'] = $this->fetchArticles($parentCatId, $website, (int)$parentTextNum, false);
-                        }
-                    }
-                }
-                
-                // 处理子级栏目
-                if (isset($item['child']) && $item['child'] !== 'undefined' && $item['child'] !== "") {
-                    $childParams = explode(',', $item['child']);
-                    
-                    if (count($childParams) !== 3) {
-                        return Result::error("子级栏目:缺少必需参数错误", 0);
-                    }
-                    
-                    list($childCatId, $childImgNum, $childTextNum) = $childParams;
-                    $cacheKey = "child_{$childCatId}_{$website['website_id']}";
-
-                    if (!isset($childCategoryCache[$cacheKey])) {
-                        // 查询子栏目信息
-                        $childCategoryCache[$cacheKey] = WebsiteCategory::where($website)
-                            ->where("category_id", $childCatId)
-                            ->whereRaw("JSON_CONTAINS(category_arr_id, '".intval($childCatId)."') = 1")
-                            ->first(['alias', 'category_id', 'aLIas_pinyin', 'pid', 'category_arr_id as cat_arr_id']);
-                    }
-                    
-                    $childCategory = $childCategoryCache[$cacheKey];
-
-                    if (!empty($childCategory)) {
-                        $childCategory = $this->processArticles($childCategory, $website);
-                        
-                        // 查询此层级的所有子栏目
-                        $allChildCategories = WebsiteCategory::where($website)
-                            ->where("pid", $childCategory['pid'])
-                            ->get(['alias', 'category_id', 'aLIas_pinyin', 'pid', 'category_arr_id as cat_arr_id']);
-                        
-                        $allChildCategories = $this->processArticles($allChildCategories, $website);
-                        
-                        $childResult = [
-                            'alias' => $childCategory->alias,
-                            'category_id' => $childCategory->category_id,
-                            'pinyin' => $childCategory->aLIas_pinyin,
-                            'all_childcat' => $allChildCategories,
-                            'imgnum' => [],
-                            'textnum' => []
-                        ];
-                        
-                        // 查询子栏目图片新闻
-                        if (!empty($childImgNum)) {
-                            $childResult['imgnum'] = $this->fetchArticles($childCatId, $website, (int)$childImgNum, true);
-                        }
-                        
-                        // 查询子栏目文字新闻
-                        if (!empty($childTextNum)) {
-                            $childResult['textnum'] = $this->fetchArticles($childCatId, $website, (int)$childTextNum, false);
-                        }
-                        
-                        $resultItem['child'] = $childResult;
-                    }
-                }
-                return $resultItem;
-            }, $items);
-            $cache->set($cacheKey, serialize($result), 86400);
-            return Result::success($result);
-        } catch (\Exception $e) {
-            // 记录错误日志
-            return Result::error("获取网站文章失败: " . $e->getMessage());
-        }
-    }
+      return $resultItem;
+    }, $data);
+    return Result::success($result);
+    // return Result::success($data);
+}
 
 
 /**
 /**
  * 辅助方法:获取文章列表
  * 辅助方法:获取文章列表