瀏覽代碼

根据多级栏目问题,对涉及新闻的接口进行修改(获取首页新闻、获取列表页新闻、搜索新闻、获取头条新闻)并封装获取新闻路由的接口

15313670163 4 天之前
父節點
當前提交
118b8f82ee
共有 1 個文件被更改,包括 152 次插入251 次删除
  1. 152 251
      app/JsonRpc/NewsService.php

+ 152 - 251
app/JsonRpc/NewsService.php

@@ -305,9 +305,8 @@ class NewsService implements NewsServiceInterface
         // return Result::success($data['website_id']);
         $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
         $category = array_values(array_unique($category->toArray()));
-       
+        $web['website_id'] = $data['website_id'];
         if ($category) {
-            $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
             $where = [
                 'status' => 1,
             ];
@@ -337,22 +336,8 @@ class NewsService implements NewsServiceInterface
                                 ->limit($data['textnum']);
                         })
                         ->orderBy('updated_at', 'desc')
-                        ->get()
-                        ->map(function ($article ) use ($data) {
-                            $catid = $article->catid;
-                            $pinyin = '';
-                            $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
-                            if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
-                                $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
-                                if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
-                                    $pinyin = $childCategory->aLIas_pinyin ?  $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
-                                }
-                            } else {
-                                $pinyin = $category->aLIas_pinyin ?  $category->aLIas_pinyin : null;
-                            }
-                            $article->pinyin = $pinyin;
-                            return $article;
-                        });
+                        ->get();
+                        $result['level7'] = $this->processArticle($result['level7'],$web);
                         if (empty($result)) {
                             return Result::success();
                         }
@@ -405,26 +390,12 @@ class NewsService implements NewsServiceInterface
                     'article.islink',
                     'article.linkurl',
                     'article.copyfrom',
+                    'article.cat_arr_id',
                     'article.catid')
                     ->offset($data['placeid'])
                     ->limit($data['imgnum'])
-                    ->get()
-                    ->map(function ($article ) use ($data) {
-                        $catid = $article->catid;
-                        $pinyin = '';
-                        $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
-                        if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
-                            $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
-                            if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
-                                $pinyin = $childCategory->aLIas_pinyin ?  $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
-                            }
-                        } else {
-                            $pinyin = $category->aLIas_pinyin ?  $category->aLIas_pinyin : null;
-                        }
-                        $article->pinyin = $pinyin;
-                        return $article;
-                    });
-                // $result['img'] = $this->processArticles($img_article, $input); 
+                    ->get();
+                $result['img'] = $this->processArticles( $result['img'], $web); 
             }
             
             // 处理文字新闻查询
@@ -440,26 +411,12 @@ class NewsService implements NewsServiceInterface
                     'article.islink',
                     'article.linkurl',
                     'article.copyfrom',
+                    'article.cat_arr_id',
                     'article.catid')
                     ->offset($data['placeid'])
                     ->limit($data['textnum'])
-                    ->get()
-                    ->map(function ($article ) use ($data) {
-                        $catid = $article->catid;
-                        $pinyin = '';
-                        $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
-                        if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
-                            $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
-                            if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
-                                $pinyin = $childCategory->aLIas_pinyin ?  $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
-                            }
-                        } else {
-                            $pinyin = $category->aLIas_pinyin ?  $category->aLIas_pinyin : null;
-                        }
-                        $article->pinyin = $pinyin;
-                        return $article;
-                    });
-                // $result['text'] = $this->processArticles($text_article, $input); 
+                    ->get();
+                $result['text'] = $this->processArticles($result['text'], $web); 
             }
             
             if (empty($result) || count($result) == 0) {
@@ -528,27 +485,14 @@ class NewsService implements NewsServiceInterface
         // return Result::success($data);
         $where[] = ['status', '=', 1];
         if (isset($data['catid']) && !empty($data['catid'])) {
-            if (is_array($data['catid'])) {
-                $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
-                array_push($where, ['catid', 'in', $data['catid']]);
-            } else {
                 $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
                 array_push($where, ['catid', '=', $data['catid']]);
-            }
             if (empty($category)) {
                 return Result::error("此网站暂无此栏目", 0);
             }
         }
         // return Result::success($where);
-        $rep = Article::where(function ($query) use ($where) {
-            foreach ($where as $condition) {
-                if ($condition[1] === 'in') {
-                    $query->whereIn($condition[0], $condition[2]);
-                } else {
-                    $query->where($condition[0], $condition[1], $condition[2]);
-                }
-            }
-        })
+        $rep = Article::where($where)
         ->where(function ($query) use ($data) {
             $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
                   ->orWhereNull("ignore_ids");
@@ -563,32 +507,18 @@ class NewsService implements NewsServiceInterface
             'article.islink',
             'article.linkurl',
             'article.copyfrom',
+            'article.cat_arr_id',
             'article.catid')
         ->orderBy("updated_at", "desc")
         ->offset(($data['page'] - 1) * $data['pageSize'])
         ->limit($data['pageSize'])
-        ->get()
-        ->map(function ($article ) use ($data) {
-            $catid = $article->catid ?? 0;
-            $pinyin = '';
-            $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
-            $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
-            if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
-                $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
-                $pinyin = $childCategory->aLIas_pinyin ?  $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
-            }
-            $article->pinyin = $pinyin;
-            return $article;
-            });
-
-        $count = Article::where(function ($query) use ($where) {
-            foreach ($where as $condition) {
-                if ($condition[1] === 'in') {
-                    $query->whereIn($condition[0], $condition[2]);
-                } else {
-                    $query->where($condition[0], $condition[1], $condition[2]);
-                }
-            }
+        ->get();
+        $web['website_id'] = $data['website_id'];
+        $rep = $this->processArticles($rep,$web);
+        $count = Article::where($where)
+        ->where(function ($query) use ($data) {
+            $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
+                  ->orWhereNull("ignore_ids");
         })->count();
 
         $data = [
@@ -904,8 +834,6 @@ class NewsService implements NewsServiceInterface
      */
     public function selectWebsiteArticle(array $data): array
     {
-        $where = [];
-        // 初始化查询构造器
         $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
         $query = Article::where('status', 1)
             ->whereIn('catid', $category)
@@ -915,15 +843,12 @@ class NewsService implements NewsServiceInterface
                 })->orWhereNull("ignore_ids");
             });
         // return Result::success($all_articles);
-        // 检查是否存在 cityid 参数
         if (isset($data['cityid']) && !empty($data['cityid'])) {
             $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
         }
-        // 检查是否存在 department_id 参数
         if (isset($data['department_id']) && !empty($data['department_id'])) {
             $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
         }
-        // 检查是否存在 keyword 参数
         if (isset($data['keyword']) && !empty($data['keyword'])) {
             $query->where('title', 'like', '%' . $data['keyword'] . '%');
         }
@@ -941,33 +866,24 @@ class NewsService implements NewsServiceInterface
                 'article.islink',
                 'article.linkurl',
                 'article.copyfrom',
+                'article.cat_arr_id',
                 'article.catid',
                 'article.department_arr_id',
                 'article.city_arr_id',)
             ->orderBy("updated_at", "desc")
             ->offset(($data['page'] - 1) * $data['pageSize'])
             ->limit($data['pageSize'])
