123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- /**
- * Created by PhpStorm.
- * User: edz
- * Date: 2019-06-10
- * Time: 17:53
- */
- namespace App\Repositories;
- use App\Models\Behavior;
- use App\Models\Feed;
- use App\Models\PostComment;
- use App\Traits\PostTrait;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use Tymon\JWTAuth\Facades\JWTAuth;
- use App\Models\PostDislike;
- use App\Models\PostLike;
- use App\Models\PostCollect;
- use App\Traits\UserTrait;
- class FeedRepositories
- {
- use UserTrait;
- use PostTrait;
- public function __construct(PostRepositories $postRepositories, PostComment $postComment)
- {
- $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) {
- // 不记录自己的其他行为到自己的关注列表,除了发布
- if ($fan == $request['target_id'] && $feedType != 6) {
- continue;
- }
- $number = substr($fan, -1);
- $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));
- //目前只有评论可以重复出现在别人的feed流中,其他类型如果出现过一次不再产生新的feed信息
- if (in_array($feedType, [4])) {
- DB::table('feed_' . $number)->insert($data);
- } else {
- //此处需要优化,如果feed表需要清理历史数据怎么处理
- //以下判断可以控制virus的行为,重复不添加feed,但是关注用户不行,关注用户不存在is_existing字段
- //if (isset($request['is_existing']) && intval($request['is_existing']) == 0) {
- // DB::table('feed_'.$number)->insert($data);
- //}
- $feedRow = DB::table('feed_' . $number)->where('uid', $data['uid'])
- ->where('follow_uid', $data['follow_uid'])
- ->where('type', $data['type'])
- ->where('relate_id', $data['relate_id'])
- ->first();
- if (empty($feedRow)) {
- DB::table('feed_' . $number)->insert($data);
- }
- }
- }
- }
- return true;
- }
- //我的feed
- public function myFeed($request)
- {
- $userInfo = $this->getUserInfo();
- if (empty($userInfo)) {
- $userInfo['uid'] = 0;
- $blacklist = [];
- } else {
- $blacklist = Redis::smembers('blacklist_' . $userInfo['uid']);
- }
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where[] = ['uid', $userInfo['uid']];
- $number = substr($userInfo['uid'], -1);
- $table_name = 'feed_' . $number;
- $feedModel = new Feed();
- $feedModel->setTable($table_name);
- $data = $feedModel->where(function ($query) use ($blacklist) {
- if ($blacklist) {
- $query->whereNotIn('follow_uid', $blacklist);
- }
- })->where($where)
- ->whereIn('type', [4, 5, 6])//目前只展示发布,评论 关注
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- if ($data) {
- foreach ($data as $key => &$value) {
- if ($value['type'] == 6) {
- $post = $this->getPostInfo($value['relate_id'], 1);
- if ($post) {
- $value['relate_data'] = $this->postDetail($post, $value['follow_uid'], $userInfo);
- } else {
- unset($data[$key]);
- }
- }
- if ($value['type'] == 5) {
- $value['content'] = null;
- $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' => $user['username'],
- 'avatar' => $user['avatar'],
- 'follow_status' => $this->getFollowStatus($userInfo['uid'], $value['relate_id']),
- ];
- } else {
- unset($data[$key]);
- }
- }
- }
- }
- return $data;
- }
- public function postDetail($postInfo, $follow_uid, $userInfo)
- {
- Log::debug('feed流内容--' . json_encode($postInfo));
- $uid = $userInfo['uid'];
- $isFollow = 0;
- Log::debug("内容feed关注uid{$uid}followUid{$follow_uid}");
- $followStatus = $this->getFollowStatus($uid, $follow_uid);
- if ($followStatus) {
- $isFollow = $followStatus;
- }
- $user = $this->userInfo($postInfo['uid']);
- return [
- 'id' => (int)$postInfo['id'],
- 'type' => $postInfo['type'],
- 'created_at' => Carbon::parse($postInfo['created_at'])->diffForHumans(),
- 'uid' => (int)$postInfo['uid'],
- 'username' => $user['username'],
- 'avatar' => $user['avatar'],
- 'topic' => $this->getTopic($postInfo['topic_ids']),
- 'is_fine' => $postInfo['is_fine'],
- 'title' => $postInfo['title'],
- 'content' => $postInfo['content'],
- 'location' => $postInfo['location'],
- 'img' => $postInfo['img'],
- 'imgs' => $postInfo['imgs'],
- 'video' => $postInfo['video'],
- 'pv' => getNumber($postInfo['pv']),
- 'praise_count' => $postInfo['praise_count'],
- 'comment_count' => $postInfo['comment_count'],
- 'available_bean' => $postInfo['available_bean'],
- 'will_collect_bean' => $postInfo['will_collect_bean'],
- 'post_comment' => $this->getNewComment($postInfo['id'], $uid),
- 'is_like' => Redis::SISMEMBER('post_like_' . $postInfo['id'], $uid),
- 'is_dislike' => Redis::SISMEMBER('post_unlike_' . $postInfo['id'], $uid),
- 'is_collect' => Redis::SISMEMBER('post_collect_' . $postInfo['id'], $uid),
- 'follow_status' => $isFollow,
- 'h5url' => config('customer.share_post_h5url') . "?post_id={$postInfo['id']}&invite_code={$userInfo['invite_code']}",
- 'desc_url' => $postInfo['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $postInfo['id'] : '',
- ];
- }
- /**
- * 取消关注删除对应feed
- * @param $data
- */
- public function contentFeedDelete($data)
- {
- $number = substr($data['uid'], -1);
- try {
- DB::table('feed_' . $number)->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;
- }
- }
- }
|