xielin hace 5 años
padre
commit
e724421d23
Se han modificado 1 ficheros con 68 adiciones y 67 borrados
  1. 68 67
      app/Repositories/FeedRepositories.php

+ 68 - 67
app/Repositories/FeedRepositories.php

@@ -24,8 +24,7 @@ use App\Traits\UserTrait;
 class FeedRepositories
 {
     use UserTrait;
-
-    public function __construct(Feed $feed, PostRepositories $postRepositories, PostComment $postComment)
+    public function __construct(Feed $feed,PostRepositories $postRepositories,PostComment $postComment)
     {
         $this->feed = $feed;
         $this->postRepositories = $postRepositories;
@@ -50,11 +49,10 @@ class FeedRepositories
      *  更新帖子统计数
      * @param $request
      */
-    public function contentCreate($request)
-    {
+    public function contentCreate($request){
         $this->feedCreate($request);
         //关注操作不需要调用
-        if ($request['behavior_flag'] != 'focus') {
+        if($request['behavior_flag']!='focus'){
             $this->postRepositories->updatePostData($request);
         }
     }
@@ -66,54 +64,64 @@ class FeedRepositories
      */
     public function feedCreate($request)
     {
-        Log::debug('创建Feed流-请求参数:' . json_encode($request));
-        $fans = isset($request['fans']) ? $request['fans'] : [];
-        if (empty($fans)) {//没有粉丝,不用插入
+        Log::debug('创建Feed流-请求参数:'.json_encode($request));
+        $fans = isset($request['fans'])?$request['fans']:[];
+        if(empty($fans)){//没有粉丝,不用插入
             Log::debug('创建Feed流-没有粉丝,不用创建相关feed流');
             return true;
         }
-        $behaviorFlag = isset($request['behavior_flag']) ? $request['behavior_flag'] : '';
+        $behaviorFlag = isset($request['behavior_flag'])?$request['behavior_flag']:'';
         $feedType = $this->getFeedType($behaviorFlag);
-        Log::debug('创建Feed流-feed类型:' . $feedType);
-        if ($feedType) {
+        Log::debug('创建Feed流-feed类型:'.$feedType);
+        if($feedType){
             $data = [];
             foreach ($fans as $fan) {
                 $data['uid'] = $fan;
                 $data['follow_uid'] = $request['target_id'];
                 $data['follow_username'] = $request['target_username'];
-                $data['follow_avatar'] = isset($request['target_avatar']) ? $request['target_avatar'] : '';
+                $data['follow_avatar'] = isset($request['target_avatar'])?$request['target_avatar']:'';
                 $data['type'] = $feedType;
 
-                if (in_array($feedType, [1, 2, 3])) {
+                if(in_array($feedType,[1,2,3])){
                     $data['relate_id'] = $request['post_id'];
                     $content['post_desc'] = $request['post_desc'];
-                    $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
+                    $content['beans'] = isset($request['rewards']['bean'])?intval($request['rewards']['bean']):0;
                     $content['post_type'] = $request['post_type'];
-                } elseif ($feedType == 4) {//评论
+                }elseif ($feedType==4){//评论
                     $data['relate_id'] = $request['post_id'];
                     $content['post_desc'] = $request['post_desc'];
                     $content['comment_id'] = $request['comment_id'];
                     $content['comment_desc'] = $request['comment_content'];
-                    $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
+                    $content['beans'] = isset($request['rewards']['bean'])?intval($request['rewards']['bean']):0;
                     $content['post_type'] = $request['post_type'];
-                } elseif ($feedType == 6) {//发布
+                }elseif ($feedType==6){//发布
                     $data['relate_id'] = $request['post_id'];
                     $content['post_desc'] = $request['post_desc'];
-                    $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
+                    $content['beans'] = isset($request['rewards']['bean'])?intval($request['rewards']['bean']):0;
                     $content['post_type'] = $request['post_type'];
-                } elseif ($feedType == 5) {//关注
+                }elseif ($feedType==5){//关注
                     $data['relate_id'] = $request['focus_uid'];
                     $content = [];
                 }
                 $data['content'] = json_encode($content);
                 $data['created_at'] = Carbon::now();
                 $data['updated_at'] = Carbon::now();
-                Log::debug('创建Feed流-data:' . json_encode($data));
+                Log::debug('创建Feed流-data:'.json_encode($data));
                 //目前只有评论可以重复出现在别人的feed流中,其他类型如果出现过一次不再产生新的feed信息
-                if (in_array($feedType, [4])) {
+                if(in_array($feedType,[4])){
                     $this->feed->insert($data);
-                } else {
-                    if (isset($request['is_existing']) && intval($request['is_existing']) == 0) {
+                }else{
+                    //此处需要优化,如果feed表需要清理历史数据怎么处理
+                    //以下判断可以控制virus的行为,重复不添加feed,但是关注用户不行,关注用户不存在is_existing字段
+                    //if (isset($request['is_existing']) && intval($request['is_existing']) == 0) {
+                    //    $this->feed->insert($data);
+                    //}
+                    $feedRow = $this->feed->where('uid',$fan)
+                        ->where('follow_uid',$request['target_id'])
+                        ->where('type',$feedType)
+                        ->where('relate_id',$request['post_id'])
+                        ->first();
+                    if(empty($feedRow)){
                         $this->feed->insert($data);
                     }
                 }
@@ -122,45 +130,43 @@ class FeedRepositories
         }
         return true;
     }
-
     //我的feed
-    public function myFeed($request)
-    {
+    public function myFeed($request){
         $userInfo = $this->getUserInfo();
         if (empty($userInfo)) {
             return jsonError('获取用户信息失败');
         }
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
-        $where[] = ['uid', $userInfo['uid']];
+        $where[] = ['uid',$userInfo['uid']];
         $data = $this->feed
             ->where($where)
-            ->orderBy('id', 'desc')
+            ->orderBy('id','desc')
             ->paginate($perPage);
-        if ($data) {
-            foreach ($data as $key => &$value) {
-                if ($value['type'] == 6) {
+        if($data){
+            foreach ($data as $key=>&$value){
+                if($value['type'] == 6){
                     $post = $this->postRepositories->detail($value['relate_id']);
-                    if ($post) {
-                        $value['relate_data'] = $this->postDetail($post, $value['follow_uid'], $userInfo);
-                    } else {
+                    if($post){
+                        $value['relate_data'] = $this->postDetail($post,$value['follow_uid'], $userInfo);
+                    }else{
                         unset($data[$key]);
                     }
                 }
-                if ($value['type'] == 5) {
+                if($value['type'] == 5){
                     $value['content'] = null;
-                    $user = Redis::HGETALL('userInfo:' . $value['relate_id']);
-                    if (!$user) {
+                    $user = Redis::HGETALL('userInfo:'.$value['relate_id']);
+                    if(!$user){
                         $user = $this->userInfo($value['relate_id']);
                     }
-                    if ($user) {
+                    if($user){
                         Log::debug("测试feed关注状态:uid{$userInfo['uid']}followUid{$value['relate_id']}");
                         $value['relate_data'] = [
                             'uid' => intval($user['uid']),
                             'username' => $user['username'],
                             'avatar' => $user['avatar'],
-                            'follow_status' => $this->getFollowStatus($userInfo['uid'], $value['relate_id']),
+                            'follow_status' => $this->getFollowStatus($userInfo['uid'],$value['relate_id']),
                         ];
-                    } else {
+                    }else{
                         unset($data[$key]);
                     }
                 }
@@ -168,18 +174,16 @@ class FeedRepositories
         }
         return $data;
     }
-
-    public function postDetail($post, $follow_uid, $userInfo)
-    {
-        Log::debug('feed流内容--' . json_encode($post));
+    public function postDetail($post,$follow_uid,$userInfo){
+        Log::debug('feed流内容--'.json_encode($post));
         $uid = $userInfo['uid'];
         $imgs = [];
-        foreach ($post->imgs as $img) {
+        foreach($post->imgs as $img){
             $imgs[] = $img['img'];
         }
 
         $topic = [];
-        foreach ($post->topic() as $key => $val) {
+        foreach($post->topic() as $key => $val){
             $topic[] = [
                 'id' => $key,
                 'name' => $val
@@ -188,7 +192,7 @@ class FeedRepositories
         $isFollow = 0;
         Log::debug("内容feed关注uid{$uid}followUid{$follow_uid}");
         $followStatus = $this->getFollowStatus($uid, $follow_uid);
-        if ($followStatus) {
+        if($followStatus){
             $isFollow = $followStatus;
         }
         return [
@@ -211,38 +215,35 @@ class FeedRepositories
             'available_bean' => $post->data->available_bean,
             'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
             '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_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,
             'follow_status' => $isFollow,
-            'h5url' => config('customer.share_post_h5url') . "?post_id={$post['id']}&invite_code={$userInfo['invite_code']}",
-            'desc_url' => $post['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $post['id'] : '',
+            'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$userInfo['invite_code']}",
+            'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
         ];
     }
-
-    public function getPostComment($post_id)
-    {
-        $comments = $this->postComment->where(['post_id' => $post_id, 'parent_id' => 0])->select('id', 'uid', 'username', 'content', 'is_delete')->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->take(2)->get();
-        foreach ($comments as &$comment) {
-            if ($comment->is_delete) {
+    public function getPostComment($post_id){
+       $comments = $this->postComment->where(['post_id'=>$post_id,'parent_id'=>0])->select('id','uid','username','content','is_delete')->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->take(2)->get();
+       foreach($comments as &$comment){
+           if($comment->is_delete){
                 $comment->content = '该评论已被删除';
-            }
-            unset($comment->is_delete);
-        }
-        return $comments;
+           }
+           unset($comment->is_delete);
+       }
+       return $comments;
     }
 
     /**
      *  取消关注删除对应feed
      * @param $data
      */
-    public function contentFeedDelete($data)
-    {
-        try {
+    public function contentFeedDelete($data){
+        try{
             $this->feed->where('uid', $data['uid'])->where('follow_uid', $data['follow_uid'])->delete();
             Log::debug("取消关注删除对应feed成功uid{$data['uid']}followUid{$data['follow_uid']}");
-        } catch (\Exception $exception) {
-            Log::error('取消关注删除对应feed失败' . json_encode($data) . $exception->getMessage());
+        }catch (\Exception $exception){
+            Log::error('取消关注删除对应feed失败'.json_encode($data).$exception->getMessage());
             return false;
         }
     }