FeedRepositories.php 10 KB

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