|
@@ -10,6 +10,7 @@ namespace App\Traits;
|
|
|
|
|
|
use App\Models\Post;
|
|
|
use App\Models\PostComment;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
trait PostTrait
|
|
@@ -81,26 +82,41 @@ trait PostTrait
|
|
|
}
|
|
|
|
|
|
//获取内容最新评论
|
|
|
- public function getNewComment($id)
|
|
|
+ public function getNewComment($id, $uid = 0)
|
|
|
{
|
|
|
$comment = [];
|
|
|
+ $key = 'comment_like_'.$id;
|
|
|
$commentKey = 'post_new_comment_'.$id;
|
|
|
$commentData = Redis::GET($commentKey);
|
|
|
if($commentData){
|
|
|
$comment = json_decode($commentData);
|
|
|
+ foreach($comment as &$item){
|
|
|
+ $isLike = 0;
|
|
|
+ if($uid){
|
|
|
+ $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
|
|
|
+ }
|
|
|
+ $item->is_like = $isLike;
|
|
|
+ }
|
|
|
}else{
|
|
|
- $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
|
|
|
+ $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('like_count', 'desc')->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
|
|
|
foreach($comments as $item){
|
|
|
$userComment = $this->userInfo($item->uid);
|
|
|
+ $isLike = 0;
|
|
|
+ if($uid){
|
|
|
+ $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
|
|
|
+ }
|
|
|
+ $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
|
|
|
$comment[] = [
|
|
|
'id' => $item->id,
|
|
|
'username' => $userComment['username'],
|
|
|
'content' => $item->is_delete?'该评论已被删除':$item->content,
|
|
|
'is_delete' => $item->is_delete,
|
|
|
+ 'is_like' => $isLike,
|
|
|
+ 'like_count' => $likeCount,
|
|
|
];
|
|
|
}
|
|
|
Redis::SET($commentKey, json_encode($comment));
|
|
|
- Redis::EXPIRE($commentKey, 604800);
|
|
|
+ Redis::EXPIRE($commentKey, 3);
|
|
|
}
|
|
|
return $comment;
|
|
|
}
|