|
@@ -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'],
|
|
];
|
|
];
|