浏览代码

话题内容列表

wzq 5 年之前
父节点
当前提交
563dcd6f45

+ 20 - 0
app/Http/Controllers/V1/PostController.php

@@ -16,6 +16,7 @@ use App\Transformers\Post\ListTransformer;
 use App\Transformers\Post\ReplyTransformer;
 use App\Transformers\Post\SuggestTransformer;
 use App\Transformers\Topic\TopicDetailTransformer;
+use App\Transformers\Topic\TopicPostTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\Log;
@@ -217,5 +218,24 @@ class PostController extends Controller
         return jsonSuccess($data);
     }
 
+    /**
+     * 话题内容列表
+     */
+    public function topicPost(Request $request)
+    {
+        $userInfo = $this->getUserInfo();
+        if(empty($userInfo)){
+            Log::info('获取用户信息失败');
+            return jsonError('获取用户信息失败');
+        }
+        $list = $this->postRepositories->topicPost($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($list, new TopicPostTransformer($userInfo['uid']));
+        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
+        $data = $fractal->createData($resource)->toArray();
+
+        return jsonSuccess($data);
+    }
+
 
 }

+ 15 - 0
app/Repositories/PostRepositories.php

@@ -314,6 +314,21 @@ class PostRepositories
             ->paginate($perPage);
     }
 
+    /**
+     * 话题内容
+     */
+    public function topicPost($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+
+        return $this->post
+            ->join('post_data', 'post_data.post_id', '=', 'post.id')
+            ->select('post.*')
+            ->whereRaw('FIND_IN_SET(' . $request['id'] . ',post.topic_ids)')
+            ->orderBy('id','desc')
+            ->paginate($perPage);
+    }
+
 
     /**
      * 评论列表

+ 90 - 0
app/Transformers/Topic/TopicPostTransformer.php

@@ -0,0 +1,90 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/17
+ * Time: 19:26
+ */
+namespace  App\Transformers\Topic;
+
+use App\Models\Post;
+use App\Models\PostCollect;
+use App\Models\PostComment;
+use App\Models\PostDislike;
+use App\Models\PostLike;
+use App\Traits\UserTrait;
+use Carbon\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class TopicPostTransformer extends TransformerAbstract
+{
+    use UserTrait;
+    public function __construct($uid)
+    {
+        $this->uid = $uid;
+    }
+    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('id', 'desc')->limit(2)->get();
+        foreach($comments as $item){
+            $replyCount = $item->reply->count();
+            $replies = PostComment::where('parent_id', $item->id)->orderBy('id', 'desc')->limit(2)->get();
+            $reply = [];
+            foreach($replies as $val){
+                $reply[] = [
+                    'nickname' => $val->nickname,
+                    'reply_username' => $val->reply_username,
+                    'content' => $val->content,
+                ];
+            }
+            $comment[] = [
+                'id' => $item->id,
+                'nickname' => $item->nickname,
+                'content' => $item->content,
+                'reply_count' => $replyCount,
+                'reply' => $reply,
+            ];
+        }
+        $topic = [];
+        foreach($post->topic() as $key => $val){
+            $topic[] = [
+                'id' => $key,
+                'name' => $val
+            ];
+        }
+        $isFollow = 0;
+        $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
+        if($followStatus){
+            $isFollow = $followStatus;
+        }
+        return [
+            'id' => $post['id'],
+            'type' => $post['type'],
+            'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
+            'uid' => $post['uid'],
+            'username' => $post['username'],
+            'avatar' => $post['avatar'],
+            'topic' => $topic,
+            'title' => $post['title'],
+            'content' => $post['content'],
+            'location' => $post['location'],
+            'img' => $post['img'],
+            'imgs' => $imgs,
+            'video' => $post['video'],
+            'pv' => $post->data->pv,
+            'praise_count' => $post->data->praise_count,
+            'comment_count' => $post->data->comment_count,
+            'will_collect_bean' => $post->data->will_collect_bean,
+            'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
+            'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
+            'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
+            'comment' => $comment,
+            'is_follow' => $isFollow,
+        ];
+    }
+}

+ 1 - 0
routes/api.php

@@ -29,6 +29,7 @@ $api->version('v1', [
         $api->get('post/reply', 'PostController@replyList');
         $api->get('topicCategory', 'CategoryController@index');
         $api->get('topicCategory/getTopics', 'CategoryController@getTopics');
+        $api->get('post/topic', 'PostController@topicPost');
         $api->get('topic/detail', 'PostController@topicDetail');
         //关注推荐话题
         $api->post('memberFollowTopic', 'MemberFollowTopic@memberFollowTopic');