PostRepositories.php 14 KB

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