wzq 5 年 前
コミット
1939044873
共有3 個のファイルを変更した20 個の追加2 個の削除を含む
  1. 10 1
      app/Helper/helper.php
  2. 2 1
      app/Http/Controllers/V1/PostController.php
  3. 8 0
      app/Repositories/PostRepositories.php

+ 10 - 1
app/Helper/helper.php

@@ -77,7 +77,7 @@ function http($url, $param, $method = 'post')
 
 }
 
- function jsonSuccess($data = [], $msg = "成功")
+ function jsonSuccess($data = [], $msg = "成功", $extra = [])
 {
     $response = array(
         'code' => 0,
@@ -95,13 +95,22 @@ function http($url, $param, $method = 'post')
                         'pagination' => $data['meta']['pagination']
                     )
                 );
+                if($extra){
+                    $temp['data'] = array_merge($temp['data'], $extra);
+                }
                 $response = array_merge($response, $temp);
             } elseif(isset($data['data'])) {
+                if($extra){
+                    $data['data'] = array_merge($data['data'], $extra);
+                }
                 $response = array_merge($response, $data);
             }else{
                 $temp = array(
                     'data' => $data
                 );
+                if($extra){
+                    $temp['data'] = array_merge($temp['data'], $extra);
+                }
                 $response = array_merge($response, $temp);
             }
         } else {

+ 2 - 1
app/Http/Controllers/V1/PostController.php

@@ -282,7 +282,8 @@ class PostController extends Controller
         $resource->setPaginator(new IlluminatePaginatorAdapter($list));
         $data = $fractal->createData($resource)->toArray();
 
-        return jsonSuccess($data);
+        $commentCount = $this->postRepositories->getCommentCount($request['post_id']);
+        return jsonSuccess($data, '成功', ['comment_count' => $commentCount]);
     }
 
     /**

+ 8 - 0
app/Repositories/PostRepositories.php

@@ -669,4 +669,12 @@ class PostRepositories
         }
     }
 
+    /**
+     * 内容评论数
+     */
+    public function getCommentCount($id)
+    {
+        return $this->postComment->where('post_id', $id)->count();
+    }
+
 }