wzq 5 gadi atpakaļ
vecāks
revīzija
39b97e6427
2 mainītis faili ar 61 papildinājumiem un 33 dzēšanām
  1. 31 4
      app/Traits/PostTrait.php
  2. 30 29
      app/Transformers/Post/SuggestTransformer.php

+ 31 - 4
app/Traits/PostTrait.php

@@ -13,10 +13,6 @@ use Illuminate\Support\Facades\Redis;
 
 trait PostTrait
 {
-//    public function __construct(Post $post) {
-//        $this->post = $post;
-//    }
-
     //预计可获得U米数
     public function availableBean()
     {
@@ -43,4 +39,35 @@ trait PostTrait
             ->select('post.*')
             ->find($id);
     }
+
+    //获取内容话题
+    public function getTopic($topic_ids)
+    {
+        $ids = explode(',', $topic_ids);
+        $topic = [];
+        foreach($ids as $id){
+            $name = $topicNameArray = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
+            if($name && isset($name[0])){
+                $topic[] = [
+                    'id' => intval($id),
+                    'name' => $name[0],
+                ];
+            }
+        }
+        return $topic;
+    }
+
+    //获取内容详情
+    public function getPostInfo($id)
+    {
+        $data =  Redis::HGETALL('post_info_'.$id);
+        if($data){
+            $data['praise_count'] = intval($data['praise_count']);
+            $data['comment_count'] = intval($data['comment_count']);
+            $data['collect_count'] = intval($data['collect_count']);
+            $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
+            $data['imgs'] = json_decode($data['imgs'], true);
+        }
+        return $data;
+    }
 }

+ 30 - 29
app/Transformers/Post/SuggestTransformer.php

@@ -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,