Explorar o código

c端-获取首页所有资讯的接口进行升级并且更新最新版获取对应路由的接口

15313670163 hai 1 semana
pai
achega
c730154e33
Modificáronse 1 ficheiros con 232 adicións e 168 borrados
  1. 232 168
      app/JsonRpc/NewsService.php

+ 232 - 168
app/JsonRpc/NewsService.php

@@ -988,177 +988,193 @@ class NewsService implements NewsServiceInterface
   */
   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);
-            }
-          }
-          // 查询文字新闻
-          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 ?? [],
-          ]; 
+    $time1 = microtime(true);
+    $data = json_decode($data['id'], true);
+    $wetbsite_id = $data['website_id'] ?? 2;
+    $categorys = $this->processArticlePro($wetbsite_id);
+    $cat_1st_arr = $categorys['cat_1st_arr'];
+    $catiall = $categorys['catiall'];
+    $websiteInfoIndexed = $categorys['websiteInfoIndexed'];
+    $parent_category = array_column($data, 'parent');
+    $parent_cat = [];        
+    $child_category = array_column($data,'child');
+    if(!empty($parent_category)){
+        foreach($parent_category as $key => $value){
+            $arr = array_map('intval', explode(',', $value));
+            $parent_cat[$key] = $arr[0] ?? 0;
+            $parent_category = $arr[0] ?? 0;
+            $article_imgnum = $arr[1] ?? 0;
+            $article_textnum = $arr[2] ?? 0;
+            if($parent_category != 0){
+                $category_arr = array_merge([$parent_category], $cat_1st_arr[$parent_category]);
+                if($article_imgnum != 0){
+                    $img_article = Article::whereIn('catid', $category_arr)
+                        ->where('status',1)
+                        ->where('imgurl','!=', '')
+                        ->leftJoinSub(function ($query) use ($wetbsite_id) {
+                            $query->from('article_ignore')
+                                ->where('website_id', $wetbsite_id);
+                        }, 'article_ignore', function ($join) {
+                            $join->on('article_ignore.article_id', '=', 'article.id');
+                        })
+                        ->where(function ($query) use ($wetbsite_id) {
+                            $query->whereNull('article_ignore.article_id')
+                                ->orWhere('article_ignore.website_id', '!=', $wetbsite_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')
+                        ->orderBy('updated_at', 'desc')
+                        ->limit($article_imgnum)
+                        ->get()->all();
+                        foreach ($img_article as $k => $v) {
+                            $img_article[$k]->category_name = $catiall[$v->catid]['alias'];
+                            $img_article[$k]->pinyin = $catiall[$v->catid]['pinyin'];
+                            
+                        }
+                }else{
+                    $img_article = [];
+                }
+                if($article_textnum != 0){
+                    $text_article = Article::whereIn('catid', $category_arr)
+                        ->where('status',1)
+                        ->leftJoinSub(function ($query) use ($wetbsite_id) {
+                            $query->from('article_ignore')
+                                ->where('website_id', $wetbsite_id);
+                        }, 'article_ignore', function ($join) {
+                            $join->on('article_ignore.article_id', '=', 'article.id');
+                        })
+                        ->where(function ($query) use ($wetbsite_id) {
+                            $query->whereNull('article_ignore.article_id')
+                                ->orWhere('article_ignore.website_id', '!=', $wetbsite_id);
+                        })
+                        ->select('id','title','author','updated_at','introduce','islink','linkurl','copyfrom','cat_arr_id','catid')
+                        ->orderBy('updated_at', 'desc')
+                        ->limit($article_textnum)
+                        ->get()->all();
+                    foreach ($text_article as $k => $v) {
+                        $text_article[$k]->category_name = $catiall[$v->catid]['alias'];
+                        $text_article[$k]->pinyin = $catiall[$v->catid]['pinyin'];
+                        // $text_article[$k]->alias = $catiall[$v->catid]['alias'];
+                    }
+                }else{
+                    $text_article = [];
+                }
+                $catiall[$parent_category]['imgnum'] = $img_article;
+                $catiall[$parent_category]['textnum'] = $text_article;
+                $parent[$key] = $catiall[$parent_category];              
+            }else{
+                $parent[$key] = '';
+            }
+        }
+    }
+    if(!empty($child_category)){
+        foreach($child_category as $key => $value){
+            $arr = array_map('intval', explode(',', $value));
+            $parent_category = $parent_cat[$key];
+            $child_category = $arr[0] ?? 0;
+            $article_imgnum = $arr[1] ?? 0;
+            $article_textnum = $arr[2] ?? 0;
+           
+            if($child_category != 0){
+                $all_childcat = $cat_1st_arr[$parent_category];
+                $processedChildCat = array_map(function ($v) use ($websiteInfoIndexed) {
+                    // 从 $websiteInfoIndexed 中获取对应的数据
+                    $info = $websiteInfoIndexed[$v];
+                    // 返回一个包含所需信息的数组
+                    return [
+                        'alias' => $info->alias,
+                        'category_id' => $info->category_id,
+                        'aLIas_pinyin' => $info->aLIas_pinyin,
+                        'pid' => $info->pid,
+                        'type' => $info->type,
+                        'cat_arr_id' => $info->category_arr_id ?? ['出错'],
+                        'pinyin' => $info->pinyin,
+                    ];
+                }, $all_childcat);
+             if($article_imgnum != 0){
+                $img_article = Article::where('catid', $child_category)
+                    ->where('status',1)
+                    ->where('imgurl','!=', '')
+                    ->leftJoinSub(function ($query) use ($wetbsite_id) {
+                        $query->from('article_ignore')
+                            ->where('website_id', $wetbsite_id);
+                    }, 'article_ignore', function ($join) {
+                        $join->on('article_ignore.article_id', '=', 'article.id');
+                    })
+                    ->where(function ($query) use ($wetbsite_id) {
+                        $query->whereNull('article_ignore.article_id')
+                            ->orWhere('article_ignore.website_id', '!=', $wetbsite_id);
+                    })
+                    ->select('id','title','imgurl','author','updated_at','introduce','islink','linkurl','copyfrom','cat_arr_id','catid')
+                    ->orderBy('updated_at', 'desc')
+                    ->limit($article_imgnum)
+                    ->get()->all();
+                    foreach ($img_article as $k => $v) {
+                        // var_dump($v);
+                        // var_dump($k);
+                        $img_article[$k]->category_name = $catiall[$v->catid]['alias'];
+                        $img_article[$k]->alias = $catiall[$v->catid]['alias'];
+                    }
+            }else{
+                $img_article = [];
+            }
+            if($article_textnum != 0){
+                $text_article = Article::where('catid', $child_category)
+                    ->where('status',1)
+                    ->leftJoinSub(function ($query) use ($wetbsite_id) {
+                        $query->from('article_ignore')
+                            ->where('website_id', $wetbsite_id);
+                    }, 'article_ignore', function ($join) {
+                        $join->on('article_ignore.article_id', '=', 'article.id');
+                    })
+                    ->where(function ($query) use ($wetbsite_id) {
+                        $query->whereNull('article_ignore.article_id')
+                            ->orWhere('article_ignore.website_id', '!=', $wetbsite_id);
+                    })
+                    ->select('id','title','author','updated_at','introduce','islink','linkurl','copyfrom','cat_arr_id','catid')
+                    ->orderBy('updated_at', 'desc')
+                    ->limit($article_textnum)
+                    ->get()->all();
+                foreach ($text_article as $k => $v) {
+                    // var_dump($v);
+                    // var_dump($k);
+                    $text_article[$k]->category_name = $catiall[$v->catid]['alias'];
+                    // $text_article[$k]->pinyin = $catiall[$v->catid]['pinyin'];
+                    $text_article[$k]->alias = $catiall[$v->catid]['alias'];
+                }
+            }else{
+                $text_article = [];
+            }
+            $catiall[$child_category]['all_childcat'] = $processedChildCat;
+            $catiall[$child_category]['imgnum'] = $img_article;
+            $catiall[$child_category]['textnum'] = $text_article;
+            $child[$key] = $catiall[$child_category];               
+                // var_dump($pids);
+            }else{
+                $child[$key] = '';
+            }
+        }
+    }
+    foreach($parent as $key => $value){
+        $result[$key] = $value;
+        if(!empty($child[$key])){
+            // var_dump($child[$key]);
+           
+            $result[$key]['child'] = $child[$key];
+            // $value[$key]['child'] = $child[$key];
         }else{
-          $resultItem['child'] = [];
+            $result[$key]['child'] = [
+                "alias" => null,
+                "category_id" => null,
+                "type" => null,
+                "pinyin" => null,
+                "all_childcat" => [],
+                "imgnum" => [],
+                "textnum" => []
+            ];
         }
-      return $resultItem;
-    }, $data);
+    }
     return Result::success($result);
-    // return Result::success($data);
+
 }
 
 /**
@@ -1634,6 +1650,54 @@ private function fetchArticles($catId, $website, $limit, $isImageArticle = false
         $goods->imgurl = json_decode($goods->imgurl, true);
         return Result::success($goods);
     }
+    /**
+     * plus  pro  处理文章路由问题
+     */
+    function processArticlePro($website_id){
+        $categorys = WebsiteCategory::where('website_id', $website_id)->get()->all();
+        $websiteInfoIndexed = array_column($categorys, null, 'category_id');
+        // 取出category_id 对应的aLIas_pinyin 
+        $categoryIds = array_column($categorys, 'category_id');
+        $categoryPIds = array_column($categorys, 'pid');
+        $aliasPinyins = array_column($categorys, 'aLIas_pinyin');
+        $cat_arr = array_combine($categoryIds, $aliasPinyins);
+        // return Result::success($categorys);
+        $catiall = [];
+        //一级所有子级的记录
+        $cat_1st_arr = [];
+        foreach ($categorys as $key => $value) {
+            if($value->pid != 0 && !in_array($value->category_id,$categoryPIds)){
+                $cat_1st_arr[$value->pid][] = $value->category_id;
+            }
+            // unset($cat_1st_arr[0]);
+            //     //算出路由拼音
+            $category_arr_id = json_decode($value->category_arr_id);
+            $pinyin_str = '';
+            // 算出一级  并且算出子级
+            if ($value->pid == 0) {
+                $cat_1st_arr[$value->pid][] = $value->category_id;
+            } else {
+                $cat_1st_arr[$value->category_id][] = [];
+                // $cat_1st_arr[$value->pid][] = $value->category_id;
+            }
+            foreach ($category_arr_id as $k => $v) {
+                $pinyin_str .= $cat_arr[$v] . '/';
+            }
+            $pinyin_str = rtrim($pinyin_str, '/');
+            $catiall[$value->category_id]['alias'] = $value->alias;
+            $catiall[$value->category_id]['category_id'] = $value->category_id;
+            $catiall[$value->category_id]['type'] = $value->type;
+            $catiall[$value->category_id]['pinyin'] = $pinyin_str; 
+            // $cat_id = $value->category_id;
+            $websiteInfoIndexed[$value->category_id]->pinyin = $pinyin_str;
+        }
+        return [
+            'cat_1st_arr' => $cat_1st_arr,
+            'catiall' => $catiall,
+            'websiteInfoIndexed' => $websiteInfoIndexed,
+        ];
+
+    }
     /**
      * 封装处理文章的路由问题
      *  */