-            ->get()
-            ->map(function ($article ) use ($data) {
-                $catid = $article->catid ?? 0;
-                $pinyin = '';
-                $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
-                $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
-                if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
-                    $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
-                    $pinyin = $childCategory->aLIas_pinyin ?  $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
-                }
-                $article->pinyin = $pinyin;
-                return $article;
-                });
+            ->get();
+        $web['website_id'] = $data['website_id'];
+        $articles = $this->processArticles($articles,$web);
         if (empty($articles)) {
             return Result::error("没有符合条件的资讯数据");
         }
-        $data = [
+        $result = [
             'rows' => $articles,
             'count' => $count,
         ];
-        return Result::success($data);
+        return Result::success($result);
     }
 
     
@@ -1074,29 +990,52 @@ class NewsService implements NewsServiceInterface
             // 使用 array_map 处理每个元素
             $result = array_map(function ($item) use ($input) {
                 // 检查parent元素是否存在且不是undefined
-                if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
+                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'],
                     ];
                     // 查询栏目名称
-                    $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
-                
-                    if(empty($category)){
-                        $parent_alias = '';
-                        $parent_pinyin = null;
-                        $imgArticles = [];
+                    $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'])."') = 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.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);
+                        }
+                    }
+                    // 查询文字新闻
+                    if(isset($parentTextNum) && $parentTextNum!=0 && !empty($categroy)){  
                         $textArticles = [];
-                    }else{
-                        $parent_alias = $category->alias ?? '';
-                        $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
-                        // 查找子分类ID数组
-                        $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
-                        array_push($childCategoryIds,$parentCatId);
-                        $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
-                        if($parentImgNum!=0){
-                            // 查询图片新闻
-                            $imgArticles = Article::where('article.status', 1)
+                        $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')");
@@ -1107,148 +1046,98 @@ class NewsService implements NewsServiceInterface
                                         ->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")
+                                '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($parentImgNum)
-                            ->get()->all();          
-                            // 查询文字新闻
-                        }
-                        if($parentTextNum!=0){  
-                            $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'])."') = 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();
+                            ->limit($parentTextNum)
+                            ->get();
+                        if(!empty($textArticles)){
+                            $textArticles = $this->processArticles($textArticles , $website);
                         }
                     }
-                    
                 }
                 $resultItem = [
-                    'alias' => $parent_alias ?? '',
-                    'category_id' => $parentCatId ?? 0,
-                    'pinyin' => $parent_pinyin ?? null,
+                    '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'] != ""){
-                    $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();
+                    $count = explode(',', $item['child']);
+                    if(count($count)!=3){
+                        return Result::error("子级栏目:缺少必需参数错误",0);
+                    }
+                    list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
+                    $childCategory = 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']);
                     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();
-                        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)
+                        $childCategory = $this->processArticles($childCategory, $website);
+                        // 查询此层级的所有子栏目
+                        $all_childcat = WebsiteCategory::where($website)
+                        ->where("pid", $childCategory['pid'])
+                        // ->whereRaw("JSON_CONTAINS(category_arr_id, '".intval($childCatId)."') = 1")
+                        ->get(['alias', 'category_id','aLIas_pinyin','pid','category_arr_id as cat_arr_id']);
+                        $all_childcat = $this->processArticles($all_childcat, $website);
+                        // 查询子栏目图片新闻
+                        if( isset($childImgNum) && $childImgNum!=0 ){
+                            $childImgArticles = Article::where(function($query) use ($childCatId) {
+                                $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$childCatId\"')")
+                                        ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$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'])."') = 0");
                                     })->orWhereNull("ignore_ids");
                                 })
