PostRepositories.php 19 KB

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