wzq il y a 5 ans
Parent
commit
03d0038109

+ 4 - 4
app/Models/Post.php

@@ -27,9 +27,9 @@ class Post extends Model
         return $this->hasOne('App\Models\PostData', 'post_id', 'id');
     }
 
-    public function topic()
-    {
-        return Topic::whereIn('id', explode(',', $this->topic_ids))->pluck('name', 'id');
-    }
+//    public function topic()
+//    {
+//        return Topic::whereIn('id', explode(',', $this->topic_ids))->pluck('name', 'id');
+//    }
 
 }

+ 17 - 0
app/Traits/PostTrait.php

@@ -31,4 +31,21 @@ trait PostTrait
         }
         return intval($num * $t);
     }
+
+    //获取内容话题
+    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;
+    }
 }

+ 3 - 1
app/Transformers/Post/DetailTransformer.php

@@ -8,11 +8,13 @@
 
 namespace App\Transformers\Post;
 use App\Models\Post;
+use App\Traits\PostTrait;
 use Illuminate\Support\Carbon;
 use League\Fractal\TransformerAbstract;
 
 class DetailTransformer extends  TransformerAbstract
 {
+    use PostTrait;
     public function transform(Post $post)
     {
         $imgs = [];
@@ -25,7 +27,7 @@ class DetailTransformer extends  TransformerAbstract
             'uid' => $post['uid'],
             'username' => $post['username'],
             'avatar' => $post['avatar'],
-            'topic' => $post->topic(),
+            'topic' => $this->getTopic($post['topic_ids']),
             'title' => $post['title'],
             'content' => $post['content'],
             'type' => $post['type'],

+ 3 - 1
app/Transformers/Post/PostTransformer.php

@@ -9,11 +9,13 @@
 namespace App\Transformers\Post;
 
 use App\Models\Post;
+use App\Traits\PostTrait;
 use Illuminate\Support\Carbon;
 use League\Fractal\TransformerAbstract;
 
 class PostTransformer extends TransformerAbstract
 {
+    use PostTrait;
     public function transform(Post $post)
     {
         return [
@@ -22,7 +24,7 @@ class PostTransformer extends TransformerAbstract
             'uid' => $post['uid'],
             'username' => $post['username'],
             'avatar' => $post['avatar'],
-            'topic' => $post->topic(),
+            'topic' => $this->getTopic($post['topic_ids']),
             'content' => subtext(strip_tags($post['content']), 20),
             'location' => $post['location'],
             'pv' => $post->data->pv_real.'/'.$post->data->pv,