zhangchangchun vor 5 Jahren
Ursprung
Commit
05aad87fee

+ 34 - 0
app/Http/Controllers/V1/FeedController.php

@@ -0,0 +1,34 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-24
+ * Time: 14:40
+ */
+
+namespace App\Http\Controllers\V1;
+
+
+use App\Repositories\FeedRepositories;
+use App\Transformers\FeedTransformer;
+use Illuminate\Support\Facades\Validator;
+use League\Fractal\Manager;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+use League\Fractal\Resource\Collection;
+use Illuminate\Http\Request;
+
+class FeedController extends Controller {
+    public function __construct(FeedRepositories $feedRepositories) {
+        $this->feedRepositories = $feedRepositories;
+    }
+    //关注feed流
+    public function index(Request $request){
+        $data = $request->all();
+        $list = $this->feedRepositories->myFeed($data);
+        $fractal = new Manager();
+        $resource = new Collection($list, new FeedTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
+        $data = $fractal->createData($resource)->toArray();
+        return jsonSuccess($data);
+    }
+}

+ 4 - 0
app/Models/Post.php

@@ -32,4 +32,8 @@ class Post extends Model
         return Topic::whereIn('id', explode(',', $this->topic_ids))->pluck('name', 'id');
     }
 
+    public function comment(){
+        return $this->hasOne('App\Models\PostComment', 'post_id', 'id');
+    }
+
 }

+ 80 - 2
app/Repositories/FeedRepositories.php

@@ -11,15 +11,23 @@ namespace App\Repositories;
 
 use App\Models\Behavior;
 use App\Models\Feed;
+use App\Models\PostComment;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\Log;
+use Tymon\JWTAuth\Facades\JWTAuth;
+use App\Models\PostDislike;
+use App\Models\PostLike;
+use App\Models\PostCollect;
+use App\Traits\UserTrait;
 
 class FeedRepositories
 {
-    public function __construct(Feed $feed,PostRepositories $postRepositories)
+    use UserTrait;
+    public function __construct(Feed $feed,PostRepositories $postRepositories,PostComment $postComment)
     {
         $this->feed = $feed;
         $this->postRepositories = $postRepositories;
+        $this->postComment = $postComment;
     }
 
     public function getFeedType($action)
@@ -99,6 +107,76 @@ class FeedRepositories
         }
         return true;
     }
+    //我的feed
+    public function myFeed($request){
+        $token =  JWTAuth::decode(JWTAuth::getToken());
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+        $where[] = ['uid',$token['user']->uid];
+        $data = $this->feed
+            ->where($where)
+            ->orderBy('id','desc')
+            ->paginate($perPage);
+        if($data){
+            foreach ($data as $key=>$value){
+                if($value['type'] == 6){
+                    $post = $this->postRepositories->detail($value['relate_id']);
+                    $value['relate_data'] = $this->postDetail($post);
+                }
+                if($value['type'] == 5){
+                    $value['relate_data'] = $this->getFollowMemberFans($value['relate_id']);
+                }
+                $data[] = $value;
+            }
+        }
+        return $data;
+    }
+    public function postDetail($post){
+        $token =  JWTAuth::decode(JWTAuth::getToken());
+        $uid = $token['user']->uid;
+        $imgs = [];
+        foreach($post->imgs as $img){
+            $imgs[] = $img['img'];
+        }
 
-
+        $topic = [];
+        foreach($post->topic() as $key => $val){
+            $topic[] = [
+                'id' => $key,
+                'name' => $val
+            ];
+        }
+        $isFollow = 0;
+        $followStatus = $this->getFollowStatus($uid, $post['follow_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,
+            'available_bean' => $post->data->available_bean,
+            'will_collect_bean' => $post->data->will_collect_bean,
+            'post_comment' => $this->getPostComment($post['id']),
+            'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $uid)->exists()?1:0,
+            'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $uid)->exists()?1:0,
+            'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $uid)->exists()?1:0,
+            'is_follow' => $isFollow,
+        ];
+    }
+    public function getPostComment($post_id){
+       return $this->postComment->where(['post_id'=>$post_id,'parent_id'=>0,'is_delete'=>0  ])->select('id','uid','username','content')->orderBy('id','desc')->take(2)->get();
+    }
 }

