PostRepositories.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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\Post;
  11. use App\Models\PostCollect;
  12. use App\Models\PostComment;
  13. use App\Models\PostData;
  14. use App\Models\PostImgs;
  15. use App\Models\PostLike;
  16. use App\Models\Topic;
  17. use App\Service\DetectionService;
  18. use App\Service\RabbitMqUtil;
  19. use App\Traits\PostTrait;
  20. use App\Traits\UserTrait;
  21. use Illuminate\Database\QueryException;
  22. use Illuminate\Support\Carbon;
  23. use Illuminate\Support\Facades\Log;
  24. use Illuminate\Support\Facades\Redis;
  25. use Illuminate\Support\Facades\DB;
  26. class PostRepositories
  27. {
  28. use UserTrait;
  29. use PostTrait;
  30. public function __construct(Post $post,
  31. PostData $postData,
  32. PostImgs $postImgs,
  33. PostComment $postComment,
  34. DetectionService $detectionService,
  35. RabbitMqUtil $rabbitMqUtil,
  36. Topic $topic)
  37. {
  38. $this->post = $post;
  39. $this->postData = $postData;
  40. $this->postImgs = $postImgs;
  41. $this->postComment = $postComment;
  42. $this->detectionService = $detectionService;
  43. $this->rabbitMqUtil = $rabbitMqUtil;
  44. $this->topic = $topic;
  45. }
  46. /**
  47. * 发布内容
  48. */
  49. public function create($request)
  50. {
  51. //验证小号
  52. $userInfo = $this->getUserInfo();
  53. if (empty($userInfo)) {
  54. Log::info('获取用户信息失败');
  55. return jsonError('获取用户信息失败');
  56. }
  57. $isValid = 0;
  58. if($userInfo['strength']){
  59. $isValid = 1;
  60. }
  61. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  62. $oneHourPostCount = $this->post->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  63. if($oneHourPostCount > 5){
  64. return jsonError('创作欲望太强啦,休息一下,看看其他用户的内容吧!');
  65. }
  66. $detectionText = $request['title'] .','. $request['content'];
  67. $detectionTextResult = $this->detectionService->checkText($detectionText);
  68. if ($detectionTextResult['code']<0) {
  69. return jsonError('内容违规,请修正哦');
  70. }
  71. $topicIds = json_decode($request['topic_ids'], true);
  72. $topicCount = count($topicIds);
  73. if($topicCount == 0 || $topicCount > 5){
  74. return jsonError('所选话题必须1-5个');
  75. }
  76. //验证话题
  77. $hasTopicCount = $this->topic->whereIn('id', $topicIds)->count();
  78. if($topicCount != $hasTopicCount){
  79. Log::error('所选话题非法'.$request['topic_ids']);
  80. return jsonError('所选话题非法');
  81. }
  82. $imgs = [];
  83. if($request['type'] == 'image'){
  84. $imgs = json_decode($request['imgs'], true);
  85. $imgCount = count($imgs);
  86. if($imgCount == 0 || $imgCount > 9){
  87. return jsonError('所传图集必须1-9个');
  88. }
  89. }
  90. $allImg = array_merge($imgs, [$request['img']]);
  91. $detectionImageResult = $this->detectionService->checkImg($allImg);
  92. if ($detectionImageResult['code']<0) {
  93. return jsonError('图片违规,请修正哦');
  94. }
  95. $data = [
  96. 'uid' => $userInfo['uid'],
  97. 'username' => $userInfo['username'],
  98. 'mobile' => $userInfo['mobile'],
  99. 'avatar' => $userInfo['avatar']??'',
  100. 'type' => $request['type'],
  101. 'img' => $request['img'],
  102. 'video' => isset($request['video'])? $request['video'] : '',
  103. 'topic_ids' => implode(',', $topicIds),
  104. 'title' => isset($request['title'])? $request['title'] : '',
  105. 'content' => $request['content'],
  106. 'location' => isset($request['location'])? $request['location'] : '',
  107. 'is_suggest' => 0,
  108. 'is_hide' => 0
  109. ];
  110. $date = date('Y-m-d H:i:s');
  111. DB::beginTransaction();
  112. try{
  113. $post = $this->post->create($data);
  114. $this->postData->create([
  115. 'post_id' => $post->id,
  116. 'pv' => 0,
  117. 'pv_real' => 0,
  118. 'dislike_count' => 0,
  119. 'praise_count' => 0,
  120. 'praise_real_count' => 0,
  121. 'share_count' => 0,
  122. 'share_real_count' => 0,
  123. 'comment_count' => 0,
  124. 'collect_count' => 0,
  125. 'collect_real_count' => 0,
  126. 'available_bean' => $this->availableBean(),
  127. 'will_collect_bean' => rand(100, 200),
  128. 'collect_bean' => 0,
  129. 'weight' => 0
  130. ]);
  131. if($imgs){
  132. $imgData = [];
  133. foreach($imgs as $img){
  134. $imgData[] = [
  135. 'post_id' => $post->id,
  136. 'img' => $img,
  137. 'created_at' => $date,
  138. 'updated_at' => $date
  139. ];
  140. }
  141. $this->postImgs->insert($imgData);
  142. }
  143. DB::commit();
  144. Redis::zadd('post_trigger_type', $isValid, $post->id);
  145. return jsonSuccess();
  146. }catch (QueryException $exception){
  147. DB::rollBack();
  148. Log::debug('发布内容失败:'.$exception->getMessage());
  149. return jsonError('发布内容失败,请重试');
  150. }
  151. }
  152. /**
  153. * 评论&回复
  154. */
  155. public function comment($request)
  156. {
  157. //验证小号
  158. $userInfo = $this->getUserInfo();
  159. if (empty($userInfo)) {
  160. Log::info('获取用户信息失败');
  161. return jsonError('获取用户信息失败');
  162. }
  163. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  164. $oneHourCommentCount = $this->postComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  165. if($oneHourCommentCount > 59){
  166. return jsonError('回复了这么多,休息休息,喝口水吧!');
  167. }
  168. $detectionTextResult = $this->detectionService->checkText($request['content']);
  169. if ($detectionTextResult['code']<0) {
  170. return jsonError('内容违规,请修正哦');
  171. }
  172. $post = $this->post->find($request['post_id']);
  173. if(!$post){
  174. return jsonError('获取内容信息失败');
  175. }
  176. $data = [
  177. 'uid' => $userInfo['uid'],
  178. 'post_id' => $request['post_id'],
  179. 'parent_id' => 0,
  180. 'username' => $userInfo['username'],
  181. 'reply_uid' => 0,
  182. 'reply_username' => '',
  183. 'avatar' => $userInfo['avatar']??'',
  184. 'content' => $request['content'],
  185. 'is_delete' => 0,
  186. ];
  187. if(isset($request['parent_id']) && $request['parent_id'] != 0){
  188. $comment = $this->postComment->find($request['parent_id']);
  189. if(!$comment || $comment->post_id != $post->id){
  190. return jsonError('获取评论信息失败');
  191. }
  192. if($comment->parent_id){
  193. return jsonError('只能回复评论');
  194. }
  195. $data['parent_id'] = $request['parent_id'];
  196. if(isset($request['reply_uid']) && isset($request['reply_username'])){
  197. $data['reply_uid'] = $request['reply_uid'];
  198. $data['reply_username'] = $request['reply_username'];
  199. $this->rabbitMqUtil->push('add_message', [
  200. 'uid' => $request['reply_uid'],
  201. 'message_show_type' => 'post_reply_main',
  202. 'param' => [
  203. 'uid' => $userInfo['uid'],
  204. 'username' => $userInfo['username'],
  205. 'post_id' => $post->id,
  206. 'content' => subtext($request['content'], 20),
  207. ]
  208. ]);
  209. }else{
  210. $data['reply_uid'] = 0;
  211. $data['reply_username'] = '';
  212. $this->rabbitMqUtil->push('add_message', [
  213. 'uid' => $comment->uid,
  214. 'message_show_type' => 'post_reply',
  215. 'param' => [
  216. 'uid' => $userInfo['uid'],
  217. 'username' => $userInfo['username'],
  218. 'post_id' => $post->id,
  219. 'content' => subtext($request['content'], 20),
  220. ]
  221. ]);
  222. }
  223. }else{
  224. $this->rabbitMqUtil->push('add_message', [
  225. 'uid' => $post->uid,
  226. 'message_show_type' => 'post_comment',
  227. 'param' => [
  228. 'uid' => $userInfo['uid'],
  229. 'username' => $userInfo['username'],
  230. 'post_id' => $post->id,
  231. 'content' => subtext($request['content'], 20),
  232. ]
  233. ]);
  234. }
  235. DB::beginTransaction();
  236. try{
  237. $this->postComment->create($data);
  238. $post->data->comment_count += 1;
  239. $post->data->save();
  240. DB::commit();
  241. return jsonSuccess();
  242. }catch (QueryException $exception){
  243. DB::rollBack();
  244. Log::debug('评论内容失败:'.$exception->getMessage());
  245. return jsonError('评论内容失败,请重试');
  246. }
  247. }
  248. /**
  249. * 内容列表
  250. */
  251. public function lists($request)
  252. {
  253. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  254. return $this->post
  255. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  256. ->select('post.*')
  257. ->where(function($query) use ($request){
  258. if(isset($request['keyword'])){
  259. $query->where('title', 'like', "%{$request['keyword']}%")
  260. ->orWhere('content', 'like', "%{$request['keyword']}%");
  261. }
  262. })
  263. ->orderBy('weight','desc')
  264. ->paginate($perPage);
  265. }
  266. /**
  267. * 内容详情
  268. */
  269. public function detail($id)
  270. {
  271. return $this->post
  272. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  273. ->select('post.*')
  274. ->find($id);
  275. }
  276. /**
  277. * 推荐内容列表
  278. */
  279. public function suggestPost($request)
  280. {
  281. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  282. return $this->post
  283. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  284. ->select('post.*')
  285. ->orderBy('weight','desc')
  286. ->paginate($perPage);
  287. }
  288. /**
  289. * 评论列表
  290. */
  291. public function commentList($request)
  292. {
  293. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  294. return $this->postComment
  295. ->where('post_id', $request['post_id'])
  296. ->where('parent_id', 0)
  297. ->orderBy('id','desc')
  298. ->paginate($perPage);
  299. }
  300. /**
  301. * 回复列表
  302. */
  303. public function replyList($request)
  304. {
  305. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  306. return $this->postComment
  307. ->where('parent_id', $request['id'])
  308. ->orderBy('id','desc')
  309. ->paginate($perPage);
  310. }
  311. /**
  312. * 更新帖子统计数量
  313. * @param $request
  314. * @return mixed
  315. */
  316. public function updatePostData($request)
  317. {
  318. $postId = $request['post_id'];
  319. $post = PostData::where('post_id', $postId)->first();
  320. if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
  321. $post->pv += 1;
  322. $post->pv_real += 1;
  323. Log::debug("帖子:".$postId."被阅读,pv +1");
  324. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
  325. $post->dislike += 1;
  326. Log::debug("帖子:".$postId."被不喜欢,unlike +1");
  327. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
  328. if($request['behavior_value']){
  329. $post->praise_count += 1;
  330. $post->praise_real_count += 1;
  331. PostLike::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  332. Log::debug("帖子:".$postId."被点赞,praise_count +1");
  333. }else{
  334. $post->praise_count -= 1;
  335. $post->praise_real_count -= 1;
  336. PostLike::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
  337. Log::debug("帖子:".$postId."被取消点赞,praise_count -1");
  338. }
  339. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
  340. $post->share_count += 1;
  341. $post->share_real_count += 1;
  342. Log::debug("帖子:".$postId."被分享,share_count +1");
  343. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
  344. $post->comment_count += 1;
  345. Log::debug("帖子:".$postId."被评论,comment_count +1");
  346. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
  347. if($request['behavior_value']) {
  348. $post->collect_count += 1;
  349. $post->collect_real_count += 1;
  350. PostCollect::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  351. Log::debug("帖子:".$postId."被收藏,collect_count +1");
  352. }else{
  353. $post->collect_count -= 1;
  354. $post->collect_real_count -= 1;
  355. PostCollect::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
  356. Log::debug("帖子:".$postId."被取消收藏,collect_count -1");
  357. }
  358. }
  359. $this->collectPostId($request['post_id']);
  360. return $post->save();
  361. }
  362. /**
  363. * 收集所有有操作的帖子,存入redis
  364. * 供后续计算帖子权重
  365. * @param $id
  366. */
  367. public function collectPostId($id)
  368. {
  369. $key = "community_calc_post_score";
  370. Redis::sadd($key,$id);
  371. Log::debug('存入帖子'.$id.'到权重列表');
  372. }
  373. /**
  374. * 话题详情
  375. */
  376. public function topicDetail($id)
  377. {
  378. return $this->topic
  379. ->find($id);
  380. }
  381. }