wzq пре 5 година
родитељ
комит
6ce43f90f8

+ 14 - 36
app/Repositories/FeedRepositories.php

@@ -12,6 +12,7 @@ namespace App\Repositories;
 use App\Models\Behavior;
 use App\Models\Feed;
 use App\Models\PostComment;
+use App\Traits\PostTrait;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -25,6 +26,7 @@ use App\Traits\UserTrait;
 class FeedRepositories
 {
     use UserTrait;
+    use PostTrait;
 
     public function __construct(PostRepositories $postRepositories, PostComment $postComment)
     {
@@ -186,18 +188,8 @@ class FeedRepositories
     {
         Log::debug('feed流内容--' . json_encode($post));
         $uid = $userInfo['uid'];
-        $imgs = [];
-        foreach ($post->imgs as $img) {
-            $imgs[] = $img['img'];
-        }
+        $postInfo = $this->getPostInfo($post['id']);
 
-        $topic = [];
-        foreach ($post->topic() as $key => $val) {
-            $topic[] = [
-                'id' => $key,
-                'name' => $val
-            ];
-        }
         $isFollow = 0;
         Log::debug("内容feed关注uid{$uid}followUid{$follow_uid}");
         $followStatus = $this->getFollowStatus($uid, $follow_uid);
@@ -212,42 +204,28 @@ class FeedRepositories
             '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,
-            'available_bean' => $post->data->available_bean,
-            'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
-            'post_comment' => $this->getPostComment($post['id']),
-            'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $uid)->exists() ? 1 : 0,
-            'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $uid)->exists() ? 1 : 0,
-            'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $uid)->exists() ? 1 : 0,
+            'pv' => getNumber($postInfo['pv']),
+            'praise_count' => $postInfo['praise_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'available_bean' => $postInfo['available_bean'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
+            'post_comment' => $this->getNewComment($post['id']),
+            'is_like' => Redis::SISMEMBER('post_like_'.$post['id'], $uid),
+            'is_dislike' => Redis::SISMEMBER('post_unlike_'.$post['id'], $uid),
+            'is_collect' => Redis::SISMEMBER('post_collect_'.$post['id'], $uid),
             'follow_status' => $isFollow,
             'h5url' => config('customer.share_post_h5url') . "?post_id={$post['id']}&invite_code={$userInfo['invite_code']}",
             'desc_url' => $post['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $post['id'] : '',
         ];
     }
 
-    public function getPostComment($post_id)
-    {
-        $comments = $this->postComment->where(['post_id' => $post_id, 'parent_id' => 0])->select('id', 'uid', 'content', 'is_delete')->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->take(2)->get();
-        foreach ($comments as &$comment) {
-            if ($comment->is_delete) {
-                $comment->content = '该评论已被删除';
-            }
-            unset($comment->is_delete);
-            $user = $this->userInfo($comment->uid);
-            $comment->username = $user['username'];
-        }
-        return $comments;
-    }
-
     /**
      *  取消关注删除对应feed
      * @param $data

+ 28 - 0
app/Traits/PostTrait.php

@@ -9,6 +9,7 @@
 namespace App\Traits;
 
 use App\Models\Post;
+use App\Models\PostComment;
 use Illuminate\Support\Facades\Redis;
 
 trait PostTrait
@@ -65,9 +66,36 @@ trait PostTrait
             $data['praise_count'] = intval($data['praise_count']);
             $data['comment_count'] = intval($data['comment_count']);
             $data['collect_count'] = intval($data['collect_count']);
+            $data['available_bean'] = intval($data['available_bean']);
+            $data['collect_bean'] = intval($data['collect_bean']);
             $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
             $data['imgs'] = json_decode($data['imgs'], true);
         }
         return $data;
     }
+
+    //获取内容最新评论
+    public function getNewComment($id)
+    {
+        $comment = [];
+        $commentKey = 'post_new_comment_'.$id;
+        $commentData = Redis::GET($commentKey);
+        if($commentData){
+            $comment = json_decode($commentData);
+        }else{
+            $comments = PostComment::where('post_id', $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,
+                    'is_delete' => $item->is_delete,
+                ];
+            }
+            Redis::SET($commentKey, json_encode($comment));
+            Redis::EXPIRE($commentKey, 300);
+        }
+        return $comment;
+    }
 }

+ 15 - 24
app/Transformers/Post/DetailTransformer.php

@@ -12,13 +12,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 DetailTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -26,33 +29,21 @@ class DetailTransformer extends TransformerAbstract
     }
     public function transform(Post $post)
     {
-        $imgs = [];
-        foreach($post->imgs as $img){
-            $imgs[] = $img['img'];
-        }
-
-        $topic = [];
-        foreach($post->topic() as $key => $val){
-            $topic[] = [
-                'id' => $key,
-                'name' => $val
-            ];
-        }
-
         $isLike = 0;
         $isDislike = 0;
         $isCollect = 0;
         $isFollow = 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);
             $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
             if($followStatus){
                 $isFollow = $followStatus;
             }
         }
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -60,20 +51,20 @@ class DetailTransformer extends TransformerAbstract
             'uid' => $post['uid'],
             'username' => $user['username'],
             'avatar' => $user['avatar'],
-            'topic' => $topic,
+            'topic' => $this->getTopic($post['topic_ids']),
             'topic_ids' => $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,
-            'collect_count' => $post->data->collect_count,
-            'comment_count' => $post->data->comment_count,
-            'available_bean' => $post->data->available_bean,
-            'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
+            'pv' => getNumber($postInfo['pv']),
+            'praise_count' => $postInfo['praise_count'],
+            'collect_count' => $postInfo['collect_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'available_bean' => $postInfo['available_bean'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
             'is_like' => $isLike,
             'is_dislike' => $isDislike,
             'is_collect' => $isCollect,

+ 6 - 2
app/Transformers/Post/ListTransformer.php

@@ -9,12 +9,15 @@ namespace  App\Transformers\Post;
 
 use App\Models\Post;
 use App\Models\PostLike;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 
 class ListTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -24,9 +27,10 @@ class ListTransformer extends TransformerAbstract
     {
         $isLike = 0;
         if($this->uid){
-            $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
+            $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
         }
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -36,7 +40,7 @@ class ListTransformer extends TransformerAbstract
             'title' => $post['title'],
             'content' => subtext($post['content'], 100),
             'img' => $post['img'],
-            'praise_count' => $post->data->praise_count,
+            'praise_count' => $postInfo['praise_count'],
             'is_like' => $isLike,
             'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
             'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',

+ 4 - 1
app/Transformers/Post/MyTransformer.php

@@ -8,17 +8,20 @@
 namespace  App\Transformers\Post;
 
 use App\Models\Post;
+use App\Traits\PostTrait;
 use League\Fractal\TransformerAbstract;
 
 class MyTransformer extends TransformerAbstract
 {
+    use PostTrait;
     public function transform(Post $post)
     {
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'img' => $post['deleted_at']?config('customer.post_delete_image'):$post['img'],
             'type' => $post['type'],
-            'collect_bean' => $post->data->collect_bean > 0 ? $post->data->collect_bean : 0,
+            'collect_bean' => $postInfo['collect_bean'] > 0 ? $postInfo['collect_bean'] : 0,
         ];
     }
 }

+ 11 - 20
app/Transformers/Post/PostTransformer.php

@@ -12,6 +12,7 @@ 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 League\Fractal\TransformerAbstract;
@@ -19,24 +20,14 @@ use League\Fractal\TransformerAbstract;
 class PostTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct()
     {
     }
     public function transform(Post $post)
     {
-        $imgs = [];
-        foreach($post->imgs as $img){
-            $imgs[] = $img['img'];
-        }
-
-        $topic = [];
-        foreach($post->topic() as $key => $val){
-            $topic[] = [
-                'id' => $key,
-                'name' => $val
-            ];
-        }
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -44,19 +35,19 @@ class PostTransformer 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,
-            'collect_count' => $post->data->collect_count,
-            'comment_count' => $post->data->comment_count,
-            'available_bean' => $post->data->available_bean,
-            'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
+            'pv' => getNumber($postInfo['pv']),
+            'praise_count' => $postInfo['praise_count'],
+            'collect_count' => $postInfo['collect_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'available_bean' => $postInfo['available_bean'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
         ];
     }
 }

+ 2 - 23
app/Transformers/Post/SuggestTransformer.php

@@ -9,10 +9,6 @@
 namespace  App\Transformers\Post;
 
 use App\Models\Post;
-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;
@@ -30,24 +26,7 @@ class SuggestTransformer extends TransformerAbstract
     }
     public function transform(Post $post)
     {
-        $comment = [];
-        $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));
-            Redis::EXPIRE($commentKey, 300);
-        }
+
 
         $isLike = 0;
         $isDislike = 0;
@@ -82,7 +61,7 @@ class SuggestTransformer extends TransformerAbstract
             'is_like' => $isLike,
             'is_dislike' => $isDislike,
             'is_collect' => $isCollect,
-            'comment' => $comment,
+            'comment' => $this->getNewComment($post['id']),
             'is_follow' => $this->getFollowStatus($this->uid, $post['uid']),
             'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
             'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',

+ 12 - 14
app/Transformers/Post/VideoTransformer.php

@@ -10,12 +10,15 @@ namespace  App\Transformers\Post;
 use App\Models\Post;
 use App\Models\PostCollect;
 use App\Models\PostLike;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 
 class VideoTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -27,21 +30,16 @@ class VideoTransformer extends TransformerAbstract
         $isCollect = 0;
         $isFollow = 0;
         if($this->uid){
-            $isLike = PostLike::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);
+            $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
             $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
             if($followStatus){
                 $isFollow = $followStatus;
             }
         }
-        $topic = [];
-        foreach($post->topic() as $key => $val){
-            $topic[] = [
-                'id' => $key,
-                'name' => $val
-            ];
-        }
+
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -52,11 +50,11 @@ class VideoTransformer extends TransformerAbstract
             'content' => $post['content'],
             'img' => $post['img'],
             'video' => $post['video'],
-            'topic' => $topic,
-            'praise_count' => $post->data->praise_count,
-            'collect_count' => $post->data->collect_count,
-            'comment_count' => $post->data->comment_count,
-            'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
+            'topic' => $this->getTopic($post['topic_ids']),
+            'praise_count' => $postInfo['praise_count'],
+            'collect_count' => $postInfo['collect_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
             'is_like' => $isLike,
             'is_collect' => $isCollect,
             'is_follow' => $isFollow,