Просмотр исходного кода

修改接口:获取单页推荐列表、获取资讯推荐列表、获取单页详情

FengR 1 месяц назад
Родитель
Сommit
fdcc2cfddb
1 измененных файлов с 45 добавлено и 20 удалено
  1. 45 20
      app/JsonRpc/NewsService.php

+ 45 - 20
app/JsonRpc/NewsService.php

@@ -354,28 +354,41 @@ class NewsService implements NewsServiceInterface
   }
   public function getArticleCommend(array $data): array
   {
-    $where = [
-      'website_id' => $data['website_id'],
-    ];
-    $ids = WebsiteCategory::where($where)->pluck('category_id')->toArray();
-    var_dump($ids);
-    if (empty($ids)) {
-      return Result::error("没有推荐数据");
+    $user = User::where('id',$data['user_id'])->first();
+    if(empty($user)){
+      return Result::error('用户不存在!');
+    }
+    $ids = [];
+    if(empty($data['website_id']) && $user['type_id'] == 10000){
+      $ids = [];
+    }else{
+      $where = [
+        'website_id' => $data['website_id'] ?? $data['web_id'],
+      ];
+      $ids = WebsiteCategory::where($where)->pluck('category_id')->toArray();
+      // var_dump($ids);
+      if (empty($ids)) {
+        return Result::error("没有推荐数据");
+      }
     }
-    //id
     $whereArticle = [];
-    if (isset($data['id'])) {
+    if (isset($data['id']) && !empty($data['id'])) {
       $whereArticle['id'] = $data['id'];
     }
     //title  like
-    if (isset($data['title'])) {
+    if (isset($data['title']) && !empty($data['id'])) {
       array_push($whereArticle, ['title', 'like', "%{$data['title']}%"]);
     }
-    $article = Article::whereIn('catid', values: $ids)->where('status', 1)
-      ->where($whereArticle)
-      ->orderBy('id', 'desc')
-      ->paginate($data['pageSize'], ['*'], 'page', $data['page']);
-
+    // 若存在过滤用的分类 id 数组,则加入 whereIn 条件,否则直接跳过
+    $article = Article::when(!empty($ids) && is_array($ids), function ($query) use ($ids) {
+            $query->whereIn('catid', $ids);
+        })
+        ->where('status', 1)
+        ->when(!empty($whereArticle) , function ($query) use ($whereArticle){
+          $query->where($whereArticle);
+        })
+        ->orderBy('updated_at', 'desc')
+        ->paginate($data['pageSize'], ['*'], 'page', $data['page']);
     if ($article) {
       $data = [
         'rows' => $article->items(),
@@ -9810,15 +9823,27 @@ class NewsService implements NewsServiceInterface
     if (empty($user)) {
         return Result::error('用户不存在');
     }
-    $category = WebsiteCategory::where(['website_id' => $data['website_id']])->pluck('category_id');
-    if(empty($category)){
-      return Result::error('推荐单页不存在');
+  if(empty($data['website_id']) && $user['type_id'] == 10000){
+      $category = [];
+    }else{
+      $where = [
+        'website_id' => $data['website_id'] ?? $data['web_id'],
+      ];
+      $category = WebsiteCategory::where($where)->pluck('category_id')->toArray();
+      // var_dump($ids);
+      if (empty($ids)) {
+        return Result::error("推荐单页不存在");
+      }
     }
     $where = [];
     if(!empty($data['title'])){
+
       array_push($where, ['title', 'like', '%'.$data['title'].'%']);
     }
-    $result = SinglePage::whereIn('id', $category)
+    $result = SinglePage::
+    when(!empty($category) ,function ($query) use $category){
+      $query->whereIn('id', $category);
+    }
     ->when(!empty($where), function ($query) use ($where) {
         $query->where($where);
     })
@@ -10093,7 +10118,7 @@ class NewsService implements NewsServiceInterface
    */
   public function getSinglePageInfo(array $data): array
   {
-    $singlePage = SinglePage::where('id', $data['id'])->where('user_id', $data['user_id'])->first();
+    $singlePage = SinglePage::where('id', $data['id'])->first();
     if (empty($singlePage)) {
         return Result::error('单页不存在');
     }