Browse Source

修改模块新闻加强plus版 一级栏目获取的新闻可能为父级或子级栏目的新闻并获取到了他的路由;当传递参数为undefind时,返回的子级数组为空

15313670163 5 days ago
parent
commit
ce5d3cd51d
1 changed files with 116 additions and 91 deletions
  1. 116 91
      app/JsonRpc/NewsService.php

+ 116 - 91
app/JsonRpc/NewsService.php

@@ -1007,92 +1007,115 @@ class NewsService implements NewsServiceInterface
         $data = json_decode($input['id'], true);
         // 使用 array_map 处理每个元素
         $result = array_map(function ($item) use ($input) {
-            list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
-            $website = [
-                'website_id' => $input['website_id'],
-            ];
-            // 查询栏目名称
-            $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
-            if(empty($category)){
-                $imgArticles = [];
-                $textArticles = [];
-                // return Result::error("暂无此栏目",0);
-            }else{
-                $parent_alias = $category->alias ?? '';
-                $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
-                // 查询图片新闻
-                $imgArticles = Article::where('catid', $parentCatId)
-                ->where('status', 1)
-                ->where(function ($query) use ($website) {
-                    $query->where(function ($subQuery) use ($website) {
-                        $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
-                    })->orWhereNull("ignore_ids");
-                })
-                ->where('imgurl', '!=', '')
-                ->select(
-                    'article.id',
-                    'article.title',
-                    'article.imgurl',
-                    'article.author',
-                    'article.updated_at',
-                    'article.introduce',
-                    'article.islink',
-                    'article.linkurl',
-                    'article.copyfrom',
-                    DB::raw("'$parent_pinyin' as pinyin") // 添加 pinyin 字段
-                )
-                ->orderBy('updated_at', 'desc')
-                ->limit($parentImgNum)
-                ->get()->all();
-
-                // 查询文字新闻
-                $textArticles = Article::where('catid', $parentCatId)
-                    ->where('status', 1)
-                    ->where(function ($query) use ($website) {
-                        $query->where(function ($subQuery) use ($website) {
-                            $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
-                        })->orWhereNull("ignore_ids");
-                    })
-                    ->select(
-                        'article.id',
-                        'article.title',
-                        'article.author',
-                        'article.updated_at',
-                        'article.introduce',
-                        'article.islink',
-                        'article.linkurl',
-                        'article.copyfrom',
-                         DB::raw("'$parent_pinyin' as pinyin"))
-                    ->orderBy('updated_at', 'desc')
-                    ->limit($parentTextNum)
-                    ->get()->all();
+            // 检查parent元素是否存在且不是undefined
+            if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
+                list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
+                $website = [
+                    'website_id' => $input['website_id'],
+                ];
+                // 查询栏目名称
+                $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
+                if(empty($category)){
+                    $parent_alias = '';
+                    $parent_pinyin = null;
+                    $imgArticles = [];
+                    $textArticles = [];
+                }else{
+                    $parent_alias = $category->alias ?? '';
+                    $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
+                    if($parentImgNum!=0){
+                        // 查询图片新闻
+                    $imgArticles = Article::where('article.status', 1)
+                        ->where(function ($query) use ($website, $parentCatId) {
+                            $query->where(function ($subQuery) use ($website, $parentCatId) {
+                                $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
+                                ->whereRaw("JSON_CONTAINS(article.cat_arr_id, '".intval($parentCatId)."') = 0");
+                            })->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.catid',
+                            'website_category.alias as category_name',
+                            DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin' 
+                                    ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
+                        )
+                        ->orderBy('updated_at', 'desc')
+                        ->limit($parentImgNum)
+                        ->get()->all();
+                        // 查询文字新闻
+                    }
+                    if($parentTextNum!=0){  
+                        $textArticles = Article::where('article.status', 1)
+                        ->where(function ($query) use ($website, $parentCatId) {
+                            $query->where(function ($subQuery) use ($website, $parentCatId) {
+                                $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
+                                ->whereRaw("JSON_CONTAINS(article.cat_arr_id, '".intval($parentCatId)."') = 0");
+                            })->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.imgurl',
+                            'article.author',
+                            'article.updated_at',
+                            'article.introduce',
+                            'article.islink',
+                            'article.linkurl',
+                            'article.copyfrom',
+                            'article.catid',
+                            'website_category.alias as category_name',
+                            DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin' 
+                                    ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
+                        )
+                        ->orderBy('updated_at', 'desc')
+                        ->limit($parentTextNum)
+                        ->get()->all();
+                    }
+                }
+                
             }
             $resultItem = [
-                'alias' => $parent_alias,
-                'category_id' => $parentCatId,
-                'pinyin' => $parent_pinyin,
-                'imgnum' => $imgArticles,
-                'textnum' => $textArticles,
-                
+                'alias' => $parent_alias ?? '',
+                'category_id' => $parentCatId ?? 0,
+                'pinyin' => $parent_pinyin ?? null,
+                'imgnum' => $imgArticles ?? [],
+                'textnum' => $textArticles ?? [],
             ];
-
-            if (!empty($item['child']) && $item['child'] != "") {
+            if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
                 $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
                 $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
                     ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
                     ->get()->all();
-                if ($childCategory) {
+                if (!empty($childCategory)) {
                     list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
                     // 查询子栏目名称
-                $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
-                    ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
-                    ->first();
-                $child_pinyin = $childCategoryInfo->aLIas_pinyin ? $childCategoryInfo->aLIas_pinyin : null;
-                    if(empty($childCategoryInfo)){
+                    $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
+                        ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
+                        ->first();
+                    if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
                         $childImgArticles = [];
                         $childTextArticles = [];
+                        $resultItem['child'] = [];
                     }else{
-                         // 查询子栏目图片新闻
+                        $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
+                        // 查询子栏目图片新闻
                         $childImgArticles = Article::where('catid', $childCatId)
                             ->where('status', 1)
                             ->where(function ($query) use ($website) {
@@ -1112,10 +1135,10 @@ class NewsService implements NewsServiceInterface
                                 'article.copyfrom',
                                 DB::raw("'$child_pinyin' as pinyin"))
                             ->where('imgurl', '!=', '')
-                            ->orderBy('updated_at', 'desc')
-                            ->limit($childImgNum)
-                            ->get()->all();
-                        // 查询子栏目文字新闻
+                                ->orderBy('updated_at', 'desc')
+                                ->limit($childImgNum)
+                                ->get()->all();
+                            // 查询子栏目文字新闻
                         $childTextArticles = Article::where('catid', $childCatId)
                             ->where('status', 1)
                             ->where(function ($query) use ($website) {
@@ -1136,23 +1159,25 @@ class NewsService implements NewsServiceInterface
                             ->orderBy('updated_at', 'desc')
                             ->limit($childTextNum)
                             ->get()->all();
+                        $resultItem['child'] = [
+                            'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
+                            'category_id' => $childCatId,
+                            'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
+                            'all_childcat' => $childCategory,
+                            'imgnum' => $childImgArticles,
+                            'textnum' => $childTextArticles,
+                        ];   
                     }
-                    $resultItem['child'] = [
-                        'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
-                        'category_id' => $childCatId,
-                        'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
-                        'all_childcat' => $childCategory,
-                        'imgnum' => $childImgArticles,
-                        'textnum' => $childTextArticles,
-                        
-                    ];
+                   
                     // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
                 }
+              
+            }else{
+                $resultItem['child'] = [];
             }
-
-            return $resultItem;
-        }, $data);
-        return Result::success($result);
+        return $resultItem;
+    }, $data);
+    return Result::success($result);
                 // return Result::success($data);
     }