Bladeren bron

评论列表

wzq 5 jaren geleden
bovenliggende
commit
8bce2c0e21

+ 21 - 0
app/Http/Controllers/V1/PostController.php

@@ -10,6 +10,7 @@ namespace App\Http\Controllers\V1;
 
 use App\Repositories\PostRepositories;
 use App\Traits\UserTrait;
+use App\Transformers\Post\CommentTransformer;
 use App\Transformers\Post\SuggestTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
@@ -57,4 +58,24 @@ class PostController extends Controller
         }
         return jsonSuccess($data);
     }
+
+    /**
+     * 评论列表
+     */
+    public function commentList(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'post_id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+        $purchaseList = $this->postRepositories->commentList($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($purchaseList, new CommentTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($purchaseList));
+        $data = $fractal->createData($resource)->toArray();
+
+        return jsonSuccess($data);
+    }
 }

+ 18 - 1
app/Repositories/PostRepositories.php

@@ -12,6 +12,7 @@ namespace App\Repositories;
 use App\Models\Behavior;
 use App\Models\Post;
 use App\Models\PostCollect;
+use App\Models\PostComment;
 use App\Models\PostData;
 use App\Models\PostLike;
 use Illuminate\Support\Facades\Log;
@@ -19,9 +20,10 @@ use Illuminate\Support\Facades\Redis;
 
 class PostRepositories
 {
-    public function __construct(Post $post)
+    public function __construct(Post $post,PostComment $postComment)
     {
         $this->post = $post;
+        $this->postComment = $postComment;
     }
 
     /**
@@ -86,6 +88,21 @@ class PostRepositories
             ->paginate($perPage);
     }
 
+
+    /**
+     * 评论列表
+     */
+    public function commentList($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+
+        return $this->postComment
+            ->where('post_id', $request['post_id'])
+            ->where('parent_id', 0)
+            ->orderBy('id','desc')
+            ->paginate($perPage);
+    }
+
     /**
      * 更新帖子统计数量
      * @param $request

+ 41 - 0
app/Transformers/Post/CommentTransformer.php

@@ -0,0 +1,41 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/15
+ * Time: 16:40
+ */
+namespace  App\Transformers\Post;
+
+use App\Models\PostComment;
+use Carbon\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class CommentTransformer extends TransformerAbstract
+{
+    
+    public function transform(PostComment $postComment)
+    {
+        $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
+        $reply = [];
+        foreach($replies as $val){
+            $reply[] = [
+                'uid' => $val->uid,
+                'nickname' => $val->nickname,
+                'avatar' => $val->avatar,
+                'reply_username' => $val->reply_username,
+                'content' => $val->content,
+            ];
+        }
+        return [
+            'id' => $postComment['id'],
+            'uid' => $postComment['uid'],
+            'username' => $postComment['username'],
+            'avatar' => $postComment['avatar'],
+            'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
+            'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
+            'reply_count' => $postComment->reply->count(),
+            'reply' => $reply,
+        ];
+    }
+}

+ 1 - 0
app/Transformers/Post/SuggestTransformer.php

@@ -37,6 +37,7 @@ class SuggestTransformer extends TransformerAbstract
             foreach($replies as $val){
                 $reply[] = [
                     'nickname' => $val->nickname,
+                    'reply_username' => $val->reply_username,
                     'content' => $val->content,
                 ];
             }

+ 1 - 1
resources/lang/zh-CN/validation.php

@@ -100,7 +100,7 @@ return [
     */
 
     'attributes' => [
-        'id' => 'id',
+        'post_id' => '内容id',
     ],
     'mobile'    => '手机号码格式不正确。',
 ];

+ 1 - 1
routes/api.php

@@ -20,8 +20,8 @@ $api->version('v1', [
     $api->get('getBehaviorByIdentify', 'BehaviorController@getBehaviorByIdentify');
     //登录+验签
     $api->group(['middleware' => ['chxq_jwt_auth']], function ($api) {
-        //$api->get('test', 'TestController@index');
         $api->get('post/suggest', 'PostController@suggestPost');
+        $api->get('post/comment', 'PostController@commentList');
         $api->get('topicCategory', 'CategoryController@index');
     });