+ 12 - 0
app/Traits/PostTrait.php

@@ -8,10 +8,15 @@
  */
 namespace App\Traits;
 
+use App\Models\Post;
 use Illuminate\Support\Facades\Redis;
 
 trait PostTrait
 {
+//    public function __construct(Post $post) {
+//        $this->post = $post;
+//    }
+
     //预计可获得彩虹豆数
     public function availableBean()
     {
@@ -31,4 +36,11 @@ trait PostTrait
         }
         return intval($num * $t);
     }
+    //详情
+    public function detail($id)
+    {
+        return Post::join('post_data', 'post_data.post_id', '=', 'post.id')
+            ->select('post.*')
+            ->find($id);
+    }
 }

+ 19 - 4
app/Traits/UserTrait.php

@@ -12,7 +12,7 @@ use Tymon\JWTAuth\Facades\JWTAuth;
 
 trait UserTrait
 {
-
+    //获取用户
     public function getUserInfo() {
         try {
             $sign = generateSign([], config('customer.app_secret'));
@@ -26,11 +26,12 @@ trait UserTrait
         }
 
     }
-
+    //检查关注状态
     public function getFollowStatus($uid, $followUid) {
         try {
             $sign = generateSign([], config('customer.app_secret'));
             $url = config("customer.app_service_url").'/user/v2/follow/checkStatus';
+            //$url = 'http://localhost:8080/v2/follow/checkStatus';
             $array = [
                 'json' => ['sign' => $sign, 'uid' => $followUid, 'follow_uid' => $uid], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
             ];
@@ -40,7 +41,7 @@ trait UserTrait
         }
 
     }
-
+    //检查关注状态 多个uid
     public function getFollowMembersStatus($uids) {
         try {
             $sign = generateSign([], config('customer.app_secret'));
@@ -53,6 +54,20 @@ trait UserTrait
             Log::debug($e->getMessage());
             return [];
         }
-
+    }
+    //检查关注状态 多个uid
+    public function getFollowMemberFans($follow_id) {
+        try {
+            $sign = generateSign([], config('customer.app_secret'));
+            $url = config("customer.app_service_url").'/user/v2/member/getFollowMemberFans';
+            //$url = 'http://localhost:8080/v2/member/getFollowMemberFans';
+            $array = [
+                'json' => ['sign' => $sign, 'follow_id' => $follow_id], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            return http($url,$array,'get');
+        } catch (\Exception $e) {
+            Log::debug($e->getMessage());
+            return [];
+        }
     }
 }

+ 46 - 0
app/Transformers/FeedTransformer.php

@@ -0,0 +1,46 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-24
+ * Time: 14:45
+ */
+
+namespace App\Transformers;
+
+
+use App\Models\Feed;
+use App\Repositories\PostRepositories;
+use App\Traits\PostTrait;
+use App\Traits\UserTrait;
+use League\Fractal\TransformerAbstract;
+use Carbon\Carbon;
+use App\Models\PostDislike;
+use App\Models\PostLike;
+
+class FeedTransformer extends TransformerAbstract {
+    use UserTrait;
+    use PostTrait;
+    public function transform(Feed $feed)
+    {
+        return [
+            'id' => $feed['id'],
+            'uid' => $feed['uid'],
+            'follow_uid' => $feed['follow_uid'],
+            'follow_username' => $feed['follow_username'],
+            'follow_avatar' => $feed['follow_avatar'],
+           // 'follow_status' => $this->getFollowStatus($feed['uid'],$feed['follow_uid']),
+            'type' => $feed['type'],
+            'relate_id' => $feed['relate_id'],
+            'relate_data' => $feed['relate_data'],
+            'content' => json_decode($feed['content']),
+            'created_at' => Carbon::parse($feed['created_at'])->diffForHumans(),
+        ];
+    }
+    public function getData($feed){
+//        if($feed['type'] == 6){
+//           return $this->detail($feed['relate_id']);
+//        }
+    }
+
+}

+ 2 - 0
routes/api.php

@@ -79,6 +79,8 @@ $api->version('v1', [
 
         //用户发布数,收藏数,转发数
         $api->get('post/memberPostStatistics', 'PostController@memberPostStatistics');
+        //关注feed流
+        $api->get('feed', 'FeedController@index');
     });
 
 });