-                                ->select(
-                                    'article.id',
-                                    'article.title',
-                                    'article.imgurl',
-                                    'article.author',
-                                    'article.updated_at',
-                                    'article.introduce',
-                                    'article.islink',
-                                    'article.linkurl',
-                                    'article.copyfrom',
-                                    DB::raw("'$child_pinyin' as pinyin"))
+                                ->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()->all();
-                                // 查询子栏目文字新闻
-                            $childTextArticles = Article::where('catid', $childCatId)
+                                ->orderBy('updated_at', 'desc')
+                                ->limit($childImgNum)
+                                ->get();
+                            $childImgArticles = $this->processArticles($childImgArticles, $website);
+                        }
+                        // 查询子栏目文字新闻
+                        if( isset($childTextNum) && $childTextNum!=0 ){
+                            $childTextArticles = Article::where(function($query) use ($childCatId) {
+                                $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$childCatId\"')")
+                                        ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$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'])."') = 0");
                                     })->orWhereNull("ignore_ids");
                                 })
-                                ->select(
-                                    'article.id',
-                                    'article.title',
-                                    'article.author',
-                                    'article.updated_at',
-                                    'article.introduce',
-                                    'article.islink',
-                                    'article.linkurl',
-                                    'article.copyfrom',
-                                    DB::raw("'$child_pinyin' as pinyin"))
+                                ->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()->all();
-                            $resultItem['child'] = [
-                                'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
-                                'category_id' => $childCatId,
-                                'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
-                                'all_childcat' => $childCategory,
-                                'imgnum' => $childImgArticles,
-                                'textnum' => $childTextArticles,
-                            ];   
+                                ->get();
+                            $childTextArticles = $this->processArticles($childTextArticles, $website); 
                         }
                     }
+                    $resultItem['child'] = [
+                        'alias' => $childCategory ? $childCategory->alias : null,
+                        'category_id' => $childCategory ? $childCategory->category_id : null,
+                        'pinyin' => $childCategory ? $childCategory->pinyin : null,
+                        'all_childcat' => $all_childcat ?? [],
+                        'imgnum' => $childImgArticles ?? [],
+                        'textnum' => $childTextArticles ?? [],
+                    ];  
                 }else{
                     $resultItem['child'] = [];
                 }
@@ -1665,29 +1554,41 @@ class NewsService implements NewsServiceInterface
      * 封装处理文章的路由问题
      *  */
     function processArticles($articles, $data) {
-        if (!is_array($data)) {
-            // 可以根据实际情况进行日志记录或抛出异常
-            // 这里简单输出错误信息
-            echo "Error: \$data is not an array in processArticles. It is of type ". gettype($data). PHP_EOL;
-            return $articles;
-        }
-        return $articles->map(function ($article) use ($data) {
-            $catid = $article->cat_arr_id?? '';
-            $level = json_decode($catid, true);
-            $pinyin = '';
-            $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
-            if (!empty($category->pid) && $category->pid!= 0) {
+        // 检查 $articles 是否为数组或可迭代对象
+        if (is_array($articles) || is_iterable($articles) &&!empty($articles)) {
+            return $articles->map(function ($article) use ($data) {
+                $catid = $article->cat_arr_id?? '';
+                $level = json_decode($catid, true);
+                if (!is_array($level)) {
+                    return $article;
+                } else {
+                    $level = array_map('intval', $level);
+                }
                 $pinyin = WebsiteCategory::whereIn('category_id', $level)
+                    ->where($data)
                     ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
-                    ->get(['aLIas_pinyin'])
                     ->pluck('aLIas_pinyin')
                     ->implode('/');
+                $article->pinyin = $pinyin ?? '';
+                return $article;
+            });
+        } else {
+            // 若 $articles 不是数组或可迭代对象,直接处理单条数据
+            $catid = $articles->cat_arr_id?? '';
+            $level = json_decode($catid, true);
+            if (!is_array($level)) {
+                return $articles;
             } else {
-                $pinyin = $category->aLIas_pinyin ?? '';
+                $level = array_map('intval', $level);
             }
-            $article->pinyin = $pinyin;
-            return $article;
-        });
+            $pinyin = WebsiteCategory::whereIn('category_id', $level)
+                ->where($data)
+                ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
+                ->pluck('aLIas_pinyin')
+                ->implode('/');
+            $articles->pinyin = $pinyin ?? '';
+            return $articles;
+        }
     }
     // 封装处理由问题
     function processJob($job, $data) {