PostRepositories.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. $id = 0;
  294. if(isset($request['id'])){
  295. $id = $request['id'];
  296. }
  297. $where[] = ['type', 'video'];
  298. return $this->post
  299. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  300. ->select('post.*', DB::raw("IF (post.id = {$id},1,0) as sort"))
  301. ->where($where)
  302. ->orderBy('sort', 'desc')
  303. ->orderBy('weight','desc')
  304. ->paginate($perPage);
  305. }
  306. /**
  307. * 个人中心内容列表
  308. */
  309. public function MyPost($type, $uid)
  310. {
  311. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  312. $where = [];
  313. if($type == 'create'){
  314. $where[] = ['post.uid', $uid];
  315. $post = $this->post;
  316. $order = 'post.id';
  317. }elseif($type == 'collect'){
  318. $post = $this->post->join('post_collect', 'post_collect.post_id', '=', 'post.id');
  319. $where[] = ['post_collect.uid', $uid];
  320. $order = 'post_collect.id';
  321. }else{
  322. $post = $this->post->join('post_share', 'post_share.post_id', '=', 'post.id');
  323. $where[] = ['post_share.uid', $uid];
  324. $order = 'post_share.updated_at';
  325. }
  326. return $post
  327. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  328. ->select('post.*')
  329. ->where($where)
  330. ->orderBy($order,'desc')
  331. ->paginate($perPage);
  332. }
  333. /**
  334. * 内容详情
  335. */
  336. public function detail($id)
  337. {
  338. return $this->post
  339. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  340. ->select('post.*')
  341. ->find($id);
  342. }
  343. /**
  344. * 推荐内容列表
  345. */
  346. public function suggestPost($request)
  347. {
  348. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  349. return $this->post
  350. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  351. ->select('post.*')
  352. ->orderBy('weight','desc')
  353. ->paginate($perPage);
  354. }
  355. /**
  356. * 话题内容
  357. */
  358. public function topicPost($request)
  359. {
  360. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  361. return $this->post
  362. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  363. ->select('post.*')
  364. ->whereRaw('FIND_IN_SET(' . $request['id'] . ',post.topic_ids)')
  365. ->orderBy('id','desc')
  366. ->paginate($perPage);
  367. }
  368. /**
  369. * 评论列表
  370. */
  371. public function commentList($request)
  372. {
  373. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  374. return $this->postComment
  375. ->where('post_id', $request['post_id'])
  376. ->where('parent_id', 0)
  377. ->orderBy('id','desc')
  378. ->paginate($perPage);
  379. }
  380. /**
  381. * 回复列表
  382. */
  383. public function replyList($request)
  384. {
  385. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  386. return $this->postComment
  387. ->where('parent_id', $request['id'])
  388. ->orderBy('id','desc')
  389. ->paginate($perPage);
  390. }
  391. /**
  392. * 话题列表
  393. */
  394. public function topicList($request)
  395. {
  396. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  397. $where = [];
  398. if(isset($request['category_id'])){
  399. $topic = $this->topic->join('category_topic', 'category_topic.topic_id', '=', 'topic.id')->select('topic.*');
  400. $where[] = ['category_topic.category_id', $request['category_id']];
  401. }else{
  402. $topic = $this->topic;
  403. }
  404. if(isset($request['is_suggest'])){
  405. $where[] = ['topic.is_suggest', $request['is_suggest']];
  406. }
  407. return $topic
  408. ->where($where)
  409. ->orderBy('id','desc')
  410. ->paginate($perPage);
  411. }
  412. /**
  413. * 更新帖子统计数量
  414. * @param $request
  415. * @return mixed
  416. */
  417. public function updatePostData($request)
  418. {
  419. $postId = isset($request['post_id'])?$request['post_id']:0;
  420. if(empty($postId)){
  421. Log::debug("非帖子类操作,不操作帖子统计数量".json_encode($request));
  422. return true;
  423. }
  424. $post = PostData::where('post_id', $postId)->first();
  425. if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
  426. $post->pv += 1;
  427. $post->pv_real += 1;
  428. Log::debug("帖子:".$postId."被阅读,pv +1");
  429. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
  430. $post->dislike_count += 1;
  431. Log::debug("帖子:".$postId."被不喜欢,unlike +1");
  432. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
  433. if($request['behavior_value']){
  434. $post->praise_count += 1;
  435. $post->praise_real_count += 1;
  436. PostLike::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  437. Log::debug("帖子:".$postId."被点赞,praise_count +1");
  438. }else{
  439. $post->praise_count -= 1;
  440. $post->praise_real_count -= 1;
  441. PostLike::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
  442. Log::debug("帖子:".$postId."被取消点赞,praise_count -1");
  443. }
  444. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
  445. $post->share_count += 1;
  446. $post->share_real_count += 1;
  447. $shareRow = PostShare::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->first();
  448. if($shareRow){
  449. PostShare::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->update(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  450. }else{
  451. PostShare::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  452. }
  453. Log::debug("帖子:".$postId."被分享,share_count +1");
  454. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
  455. $post->comment_count += 1;
  456. Log::debug("帖子:".$postId."被评论,comment_count +1");
  457. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
  458. if($request['behavior_value']) {
  459. $post->collect_count += 1;
  460. $post->collect_real_count += 1;
  461. PostCollect::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  462. Log::debug("帖子:".$postId."被收藏,collect_count +1");
  463. }else{
  464. $post->collect_count -= 1;
  465. $post->collect_real_count -= 1;
  466. PostCollect::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
  467. Log::debug("帖子:".$postId."被取消收藏,collect_count -1");
  468. }
  469. }
  470. $this->collectPostId($request['post_id']);
  471. return $post->save();
  472. }
  473. /**
  474. * 收集所有有操作的帖子,存入redis
  475. * 供后续计算帖子权重
  476. * @param $id
  477. */
  478. public function collectPostId($id)
  479. {
  480. $key = "community_calc_post_score";
  481. Redis::sadd($key,$id);
  482. Log::debug('存入帖子'.$id.'到权重列表');
  483. }
  484. /**
  485. * 话题详情
  486. */
  487. public function topicDetail($id)
  488. {
  489. return $this->topic
  490. ->find($id);
  491. }
  492. /**
  493. * 获取话题
  494. */
  495. public function getTopic($ids)
  496. {
  497. $topics = $this->topic
  498. ->whereIn('id', explode(',',$ids))
  499. ->get();
  500. $data = [];
  501. foreach($topics as $topic){
  502. $data[] = [
  503. 'id' => $topic->id,
  504. 'name' => $topic->name,
  505. 'img' => $topic->img,
  506. 'follow_count' => $topic->follow->count() + 9876,
  507. ];
  508. }
  509. return $data;
  510. }
  511. /**
  512. * 获取内容视频组
  513. */
  514. public function getPostVideo($ids)
  515. {
  516. return $this->post
  517. ->select('id', 'img', 'uid', 'username', 'avatar')
  518. ->whereIn('id', explode(',',$ids))
  519. ->where('type', 'video')
  520. ->get();
  521. }
  522. }