PostRepositories.php 13 KB

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