wzq 5 years ago
parent
commit
dc81a79cda

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

@@ -310,9 +310,15 @@ class PostController extends Controller
         if (!$exists) {
             return jsonError('内容飞走了');
         }
+        $userInfo = $this->getUserInfo();
+        if ($userInfo) {
+            $uid = $userInfo['uid'];
+        }else{
+            $uid = 0;
+        }
         $list = $this->postRepositories->commentList($request->all());
         $fractal = new Manager();
-        $resource = new Collection($list, new CommentTransformer());
+        $resource = new Collection($list, new CommentTransformer($request['post_id'], $uid));
         $resource->setPaginator(new IlluminatePaginatorAdapter($list));
         $data = $fractal->createData($resource)->toArray();
 
@@ -332,9 +338,16 @@ class PostController extends Controller
             return jsonError($validator->errors()->first());
         }
 
+        $userInfo = $this->getUserInfo();
+        if ($userInfo) {
+            $uid = $userInfo['uid'];
+        }else{
+            $uid = 0;
+        }
+        $postId = $this->postRepositories->getPostId($request['id']);
         $list = $this->postRepositories->replyList($request->all());
         $fractal = new Manager();
-        $resource = new Collection($list, new ReplyTransformer());
+        $resource = new Collection($list, new ReplyTransformer($postId, $uid));
         $resource->setPaginator(new IlluminatePaginatorAdapter($list));
         $data = $fractal->createData($resource)->toArray();
 

+ 11 - 0
app/Repositories/PostRepositories.php

@@ -564,6 +564,7 @@ class PostRepositories
         return $this->postComment
             ->where('post_id', $request['post_id'])
             ->where('parent_id', 0)
+            ->orderBy('like_count', 'desc')
             ->orderBy('id', 'desc')
             ->paginate($perPage);
     }
@@ -581,6 +582,16 @@ class PostRepositories
             ->paginate($perPage);
     }
 
+    /**
+     * 根据评论id获取内容id
+     */
+    public function getPostId($commentId)
+    {
+        return $this->postComment
+            ->where('id', $commentId)
+            ->value('post_id');
+    }
+
     /**
      * 话题列表
      */

+ 15 - 0
app/Traits/PostTrait.php

@@ -120,4 +120,19 @@ trait PostTrait
         }
         return $comment;
     }
+
+    //获取评论点赞数、点赞状态
+    public function getCommentLike($postId, $commentId, $uid = 0)
+    {
+        $key = 'comment_like_'.$postId;
+        $isLike = 0;
+        if($uid){
+            $isLike = Redis::ZSCORE($key, $uid.'_'.$commentId) ? 1 : 0;
+        }
+        $likeCount = Redis::ZCOUNT($key, $commentId, $commentId);
+        return [
+            'is_like' => $isLike,
+            'like_count' => $likeCount
+        ];
+    }
 }

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

@@ -8,6 +8,7 @@
 namespace  App\Transformers\Post;
 
 use App\Models\PostComment;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\Log;
@@ -17,6 +18,13 @@ use League\Fractal\TransformerAbstract;
 class CommentTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
+    public function __construct($postId, $uid)
+    {
+        $this->postId = $postId;
+        $this->uid = $uid;
+    }
+
     public function transform(PostComment $postComment)
     {
         $reply = [];
@@ -26,11 +34,15 @@ class CommentTransformer extends TransformerAbstract
             $reply = json_decode($replyData);
             foreach($reply as &$item){
                 $item->created_at = Carbon::parse($item->created_at)->diffForHumans();
+                $replyLike = $this->getCommentLike($this->postId, $item->id, $this->uid);
+                $item->is_like = $replyLike['is_like'];
+                $item->like_count = $replyLike['like_count'];
             }
         }else{
             $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
             $redisReply = [];
             foreach($replies as $val){
+                $replyLike = $this->getCommentLike($this->postId, $val->id, $this->uid);
                 $userComment = $this->userInfo($val->uid);
                 $replyUsername = '';
                 if($val->reply_uid){
@@ -45,6 +57,8 @@ class CommentTransformer extends TransformerAbstract
                     'content' => $val->is_delete?'该评论已被删除':$val->content,
                     'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
                     'is_delete' => $val->is_delete,
+                    'is_like' => $replyLike['is_like'],
+                    'like_count' => $replyLike['like_count'],
                 ];
                 $redisReply[] = [
                     'uid' => $val->uid,
@@ -54,12 +68,15 @@ class CommentTransformer extends TransformerAbstract
                     'content' => $val->is_delete?'该评论已被删除':$val->content,
                     'created_at' => $val->created_at,
                     'is_delete' => $val->is_delete,
+                    'is_like' => $replyLike['is_like'],
+                    'like_count' => $replyLike['like_count'],
                 ];
             }
             Redis::SET($replyKey, json_encode($redisReply));
             Redis::EXPIRE($replyKey, 604800);
         }
         $user = $this->userInfo($postComment['uid']);
+        $commentLike = $this->getCommentLike($this->postId, $postComment['id'], $this->uid);
         return [
             'id' => $postComment['id'],
             'uid' => $postComment['uid'],
@@ -71,6 +88,8 @@ class CommentTransformer extends TransformerAbstract
             'reply' => $reply,
             'replys' => $reply,
             'is_delete' => $postComment['is_delete'],
+            'is_like' => $commentLike['is_like'],
+            'like_count' => $commentLike['like_count'],
         ];
     }
 }

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

@@ -40,6 +40,7 @@ class ListTransformer extends TransformerAbstract
             'title' => $post['title'],
             'content' => subtext($post['content'], 100),
             'img' => $post['img'],
+            'topic' => $this->getTopic($post['topic_ids']),
             'praise_count' => $postInfo['praise_count'],
             'is_like' => $isLike,
             'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",

+ 11 - 0
app/Transformers/Post/ReplyTransformer.php

@@ -8,6 +8,7 @@
 namespace  App\Transformers\Post;
 
 use App\Models\PostComment;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
 use League\Fractal\TransformerAbstract;
@@ -15,6 +16,13 @@ use League\Fractal\TransformerAbstract;
 class ReplyTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
+    public function __construct($postId, $uid)
+    {
+        $this->postId = $postId;
+        $this->uid = $uid;
+    }
+
     public function transform(PostComment $postComment)
     {
         $user = $this->userInfo($postComment['uid']);
@@ -23,6 +31,7 @@ class ReplyTransformer extends TransformerAbstract
             $userReply = $this->userInfo($postComment['reply_uid']);
             $replyUsername = $userReply['username'];
         }
+        $commentLike = $this->getCommentLike($this->postId, $postComment['id'], $this->uid);
         return [
             'id' => $postComment['id'],
             'uid' => $postComment['uid'],
@@ -32,6 +41,8 @@ class ReplyTransformer extends TransformerAbstract
             'content' => $postComment['is_delete']?'该回复已被删除':$postComment['content'],
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
             'is_delete' => $postComment['is_delete'],
+            'is_like' => $commentLike['is_like'],
+            'like_count' => $commentLike['like_count'],
         ];
     }
 }