123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?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 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;
- 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) {
- $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;
- }
- $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($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 = $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($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;
- }
- $user = $this->userInfo($post['uid']);
- return [
- 'id' => $post['id'],
- 'type' => $post['type'],
- 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
- 'uid' => $post['uid'],
- 'username' => $user['username'],
- 'avatar' => $user['avatar'],
- 'topic' => $topic,
- 'title' => $post['title'],
- 'content' => $post['content'],
- 'location' => $post['location'],
- 'img' => $post['img'],
- 'imgs' => $imgs,
- 'video' => $post['video'],
- 'pv' => getNumber($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', '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);
- $user = $this->userInfo($comment->uid);
- $comment->username = $user['username'];
- }
- return $comments;
- }
- /**
- * 取消关注删除对应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;
- }
- }
- }
|