postId = $postId; $this->uid = $uid; } public function transform(PostComment $postComment) { $reply = []; $replyKey = 'post_new_reply_'.$postComment['id']; $replyData = Redis::GET($replyKey); if($replyData){ $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){ $userReply = $this->userInfo($val->reply_uid); $replyUsername = $userReply['username']; } $reply[] = [ 'id' => $val->id, '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, 'is_like' => $replyLike['is_like'], 'like_count' => $replyLike['like_count'], ]; $redisReply[] = [ 'id' => $val->id, 'uid' => $val->uid, 'username' => $userComment['username'], 'avatar' => $userComment['avatar'], 'reply_username' => $replyUsername, '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'], 'username' => $user['username'], 'avatar' => $user['avatar'], 'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'], 'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(), 'reply_count' => $postComment['reply_count'], 'reply' => $reply, 'is_delete' => $postComment['is_delete'], 'is_like' => $commentLike['is_like'], 'like_count' => $commentLike['like_count'], ]; } }