PostRepositories.php 19 KB

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