Browse Source

评论列表

wzq 5 years ago
parent
commit
4ce0c8de61
1 changed files with 26 additions and 17 deletions
  1. 26 17
      app/Transformers/Post/CommentTransformer.php

+ 26 - 17
app/Transformers/Post/CommentTransformer.php

@@ -10,6 +10,7 @@ namespace  App\Transformers\Post;
 use App\Models\PostComment;
 use App\Models\PostComment;
 use App\Traits\UserTrait;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
 use Carbon\Carbon;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 use League\Fractal\TransformerAbstract;
 
 
 class CommentTransformer extends TransformerAbstract
 class CommentTransformer extends TransformerAbstract
@@ -17,24 +18,32 @@ class CommentTransformer extends TransformerAbstract
     use UserTrait;
     use UserTrait;
     public function transform(PostComment $postComment)
     public function transform(PostComment $postComment)
     {
     {
-        $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
         $reply = [];
         $reply = [];
-        foreach($replies as $val){
-            $userComment = $this->userInfo($val->uid);
-            $replyUsername = '';
-            if($val->reply_uid){
-                $userReply = $this->userInfo($val->reply_uid);
-                $replyUsername = $userReply['username'];
+        $replyKey = 'post_new_reply_'.$postComment['id'];
+        $replyData = Redis::GET($replyKey);
+        if($replyData){
+            $reply = json_decode($replyData);
+        }else{
+            $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
+            foreach($replies as $val){
+                $userComment = $this->userInfo($val->uid);
+                $replyUsername = '';
+                if($val->reply_uid){
+                    $userReply = $this->userInfo($val->reply_uid);
+                    $replyUsername = $userReply['username'];
+                }
+                $reply[] = [
+                    'uid' => $val->uid,
+                    'username' => $userComment['username'],
+                    'avatar' => $userComment['avatar'],
+                    'reply_username' => $replyUsername,
+                    'content' => $val->is_delete?'该评论已被删除':$val->content,
+                    'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
+                    'is_delete' => $val->is_delete,
+                ];
             }
             }
-            $reply[] = [
-                'uid' => $val->uid,
-                'username' => $userComment['username'],
-                'avatar' => $userComment['avatar'],
-                'reply_username' => $replyUsername,
-                'content' => $val->is_delete?'该评论已被删除':$val->content,
-                'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
-                'is_delete' => $val->is_delete,
-            ];
+            Redis::SET($replyKey, json_encode($reply));
+            Redis::EXPIRE($replyKey, 300);
         }
         }
         $user = $this->userInfo($postComment['uid']);
         $user = $this->userInfo($postComment['uid']);
         return [
         return [
@@ -44,7 +53,7 @@ class CommentTransformer extends TransformerAbstract
             'avatar' => $user['avatar'],
             'avatar' => $user['avatar'],
             'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
             'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
-            'reply_count' => $postComment->reply->count(),
+            'reply_count' => PostComment::where('parent_id', $postComment['id'])->count(),
             'reply' => $reply,
             'reply' => $reply,
             'is_delete' => $postComment['is_delete'],
             'is_delete' => $postComment['is_delete'],
         ];
         ];