wzq hace 5 años
padre
commit
8299e9bc02

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

@@ -15,6 +15,7 @@ use App\Transformers\Post\DetailTransformer;
 use App\Transformers\Post\ListTransformer;
 use App\Transformers\Post\ReplyTransformer;
 use App\Transformers\Post\SuggestTransformer;
+use App\Transformers\Topic\TopicDetailTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\Log;
@@ -189,4 +190,32 @@ class PostController extends Controller
     }
 
 
+    /**
+     * 话题详情
+     */
+    public function topicDetail(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+        $userInfo = $this->getUserInfo();
+        if(empty($userInfo)){
+            Log::info('获取用户信息失败');
+            return jsonError('获取用户信息失败');
+        }
+        $detail = $this->postRepositories->topicDetail($request['id']);
+        if(!$detail){
+            return jsonError('获取话题信息失败');
+        }
+        $fractal = new Manager();
+        $res = new Item($detail, new TopicDetailTransformer($userInfo['uid']));
+        $data = $fractal->createData($res)->toArray();
+
+        return jsonSuccess($data);
+    }
+
+
 }

+ 4 - 0
app/Models/Topic.php

@@ -15,4 +15,8 @@ class Topic extends Model
     protected $table = 'topic';
     protected $guarded = [];
 
+    public function follow()
+    {
+        return $this->hasMany('App\Models\MemberFollowTopic', 'topic_id', 'id');
+    }
 }

+ 10 - 0
app/Repositories/PostRepositories.php

@@ -408,4 +408,14 @@ class PostRepositories
     }
 
 
+    /**
+     * 话题详情
+     */
+    public function topicDetail($id)
+    {
+        return $this->topic
+            ->find($id);
+    }
+
+
 }

+ 1 - 0
app/Transformers/Post/CommentTransformer.php

@@ -25,6 +25,7 @@ class CommentTransformer extends TransformerAbstract
                 'avatar' => $val->avatar,
                 'reply_username' => $val->reply_username,
                 'content' => $val->content,
+                'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
             ];
         }
         return [

+ 26 - 0
app/Transformers/Topic/TopicDetailTransformer.php

@@ -0,0 +1,26 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/17
+ * Time: 18:10
+ */
+namespace  App\Transformers\Topic;
+
+use App\Models\Topic;
+use Carbon\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class TopicDetailTransformer extends TransformerAbstract
+{
+    public function __construct($uid)
+    {
+        $this->uid = $uid;
+    }
+    public function transform(Topic $topic)
+    {
+        return [
+            'id' => $topic['id'],
+        ];
+    }
+}

+ 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/detail', 'PostController@topicDetail');
         //关注推荐话题
         $api->post('memberFollowTopic', 'MemberFollowTopic@memberFollowTopic');
         //关注话题