|
@@ -13,13 +13,16 @@ use App\Models\PostCollect;
|
|
|
use App\Models\PostComment;
|
|
|
use App\Models\PostDislike;
|
|
|
use App\Models\PostLike;
|
|
|
+use App\Traits\PostTrait;
|
|
|
use App\Traits\UserTrait;
|
|
|
use Carbon\Carbon;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
|
|
class SuggestTransformer extends TransformerAbstract
|
|
|
{
|
|
|
use UserTrait;
|
|
|
+ use PostTrait;
|
|
|
public function __construct($uid, $invite_code)
|
|
|
{
|
|
|
$this->uid = $uid;
|
|
@@ -27,36 +30,34 @@ class SuggestTransformer extends TransformerAbstract
|
|
|
}
|
|
|
public function transform(Post $post)
|
|
|
{
|
|
|
- $imgs = [];
|
|
|
- foreach($post->imgs as $img){
|
|
|
- $imgs[] = $img['img'];
|
|
|
- }
|
|
|
$comment = [];
|
|
|
- $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
|
|
|
- foreach($comments as $item){
|
|
|
- $userComment = $this->userInfo($item->uid);
|
|
|
- $comment[] = [
|
|
|
- 'id' => $item->id,
|
|
|
- 'username' => $userComment['username'],
|
|
|
- 'content' => $item->is_delete?'该评论已被删除':$item->content,
|
|
|
- ];
|
|
|
- }
|
|
|
- $topic = [];
|
|
|
- foreach($post->topic() as $key => $val){
|
|
|
- $topic[] = [
|
|
|
- 'id' => $key,
|
|
|
- 'name' => $val
|
|
|
- ];
|
|
|
+ $commentKey = 'post_new_comment_'.$post['id'];
|
|
|
+ $commentData = Redis::GET($commentKey);
|
|
|
+ if($commentData){
|
|
|
+ $comment = json_decode($commentData);
|
|
|
+ }else{
|
|
|
+ $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
|
|
|
+ foreach($comments as $item){
|
|
|
+ $userComment = $this->userInfo($item->uid);
|
|
|
+ $comment[] = [
|
|
|
+ 'id' => $item->id,
|
|
|
+ 'username' => $userComment['username'],
|
|
|
+ 'content' => $item->is_delete?'该评论已被删除':$item->content,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ Redis::SET($commentKey, json_encode($comment));
|
|
|
}
|
|
|
+
|
|
|
$isLike = 0;
|
|
|
$isDislike = 0;
|
|
|
$isCollect = 0;
|
|
|
if($this->uid){
|
|
|
- $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
|
|
|
- $isDislike = PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
|
|
|
- $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
|
|
|
+ $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
|
|
|
+ $isDislike = Redis::SISMEMBER('post_unlike_'.$post['id'], $this->uid);
|
|
|
+ $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
|
|
|
}
|
|
|
$user = $this->userInfo($post['uid']);
|
|
|
+ $postInfo = $this->getPostInfo($post['id']);
|
|
|
return [
|
|
|
'show_type' => 'post',
|
|
|
'id' => $post['id'],
|
|
@@ -65,18 +66,18 @@ class SuggestTransformer extends TransformerAbstract
|
|
|
'uid' => $post['uid'],
|
|
|
'username' => $user['username'],
|
|
|
'avatar' => $user['avatar'],
|
|
|
- 'topic' => $topic,
|
|
|
+ 'topic' => $this->getTopic($post['topic_ids']),
|
|
|
'title' => $post['title'],
|
|
|
'content' => $post['content'],
|
|
|
'location' => $post['location'],
|
|
|
'img' => $post['img'],
|
|
|
- 'imgs' => $imgs,
|
|
|
+ 'imgs' => $postInfo['imgs'],
|
|
|
'video' => $post['video'],
|
|
|
- 'pv' => getNumber($post->data->pv),
|
|
|
- 'praise_count' => $post->data->praise_count,
|
|
|
- 'comment_count' => $post->data->comment_count,
|
|
|
- 'collect_count' => $post->data->collect_count,
|
|
|
- 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
|
|
|
+ 'pv' => getNumber($postInfo['pv']),
|
|
|
+ 'praise_count' => $postInfo['praise_count'],
|
|
|
+ 'comment_count' => $postInfo['comment_count'],
|
|
|
+ 'collect_count' => $postInfo['collect_count'],
|
|
|
+ 'will_collect_bean' => $postInfo['will_collect_bean'],
|
|
|
'is_like' => $isLike,
|
|
|
'is_dislike' => $isDislike,
|
|
|
'is_collect' => $isCollect,
|