FeedRepositories.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // 不记录自己的其他行为到自己的关注列表,除了发布
  75. if ($fan == $request['target_id'] && $feedType != 6) {
  76. continue;
  77. }
  78. $number = substr($fan, -1);
  79. $data['uid'] = $fan;
  80. $data['follow_uid'] = $request['target_id'];
  81. $data['follow_username'] = $request['target_username'];
  82. $data['follow_avatar'] = isset($request['target_avatar']) ? $request['target_avatar'] : '';
  83. $data['type'] = $feedType;
  84. if (in_array($feedType, [1, 2, 3])) {
  85. $data['relate_id'] = $request['post_id'];
  86. $content['post_desc'] = $request['post_desc'];
  87. $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
  88. $content['post_type'] = $request['post_type'];
  89. } elseif ($feedType == 4) {//评论
  90. $data['relate_id'] = $request['post_id'];
  91. $content['post_desc'] = $request['post_desc'];
  92. $content['comment_id'] = $request['comment_id'];
  93. $content['comment_desc'] = $request['comment_content'];
  94. $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
  95. $content['post_type'] = $request['post_type'];
  96. } elseif ($feedType == 6) {//发布
  97. $data['relate_id'] = $request['post_id'];
  98. $content['post_desc'] = $request['post_desc'];
  99. $content['beans'] = isset($request['rewards']['bean']) ? intval($request['rewards']['bean']) : 0;
  100. $content['post_type'] = $request['post_type'];
  101. } elseif ($feedType == 5) {//关注
  102. $data['relate_id'] = $request['focus_uid'];
  103. $content = [];
  104. }
  105. $data['content'] = json_encode($content);
  106. $data['created_at'] = Carbon::now();
  107. $data['updated_at'] = Carbon::now();
  108. Log::debug('创建Feed流-data:' . json_encode($data));
  109. //目前只有评论可以重复出现在别人的feed流中,其他类型如果出现过一次不再产生新的feed信息
  110. if (in_array($feedType, [4])) {
  111. DB::table('feed_' . $number)->insert($data);
  112. } else {
  113. //此处需要优化,如果feed表需要清理历史数据怎么处理
  114. //以下判断可以控制virus的行为,重复不添加feed,但是关注用户不行,关注用户不存在is_existing字段
  115. //if (isset($request['is_existing']) && intval($request['is_existing']) == 0) {
  116. // DB::table('feed_'.$number)->insert($data);
  117. //}
  118. $feedRow = DB::table('feed_' . $number)->where('uid', $data['uid'])
  119. ->where('follow_uid', $data['follow_uid'])
  120. ->where('type', $data['type'])
  121. ->where('relate_id', $data['relate_id'])
  122. ->first();
  123. if (empty($feedRow)) {
  124. DB::table('feed_' . $number)->insert($data);
  125. }
  126. }
  127. }
  128. }
  129. return true;
  130. }
  131. //我的feed
  132. public function myFeed($request)
  133. {
  134. $userInfo = $this->getUserInfo();
  135. if (empty($userInfo)) {
  136. $userInfo['uid'] = 0;
  137. $blacklist = [];
  138. } else {
  139. $blacklist = Redis::smembers('blacklist_' . $userInfo['uid']);
  140. }
  141. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  142. $where[] = ['uid', $userInfo['uid']];
  143. $number = substr($userInfo['uid'], -1);
  144. $table_name = 'feed_' . $number;
  145. $feedModel = new Feed();
  146. $feedModel->setTable($table_name);
  147. $data = $feedModel->where(function ($query) use ($blacklist) {
  148. if ($blacklist) {
  149. $query->whereNotIn('follow_uid', $blacklist);
  150. }
  151. })->where($where)
  152. ->whereIn('type', [4, 5, 6])//目前只展示发布,评论 关注
  153. ->orderBy('id', 'desc')
  154. ->paginate($perPage);
  155. if ($data) {
  156. foreach ($data as $key => &$value) {
  157. if ($value['type'] == 6) {
  158. $post = $this->getPostInfo($value['relate_id'], 1);
  159. if ($post) {
  160. $value['relate_data'] = $this->postDetail($post, $value['follow_uid'], $userInfo);
  161. } else {
  162. unset($data[$key]);
  163. }
  164. }
  165. if ($value['type'] == 5) {
  166. $value['content'] = null;
  167. $user = $this->userInfo($value['relate_id']);
  168. if ($user) {
  169. Log::debug("测试feed关注状态:uid{$userInfo['uid']}followUid{$value['relate_id']}");
  170. $value['relate_data'] = [
  171. 'uid' => intval($user['uid']),
  172. 'username' => $user['username'],
  173. 'avatar' => $user['avatar'],
  174. 'follow_status' => $this->getFollowStatus($userInfo['uid'], $value['relate_id']),
  175. ];
  176. } else {
  177. unset($data[$key]);
  178. }
  179. }
  180. }
  181. }
  182. return $data;
  183. }
  184. public function postDetail($postInfo, $follow_uid, $userInfo)
  185. {
  186. Log::debug('feed流内容--' . json_encode($postInfo));
  187. $uid = $userInfo['uid'];
  188. $isFollow = 0;
  189. Log::debug("内容feed关注uid{$uid}followUid{$follow_uid}");
  190. $followStatus = $this->getFollowStatus($uid, $follow_uid);
  191. if ($followStatus) {
  192. $isFollow = $followStatus;
  193. }
  194. $user = $this->userInfo($postInfo['uid']);
  195. return [
  196. 'id' => (int)$postInfo['id'],
  197. 'type' => $postInfo['type'],
  198. 'created_at' => Carbon::parse($postInfo['created_at'])->diffForHumans(),
  199. 'uid' => (int)$postInfo['uid'],
  200. 'username' => $user['username'],
  201. 'avatar' => $user['avatar'],
  202. 'topic' => $this->getTopic($postInfo['topic_ids']),
  203. 'is_fine' => $postInfo['is_fine'],
  204. 'title' => $postInfo['title'],
  205. 'content' => $postInfo['content'],
  206. 'location' => $postInfo['location'],
  207. 'img' => $postInfo['img'],
  208. 'imgs' => $postInfo['imgs'],
  209. 'video' => $postInfo['video'],
  210. 'pv' => getNumber($postInfo['pv']),
  211. 'praise_count' => $postInfo['praise_count'],
  212. 'comment_count' => $postInfo['comment_count'],
  213. 'available_bean' => $postInfo['available_bean'],
  214. 'will_collect_bean' => $postInfo['will_collect_bean'],
  215. 'post_comment' => $this->getNewComment($postInfo['id'], $uid),
  216. 'is_like' => Redis::SISMEMBER('post_like_' . $postInfo['id'], $uid),
  217. 'is_dislike' => Redis::SISMEMBER('post_unlike_' . $postInfo['id'], $uid),
  218. 'is_collect' => Redis::SISMEMBER('post_collect_' . $postInfo['id'], $uid),
  219. 'follow_status' => $isFollow,
  220. 'h5url' => config('customer.share_post_h5url') . "?post_id={$postInfo['id']}&invite_code={$userInfo['invite_code']}",
  221. 'desc_url' => $postInfo['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $postInfo['id'] : '',
  222. ];
  223. }
  224. /**
  225. * 取消关注删除对应feed
  226. * @param $data
  227. */
  228. public function contentFeedDelete($data)
  229. {
  230. $number = substr($data['uid'], -1);
  231. try {
  232. DB::table('feed_' . $number)->where('uid', $data['uid'])->where('follow_uid', $data['follow_uid'])->delete();
  233. Log::debug("取消关注删除对应feed成功uid{$data['uid']}followUid{$data['follow_uid']}");
  234. } catch (\Exception $exception) {
  235. Log::error('取消关注删除对应feed失败' . json_encode($data) . $exception->getMessage());
  236. return false;
  237. }
  238. }
  239. }