瀏覽代碼

修改获取头条新闻

15313670163 5 天之前
父節點
當前提交
d652f86f56
共有 1 個文件被更改,包括 140 次插入73 次删除
  1. 140 73
      app/JsonRpc/NewsService.php

+ 140 - 73
app/JsonRpc/NewsService.php

@@ -294,7 +294,7 @@ 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()));
-        $result = [];
+       
         if ($category) {
             $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
             $where = [
@@ -313,9 +313,18 @@ class NewsService implements NewsServiceInterface
                         $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
                     }
                     // 原始查询
-                    $result = Article::where($whereL7)
-                        ->offset($placeid)
-                        ->limit($data['pageSize'])
+                    $result['level7'] = Article::where($whereL7)
+                        ->when(!empty($data['imgnum']), function ($query) use ($data) {
+                            $query->where('article.imgurl', '!=', '')
+                                ->select('article.*')
+                                ->offset($data['placeid'])
+                                ->limit($data['imgnum']);
+                        })
+                        ->when(!empty($data['textnum']), function ($query) use ($data) {
+                            $query->select('article.*')
+                                ->offset($data['placeid'])
+                                ->limit($data['textnum']);
+                        })
                         ->orderBy('updated_at', 'desc')
                         ->get()
                         ->map(function ($article ) use ($data) {
@@ -344,48 +353,104 @@ class NewsService implements NewsServiceInterface
             //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
             // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
             var_dump($where, 'where-----------------');
-            $result = Article::where($where)
-                ->whereIn("catid", $category)
-                // ->leftJoin('website_category', 'article.catid', 'website_category.category_id')
-                // ->where('website_category.website_id', $data['website_id'])
-                ->where(function ($query) use ($data) {
-                    $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
-                        ->orWhereNull("ignore_ids");
-                })
-            //$data['level'] == 4 || $data['level'] == 5 查询随机
-                ->when($data['level'] == 5, function ($query) {
-                    $query->inRandomOrder()
-                    //updated_at最近三十天;
-                        ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
-                })
-                ->when($data['level'] == 4, function ($query) {
-                    $query->orderBy("article.updated_at", "desc");
-                })
-                ->when(!empty($data['level']), function ($query) use ($data) {
-                    if ($data['level'] != 4 && $data['level'] != 5) {
-                        $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
-                            ->orderBy("article.updated_at", "desc");
-                    }
-                })
-                ->select('article.*')
-                ->offset($placeid)
-                ->limit($data['pageSize'])
-                ->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;
+           // 初始化基础查询
+            $query = Article::where($where)
+              ->whereIn("catid", $category)
+              ->where(function ($query) use ($data) {
+                $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
+                  ->orWhereNull("ignore_ids");
+              });
+
+            // 处理 level 相关查询条件
+            $query->when($data['level'] == 5, function ($query) {
+              $query->inRandomOrder()
+                ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
+            })->when($data['level'] == 4, function ($query) {
+              $query->orderBy("article.updated_at", "desc");
+            })->when(!empty($data['level']), function ($query) use ($data) {
+              if ($data['level'] != 4 && $data['level'] != 5) {
+                $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
+                  ->orderBy("article.updated_at", "desc");
+              }
+            });
+               // 初始化结果数组
+            $result = [];
+            // $input = $data['website_id']; 
+            // 处理图片新闻查询
+            if (!empty($data['imgnum'])) {
+                // 原错误提示表明 cleanBindings 方法需要传入数组类型的参数,而实际传入了 null。
+                // 这里克隆查询构建器,确保绑定参数为数组,避免该错误。
+                $imgQuery = clone $query;
+                if ($imgQuery->getBindings() === null) {
+                    $imgQuery->setBindings([]);
+                }   
+                $result['img'] = $imgQuery->where('article.imgurl', '!=', '')
+                    ->select('article.id',
+                    'article.title',
+                    'article.imgurl',
+                    'article.author',
+                    'article.updated_at',
+                    'article.introduce',
+                    'article.islink',
+                    'article.linkurl',
+                    'article.copyfrom',
+                    '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;
                         }
-                    } else {
-                        $pinyin = $category->aLIas_pinyin ?  $category->aLIas_pinyin : null;
-                    }
-                    $article->pinyin = $pinyin;
-                    return $article;
-                });
+                        $article->pinyin = $pinyin;
+                        return $article;
+                    });
+                // $result['img'] = $this->processArticles($img_article, $input); 
+            }
+            
+            // 处理文字新闻查询
+            if (!empty($data['textnum'])) {
+                $textQuery = clone $query;
+                $result['text'] = $textQuery
+                ->select('article.id',
+                    'article.title',
+                    'article.imgurl',
+                    'article.author',
+                    'article.updated_at',
+                    'article.introduce',
+                    'article.islink',
+                    'article.linkurl',
+                    'article.copyfrom',
+                    '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); 
+            }
+            
             if (empty($result) || count($result) == 0) {
                 return Result::error("暂无头条新闻");
             }
@@ -1345,35 +1410,9 @@ class NewsService implements NewsServiceInterface
         return Result::success($result); 
 
     }
-     /**
-     *获取头条类新闻模块-合集(暂时用不到)
-     * @param array $data
-     * @return array
-     */
-    public function getWebsiteAllArticlett(array $data): array
-    {
-        $input['id'] = $data['id'];
-        $input['website_id'] = $data['website_id'];
-        $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('website_id', $input['website_id'])->pluck('category_id');
-            $category = array_values(array_unique($category->toArray()));
-            $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
-            $where = [
-                'status' => 1,
-            ];
 
-            return $category;
-        }, $data); // 添加第二个参数 $data,确保 array_map 函数有两个参数
-           
-        return Result::success($result);
-    }
-    // 封装处理数据的函数
+
+    // 封装处理商品的路由问题
     function processGoods($goods, $data) {
         return $goods->map(function ($good) use ($data) {
             $catid = $good->catid ?? 0;
@@ -1596,4 +1635,32 @@ class NewsService implements NewsServiceInterface
         $goods->imgurl = json_decode($goods->imgurl, true);
         return Result::success($goods);
     }
+    /**
+     * 封装处理文章的路由问题
+     *  */
+    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) {
+                $pinyin = WebsiteCategory::whereIn('category_id', $level)
+                    ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
+                    ->get(['aLIas_pinyin'])
+                    ->pluck('aLIas_pinyin')
+                    ->implode('/');
+            } else {
+                $pinyin = $category->aLIas_pinyin ?? '';
+            }
+            $article->pinyin = $pinyin;
+            return $article;
+        });
+    }
 }