PostRepositories.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. 'content' => subtext($request['content'], 20),
  212. ]
  213. ]);
  214. }else{
  215. $data['reply_uid'] = 0;
  216. $data['reply_username'] = '';
  217. $this->rabbitMqUtil->push('add_message', [
  218. 'uid' => $comment->uid,
  219. 'message_show_type' => 'post_reply',
  220. 'param' => [
  221. 'uid' => $userInfo['uid'],
  222. 'username' => $userInfo['username'],
  223. 'post_id' => $post->id,
  224. 'content' => subtext($request['content'], 20),
  225. ]
  226. ]);
  227. }
  228. }else{
  229. $this->rabbitMqUtil->push('add_message', [
  230. 'uid' => $post->uid,
  231. 'message_show_type' => 'post_comment',
  232. 'param' => [
  233. 'uid' => $userInfo['uid'],
  234. 'username' => $userInfo['username'],
  235. 'post_id' => $post->id,
  236. 'content' => subtext($request['content'], 20),
  237. ]
  238. ]);
  239. }
  240. DB::beginTransaction();
  241. try{
  242. $this->postComment->create($data);
  243. $post->data->comment_count += 1;
  244. $post->data->save();
  245. DB::commit();
  246. return jsonSuccess();
  247. }catch (QueryException $exception){
  248. DB::rollBack();
  249. Log::debug('评论内容失败:'.$exception->getMessage());
  250. return jsonError('评论内容失败,请重试');
  251. }
  252. }
  253. /**
  254. * 内容列表
  255. */
  256. public function lists($request)
  257. {
  258. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  259. return $this->post
  260. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  261. ->select('post.*')
  262. ->where(function($query) use ($request){
  263. if(isset($request['keyword'])){
  264. $query->where('title', 'like', "%{$request['keyword']}%")
  265. ->orWhere('content', 'like', "%{$request['keyword']}%");
  266. }
  267. })
  268. ->where(function($query) use ($request){
  269. if(isset($request['topic_ids'])){
  270. $topicIds = json_decode($request['topic_ids'], true);
  271. foreach ($topicIds as $key=>$id) {
  272. if ($key==0) {
  273. $query->whereRaw('FIND_IN_SET('.$id.', post.topic_ids)');
  274. } else {
  275. $query->orWhereRaw('FIND_IN_SET('.$id.', post.topic_ids)');
  276. }
  277. }
  278. }
  279. })
  280. ->orderBy('weight','desc')
  281. ->paginate($perPage);
  282. }
  283. /**
  284. * 视频列表
  285. */
  286. public function video($request)
  287. {
  288. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  289. $where = [];
  290. if(isset($request['id'])){
  291. $where[] = ['post.id', '<>', $request['id']];
  292. }
  293. $where[] = ['type', 'video'];
  294. return $this->post
  295. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  296. ->select('post.*')
  297. ->where($where)
  298. ->orderBy('weight','desc')
  299. ->paginate($perPage);
  300. }
  301. /**
  302. * 个人中心内容列表
  303. */
  304. public function MyPost($type, $uid)
  305. {
  306. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  307. $where = [];
  308. if($type == 'create'){
  309. $where[] = ['post.uid', $uid];
  310. $post = $this->post;
  311. $order = 'post.id';
  312. }elseif($type == 'collect'){
  313. $post = $this->post->join('post_collect', 'post_collect.post_id', '=', 'post.id');
  314. $where[] = ['post_collect.uid', $uid];
  315. $order = 'post_collect.id';
  316. }else{
  317. $post = $this->post->join('post_share', 'post_share.post_id', '=', 'post.id');
  318. $where[] = ['post_share.uid', $uid];
  319. $order = 'post_share.updated_at';
  320. }
  321. return $post
  322. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  323. ->select('post.*')
  324. ->where($where)
  325. ->orderBy($order,'desc')
  326. ->paginate($perPage);
  327. }
  328. /**
  329. * 内容详情
  330. */
  331. public function detail($id)
  332. {
  333. return $this->post
  334. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  335. ->select('post.*')
  336. ->find($id);
  337. }
  338. /**
  339. * 推荐内容列表
  340. */
  341. public function suggestPost($request)
  342. {
  343. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  344. return $this->post
  345. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  346. ->select('post.*')
  347. ->orderBy('weight','desc')
  348. ->paginate($perPage);
  349. }
  350. /**
  351. * 话题内容
  352. */
  353. public function topicPost($request)
  354. {
  355. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  356. return $this->post
  357. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  358. ->select('post.*')
  359. ->whereRaw('FIND_IN_SET(' . $request['id'] . ',post.topic_ids)')
  360. ->orderBy('id','desc')
  361. ->paginate($perPage);
  362. }
  363. /**
  364. * 评论列表
  365. */
  366. public function commentList($request)
  367. {
  368. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  369. return $this->postComment
  370. ->where('post_id', $request['post_id'])
  371. ->where('parent_id', 0)
  372. ->orderBy('id','desc')
  373. ->paginate($perPage);
  374. }
  375. /**
  376. * 回复列表
  377. */
  378. public function replyList($request)
  379. {
  380. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  381. return $this->postComment
  382. ->where('parent_id', $request['id'])
  383. ->orderBy('id','desc')
  384. ->paginate($perPage);
  385. }
  386. /**
  387. * 话题列表
  388. */
  389. public function topicList($request)
  390. {
  391. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  392. $where = [];
  393. if(isset($request['category_id'])){
  394. $topic = $this->topic->join('category_topic', 'category_topic.topic_id', '=', 'topic.id')->select('topic.*');
  395. $where[] = ['category_topic.category_id', $request['category_id']];
  396. }else{
  397. $topic = $this->topic;
  398. }
  399. if(isset($request['is_suggest'])){
  400. $where[] = ['topic.is_suggest', $request['is_suggest']];
  401. }
  402. return $topic
  403. ->where($where)
  404. ->orderBy('id','desc')
  405. ->paginate($perPage);
  406. }
  407. /**
  408. * 更新帖子统计数量
  409. * @param $request
  410. * @return mixed
  411. */
  412. public function updatePostData($request)
  413. {
  414. $postId = $request['post_id'];
  415. $post = PostData::where('post_id', $postId)->first();
  416. if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
  417. $post->pv += 1;
  418. $post->pv_real += 1;
  419. Log::debug("帖子:".$postId."被阅读,pv +1");
  420. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
  421. $post->dislike += 1;
  422. Log::debug("帖子:".$postId."被不喜欢,unlike +1");
  423. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
  424. if($request['behavior_value']){
  425. $post->praise_count += 1;
  426. $post->praise_real_count += 1;
  427. PostLike::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  428. Log::debug("帖子:".$postId."被点赞,praise_count +1");
  429. }else{
  430. $post->praise_count -= 1;
  431. $post->praise_real_count -= 1;
  432. PostLike::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
  433. Log::debug("帖子:".$postId."被取消点赞,praise_count -1");
  434. }
  435. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
  436. $post->share_count += 1;
  437. $post->share_real_count += 1;
  438. $shareRow = PostShare::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->first();
  439. if($shareRow){
  440. PostShare::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->update(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  441. }else{
  442. PostShare::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  443. }
  444. Log::debug("帖子:".$postId."被分享,share_count +1");
  445. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
  446. $post->comment_count += 1;
  447. Log::debug("帖子:".$postId."被评论,comment_count +1");
  448. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
  449. if($request['behavior_value']) {
  450. $post->collect_count += 1;
  451. $post->collect_real_count += 1;
  452. PostCollect::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
  453. Log::debug("帖子:".$postId."被收藏,collect_count +1");
  454. }else{
  455. $post->collect_count -= 1;
  456. $post->collect_real_count -= 1;
  457. PostCollect::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
  458. Log::debug("帖子:".$postId."被取消收藏,collect_count -1");
  459. }
  460. }
  461. $this->collectPostId($request['post_id']);
  462. return $post->save();
  463. }
  464. /**
  465. * 收集所有有操作的帖子,存入redis
  466. * 供后续计算帖子权重
  467. * @param $id
  468. */
  469. public function collectPostId($id)
  470. {
  471. $key = "community_calc_post_score";
  472. Redis::sadd($key,$id);
  473. Log::debug('存入帖子'.$id.'到权重列表');
  474. }
  475. /**
  476. * 话题详情
  477. */
  478. public function topicDetail($id)
  479. {
  480. return $this->topic
  481. ->find($id);
  482. }
  483. }