wzq 5 роки тому
батько
коміт
309ea2b837

+ 11 - 5
app/Http/Controllers/V1/PostController.php

@@ -9,6 +9,7 @@
 namespace App\Http\Controllers\V1;
 
 use App\Repositories\PostRepositories;
+use App\Traits\UserTrait;
 use App\Transformers\Post\SuggestTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
@@ -21,7 +22,7 @@ use League\Fractal\Resource\Item;
 
 class PostController extends Controller
 {
-
+    use UserTrait;
     public function __construct(PostRepositories $postRepositories)
     {
         $this->postRepositories = $postRepositories;
@@ -32,20 +33,25 @@ class PostController extends Controller
      */
     public function suggestPost(Request $request)
     {
+        $userInfo = $this->getUserInfo();
+        if(empty($userInfo)){
+            Log::info('获取用户信息失败');
+            return jsonError('获取用户信息失败');
+        }
         $param = $request->all();
         $purchaseList = $this->postRepositories->suggestPost($param);
         $fractal = new Manager();
-        $resource = new Collection($purchaseList, new SuggestTransformer());
+        $resource = new Collection($purchaseList, new SuggestTransformer($userInfo['uid']));
         $resource->setPaginator(new IlluminatePaginatorAdapter($purchaseList));
         $data = $fractal->createData($resource)->toArray();
 
         if(!(isset($param['current_page']) && $param['current_page'] > 1)){
             $newData = [];
             foreach($data['data'] as $key => $val){
-                $newData[] = $val;
-                if($key == 3){
-                    $newData[] = ['type' => 1];
+                if($key == 1){
+                    $newData[] = ['show_type' => 1];
                 }
+                $newData[] = $val;
             }
             $data['data'] = $newData;
         }

+ 5 - 0
app/Models/PostComment.php

@@ -14,4 +14,9 @@ class PostComment extends Model
 //
     protected $table = 'post_comment';
     protected $guarded = [];
+
+    public function reply()
+    {
+        return $this->hasMany('App\Models\PostComment', 'parent_id', 'id');
+    }
 }

+ 1 - 0
app/Providers/AppServiceProvider.php

@@ -17,5 +17,6 @@ class AppServiceProvider extends ServiceProvider
         Validator::extend('mobile', function ($attribute, $value, $parameters) {
             return preg_match('/^1[3456789]{1}\d{9}$/', $value);
         });
+        \Carbon\Carbon::setLocale('zh');
     }
 }

+ 28 - 0
app/Traits/UserTrait.php

@@ -0,0 +1,28 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/5/5
+ * Time: 17:11
+ */
+namespace App\Traits;
+use Tymon\JWTAuth\Facades\JWTAuth;
+
+trait UserTrait
+{
+
+    public function getUserInfo() {
+        try {
+            $sign = generateSign([], config('customer.app_secret'));
+            $url = config("customer.app_service_url").'/user/userInfo';
+            $array = [
+                'json' => ['sign' => $sign], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            return http($url,$array);
+        } catch (\Exception $e) {
+            return [];
+        }
+
+    }
+}

+ 44 - 6
app/Transformers/Post/SuggestTransformer.php

@@ -9,31 +9,69 @@
 namespace  App\Transformers\Post;
 
 use App\Models\Post;
+use App\Models\PostCollect;
+use App\Models\PostComment;
+use App\Models\PostLike;
 use Carbon\Carbon;
 use League\Fractal\TransformerAbstract;
 
 class SuggestTransformer extends TransformerAbstract
 {
+    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,
+                    'content' => $val->content,
+                ];
+            }
+            $comment[] = [
+                'id' => $item->id,
+                'nickname' => $item->nickname,
+                'content' => $item->content,
+                'reply_count' => $replyCount,
+                'reply' => $reply,
+            ];
+        }
         return [
-            'type' => 0,
+            'show_type' => 0,
             'id' => $post['id'],
-            'created_at' => Carbon::parse($post['created_at'])->toDateTimeString(),
+            'type' => $post['type'],
+            'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
             'uid' => $post['uid'],
             'username' => $post['username'],
             'avatar' => $post['avatar'],
             'topic' => $post->topic(),
-            'content' => subtext($post['content'], 20),
+            '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,
             'share_count' => $post->data->share_count,
             'comment_count' => $post->data->comment_count,
             'collect_count' => $post->data->collect_count,
-            'create_bean' => $post->data->create_bean,
-            'is_suggest' => $post['is_suggest'],
-            'is_hide' => $post['is_hide'],
+            'will_collect_bean' => $post->data->will_collect_bean,
+            'is_like' => PostLike::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' => 1,
         ];
     }
 }