feed = $feed; $this->postRepositories = $postRepositories; $this->postComment = $postComment; } public function getFeedType($action) { $type = [ 'like' => 1, 'forward' => 2, 'collect' => 3, 'comment' => 4, 'focus' => 5, 'publish' => 6 ]; return isset($type[$action]) ? $type[$action] : 0; } /** * 创建feed流 * 更新帖子统计数 * @param $request */ public function contentCreate($request){ $this->feedCreate($request); //关注操作不需要调用 if($request['behavior_flag']!='focus'){ $this->postRepositories->updatePostData($request); } } /** * 创建feed流内容 * @param $request */ public function feedCreate($request) { 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']:''; $feedType = $this->getFeedType($behaviorFlag); 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['type'] = $feedType; 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['post_type'] = $request['post_type']; }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['post_type'] = $request['post_type']; }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['post_type'] = $request['post_type']; }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)); $this->feed->insert($data); } } return true; } //我的feed 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']]; $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']); if($post){ $value['relate_data'] = $this->postDetail($post,$value['follow_uid'], $userInfo); }else{ unset($data[$key]); } } if($value['type'] == 5){ $value['content'] = null; $user = Redis::HGETALL('userInfo:'.$value['relate_id']); if(!$user){ $user = $this->userInfo($value['relate_id']); } if($user){ Log::debug("测试feed关注状态:uid{$userInfo['uid']}followUid{$value['relate_id']}"); $value['relate_data'] = [ 'uid' => intval($user['uid']), 'username' => subtext($user['username'],10), 'avatar' => $user['avatar'], 'follow_status' => $this->getFollowStatus($userInfo['uid'],$value['relate_id']), ]; }else{ unset($data[$key]); } } } } return $data; } public function postDetail($post,$follow_uid,$userInfo){ Log::debug('feed流内容--'.json_encode($post)); $uid = $userInfo['uid']; $imgs = []; foreach($post->imgs as $img){ $imgs[] = $img['img']; } $topic = []; foreach($post->topic() as $key => $val){ $topic[] = [ 'id' => $key, 'name' => $val ]; } $isFollow = 0; Log::debug("内容feed关注uid{$uid}followUid{$follow_uid}"); $followStatus = $this->getFollowStatus($uid, $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' => subtext($post['username'],10), '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 + 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, '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']:'', ]; } 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; } /** * 取消关注删除对应feed * @param $data */ 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()); return false; } } }