Bladeren bron

模块新闻加强plus版

15313670163 2 weken geleden
bovenliggende
commit
cddfd0b383
1 gewijzigde bestanden met toevoegingen van 116 en 40 verwijderingen
  1. 116 40
      app/JsonRpc/NewsService.php

+ 116 - 40
app/JsonRpc/NewsService.php

@@ -914,6 +914,8 @@ class NewsService implements NewsServiceInterface
                 $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)
@@ -923,6 +925,16 @@ class NewsService implements NewsServiceInterface
                     })->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')
                 ->orderBy('updated_at', 'desc')
                 ->limit($parentImgNum)
                 ->get()->all();
@@ -935,27 +947,39 @@ class NewsService implements NewsServiceInterface
                             $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')
                     ->orderBy('updated_at', 'desc')
                     ->limit($parentTextNum)
                     ->get()->all();
             }
             $resultItem = [
-                'alias' => $category ? $category->alias : null,
+                'alias' => $parent_alias,
                 'category_id' => $parentCatId,
-                'pinyin' => $category->aLIas_pinyin ?? '',
+                'pinyin' => $parent_pinyin,
                 'imgnum' => $imgArticles,
                 'textnum' => $textArticles,
                 
             ];
 
             if (!empty($item['child']) && $item['child'] != "") {
-                // 查询第一个pid等于parent中第一个参数的category来获取child的category_id
-                $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)->select('category_id','alias','aLIas_pinyin')->get()->all();
+                $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) {
                     list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
                     // 查询子栏目名称
-                    $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
-                    // $childAllCartegory = WebsiteCategory::where('pid', $childCatId)->where($website)->first(['category_id']);
+                $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
+                    ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin, '/') as aLIas_pinyin", [$parent_pinyin])
+                    ->first();
                     if(empty($childCategoryInfo)){
                         $childImgArticles = [];
                         $childTextArticles = [];
@@ -968,6 +992,16 @@ class NewsService implements NewsServiceInterface
                                     $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')
                             ->where('imgurl', '!=', '')
                             ->orderBy('updated_at', 'desc')
                             ->limit($childImgNum)
@@ -980,6 +1014,15 @@ class NewsService implements NewsServiceInterface
                                     $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')
                             ->orderBy('updated_at', 'desc')
                             ->limit($childTextNum)
                             ->get()->all();
@@ -987,7 +1030,7 @@ class NewsService implements NewsServiceInterface
                     $resultItem['child'] = [
                         'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
                         'category_id' => $childCatId,
-                        'pinyin' => $childCategoryInfo->aLIas_pinyin?? '',
+                        'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
                         'all_childcat' => $childCategory,
                         'imgnum' => $childImgArticles,
                         'textnum' => $childTextArticles,
@@ -1011,58 +1054,91 @@ class NewsService implements NewsServiceInterface
      */
     public function getWebsiteArticles(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) { 
             list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']); 
             $website = [ 
-                'website_id' => $input['website_id'], 
+                'website_id' => $input['website_id']
             ]; 
             // 查询栏目名称 
             $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']); 
+            $pinyin = $category->aLIas_pinyin ? '/'.$category->aLIas_pinyin.'/' : null;
             if(empty($category)){ 
                 $imgArticles = []; 
                 $textArticles = []; 
                 // return Result::error("暂无此栏目",0); 
             }else{ 
+                // return Result::success($website);
                 // 查询图片新闻 
-                $imgArticles = Article::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', '!=', '') 
-                    ->whereRaw("JSON_CONTAINS(category_arr_id, '".intval($parentCatId)."')") 
-                    ->leftJoin('website_category', 'website_category.category_id', 'article.catid') 
-                    ->select('article.*','website_category.category_id','website_category.alias','website_category.aLIas_pinyin') 
-                    ->orderBy('updated_at', 'desc') 
-                    ->limit($parentImgNum) 
-                    ->get()->all(); 
-                // 查询文字新闻 
-                $textArticles = Article::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"); 
-                    }) 
-                    ->whereRaw("JSON_CONTAINS(category_arr_id, '".intval($parentCatId)."')") 
-                    ->leftJoin('website_category', 'website_category.category_id', 'article.catid') 
-                    ->select('article.*','website_category.category_id','website_category.alias','website_category.aLIas_pinyin') 
-                    ->orderBy('updated_at', 'desc') 
-                    ->limit($parentTextNum) 
-                    ->get()->all(); 
-            } 
+                // 合并查询条件
+                $baseQuery = Article::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");
+                    })
+                    ->whereRaw("JSON_CONTAINS(category_arr_id, '" . intval($parentCatId) . "')")
+                    ->leftJoin('website_category', 'website_category.category_id', 'article.catid')
+                    ->where('website_category.website_id', $website['website_id'])
+                    ->where('article.status', 1);
+                    
+                 // 查询文字新闻
+                 $textArticles = clone $baseQuery;
+                 $textArticles = $textArticles
+                    ->select(
+                        'article.id',
+                        'article.title',
+                        // 'article.imgurl',
+                        'article.author',
+                        'article.updated_at',
+                        'article.introduce',
+                        'article.islink',
+                        'article.linkurl',
+                        'article.copyfrom',
+                        'website_category.category_id',
+                        'website_category.alias',
+                        'website_category.aLIas_pinyin'
+                    )
+                    ->selectRaw("CONCAT(?, aLIas_pinyin, '/') as aLIas_pinyin", [$pinyin])
+                    ->orderBy('article.updated_at', 'desc')
+                    ->limit($parentTextNum)
+                    ->get()
+                    ->all();
+                // 查询图片新闻
+                $imgArticles = clone $baseQuery;
+                $imgArticles = $imgArticles
+                    ->select(
+                        'article.id',
+                        'article.title',
+                        'article.imgurl',
+                        'article.author',
+                        'article.updated_at',
+                        'article.introduce',
+                        'article.islink',
+                        'article.linkurl',
+                        'article.copyfrom',
+                        'website_category.category_id',
+                        'website_category.alias',
+                        'website_category.aLIas_pinyin'
+                    )
+                    ->selectRaw("CONCAT(?, aLIas_pinyin, '/') as aLIas_pinyin", [$pinyin])
+                    ->orderBy('article.updated_at', 'desc')
+                    ->where('imgurl', '!=', '')
+                    ->limit($parentImgNum)
+                    ->get()
+                    ->all();
+
+               
+            }
+           
             $resultItem = [ 
                 'alias' => $category ? $category->alias : null, 
                 'category_id' => $parentCatId, 
-                'pinyin' => $category->aLIas_pinyin ?? '', 
-                'imgnum' => $imgArticles, 
-                'textnum' => $textArticles, 
+                'pinyin' => $pinyin ? $pinyin : null, 
+                'imgnum' => $imgArticles ?? [], 
+                'textnum' => $textArticles ?? [], 
                 
             ];