PostRepositories.php 15 KB

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