FeedRepositories.php 10 KB

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