FeedRepositories.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: edz
  5. * Date: 2019-06-10
  6. * Time: 17:53
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Behavior;
  10. use App\Models\Feed;
  11. use App\Models\PostComment;
  12. use App\Traits\PostTrait;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. use Tymon\JWTAuth\Facades\JWTAuth;
  18. use App\Models\PostDislike;
  19. use App\Models\PostLike;
  20. use App\Models\PostCollect;
  21. use App\Traits\UserTrait;
  22. class FeedRepositories
  23. {
  24. use UserTrait;
  25. use PostTrait;
  26. public function __construct(PostRepositories $postRepositories, PostComment $postComment)
  27. {
  28. $this->postRepositories = $postRepositories;
  29. $this->postComment = $postComment;
  30. }
  31. public function getFeedType($action)
  32. {
  33. $type = [
  34. 'like' => 1,
  35. 'forward' => 2,
  36. 'collect' => 3,
  37. 'comment' => 4,
  38. 'focus' => 5,
  39. 'publish' => 6
  40. ];
  41. return isset($type[$action]) ? $type[$action] : 0;
  42. }
  43. /**
  44. * 创建feed流
  45. * 更新帖子统计数
  46. * @param $request
  47. */
  48. public function contentCreate($request)
  49. {
  50. $this->feedCreate($request);
  51. //关注操作不需要调用
  52. if ($request['behavior_flag'] != 'focus') {
  53. $this->postRepositories->updatePostData($request);
  54. }
  55. }
  56. /**
  57. * 创建feed流内容
  58. * @param $request
  59. */
  60. public function feedCreate($request)
  61. {
  62. Log::debug('创建Feed流-请求参数:' . json_encode($request));
  63. $fans = isset($request['fans']) ? $request['fans'] : [];
  64. if (empty($fans)) {//没有粉丝,不用插入
  65. Log::debug('创建Feed流-没有粉丝,不用创建相关feed流');
  66. return true;
  67. }
  68. $behaviorFlag = isset($request['behavior_flag']) ? $request['behavior_flag'] : '';
  69. $feedType = $this->getFeedType($behaviorFlag);
  70. Log::debug('创建Feed流-feed类型:' . $feedType);
  71. if ($feedType) {
  72. $data = [];
  73. foreach ($fans as $fan) {
  74. $number = substr($fan, -1);
  75. $data['uid'] = $fan;
  76. $data['follow_uid'] = $request['target_id'];
  77. $data['follow_username'] = $request['target_username'];
  78. $data['follow_avatar'] = isset($request['target_avatar']) ? $request['target_avatar'] : '';
  79. $data['type'] = $feedType;
  80. if (in_array($feedType, [1, 2, 3])) {
  81. $data['relate_id'] = $request['post_id'];
  82. $content['post_desc'] = $request['post_desc'];
  83. $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
  84. $content['post_type'] = $request['post_type'];
  85. } elseif ($feedType == 4) {//评论
  86. $data['relate_id'] = $request['post_id'];
  87. $content['post_desc'] = $request['post_desc'];
  88. $content['comment_id'] = $request['comment_id'];
  89. $content['comment_desc'] = $request['comment_content'];
  90. $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
  91. $content['post_type'] = $request['post_type'];
  92. } elseif ($feedType == 6) {//发布
  93. $data['relate_id'] = $request['post_id'];
  94. $content['post_desc'] = $request['post_desc'];
  95. $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
  96. $content['post_type'] = $request['post_type'];
  97. } elseif ($feedType == 5) {//关注
  98. $data['relate_id'] = $request['focus_uid'];
  99. $content = [];
  100. }
  101. $data['content'] = json_encode($content);
  102. $data['created_at'] = Carbon::now();
  103. $data['updated_at'] = Carbon::now();
  104. Log::debug('创建Feed流-data:' . json_encode($data));
  105. //目前只有评论可以重复出现在别人的feed流中,其他类型如果出现过一次不再产生新的feed信息
  106. if (in_array($feedType, [4])) {
  107. DB::table('feed_'.$number)->insert($data);
  108. } else {
  109. //此处需要优化,如果feed表需要清理历史数据怎么处理
  110. //以下判断可以控制virus的行为,重复不添加feed,但是关注用户不行,关注用户不存在is_existing字段
  111. //if (isset($request['is_existing']) && intval($request['is_existing']) == 0) {
  112. // DB::table('feed_'.$number)->insert($data);
  113. //}
  114. $feedRow = DB::table('feed_'.$number)->where('uid',$data['uid'])
  115. ->where('follow_uid',$data['follow_uid'])
  116. ->where('type',$data['type'])
  117. ->where('relate_id',$data['relate_id'])
  118. ->first();
  119. if(empty($feedRow)){
  120. DB::table('feed_'.$number)->insert($data);
  121. }
  122. }
  123. }
  124. }
  125. return true;
  126. }
  127. //我的feed
  128. public function myFeed($request)
  129. {
  130. $userInfo = $this->getUserInfo();
  131. if (empty($userInfo)) {
  132. $userInfo['uid'] = 0;
  133. }
  134. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  135. $where[] = ['uid', $userInfo['uid']];
  136. $number = substr($userInfo['uid'], -1);
  137. $table_name = 'feed_'.$number;
  138. $feedModel = new Feed();
  139. $feedModel->setTable($table_name);
  140. $data = $feedModel
  141. ->where($where)
  142. ->orderBy('id', 'desc')
  143. ->paginate($perPage);
  144. if ($data) {
  145. foreach ($data as $key => &$value) {
  146. if ($value['type'] == 6) {
  147. $post = $this->getPostInfo($value['relate_id'], 1);
  148. if ($post) {
  149. $value['relate_data'] = $this->postDetail($post, $value['follow_uid'], $userInfo);
  150. } else {
  151. unset($data[$key]);
  152. }
  153. }
  154. if ($value['type'] == 5) {
  155. $value['content'] = null;
  156. $user = $this->userInfo($value['relate_id']);
  157. if ($user) {
  158. Log::debug("测试feed关注状态:uid{$userInfo['uid']}followUid{$value['relate_id']}");
  159. $value['relate_data'] = [
  160. 'uid' => intval($user['uid']),
  161. 'username' => $user['username'],
  162. 'avatar' => $user['avatar'],
  163. 'follow_status' => $this->getFollowStatus($userInfo['uid'], $value['relate_id']),
  164. ];
  165. } else {
  166. unset($data[$key]);
  167. }
  168. }
  169. }
  170. }
  171. return $data;
  172. }
  173. public function postDetail($postInfo, $follow_uid, $userInfo)
  174. {
  175. Log::debug('feed流内容--' . json_encode($postInfo));
  176. $uid = $userInfo['uid'];
  177. $isFollow = 0;
  178. Log::debug("内容feed关注uid{$uid}followUid{$follow_uid}");
  179. $followStatus = $this->getFollowStatus($uid, $follow_uid);
  180. if ($followStatus) {
  181. $isFollow = $followStatus;
  182. }
  183. $user = $this->userInfo($postInfo['uid']);
  184. return [
  185. 'id' => (int) $postInfo['id'],
  186. 'type' => $postInfo['type'],
  187. 'created_at' => Carbon::parse($postInfo['created_at'])->diffForHumans(),
  188. 'uid' => (int) $postInfo['uid'],
  189. 'username' => $user['username'],
  190. 'avatar' => $user['avatar'],
  191. 'topic' => $this->getTopic($postInfo['topic_ids']),
  192. 'title' => $postInfo['title'],
  193. 'content' => $postInfo['content'],
  194. 'location' => $postInfo['location'],
  195. 'img' => $postInfo['img'],
  196. 'imgs' => $postInfo['imgs'],
  197. 'video' => $postInfo['video'],
  198. 'pv' => getNumber($postInfo['pv']),
  199. 'praise_count' => $postInfo['praise_count'],
  200. 'comment_count' => $postInfo['comment_count'],
  201. 'available_bean' => $postInfo['available_bean'],
  202. 'will_collect_bean' => $postInfo['will_collect_bean'],
  203. 'post_comment' => $this->getNewComment($postInfo['id']),
  204. 'is_like' => Redis::SISMEMBER('post_like_'.$postInfo['id'], $uid),
  205. 'is_dislike' => Redis::SISMEMBER('post_unlike_'.$postInfo['id'], $uid),
  206. 'is_collect' => Redis::SISMEMBER('post_collect_'.$postInfo['id'], $uid),
  207. 'follow_status' => $isFollow,
  208. 'h5url' => config('customer.share_post_h5url') . "?post_id={$postInfo['id']}&invite_code={$userInfo['invite_code']}",
  209. 'desc_url' => $postInfo['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $postInfo['id'] : '',
  210. ];
  211. }
  212. /**
  213. * 取消关注删除对应feed
  214. * @param $data
  215. */
  216. public function contentFeedDelete($data)
  217. {
  218. $number = substr($data['uid'], -1);
  219. try {
  220. DB::table('feed_'.$number)->where('uid', $data['uid'])->where('follow_uid', $data['follow_uid'])->delete();
  221. Log::debug("取消关注删除对应feed成功uid{$data['uid']}followUid{$data['follow_uid']}");
  222. } catch (\Exception $exception) {
  223. Log::error('取消关注删除对应feed失败' . json_encode($data) . $exception->getMessage());
  224. return false;
  225. }
  226. }
  227. }