PostRepositories.php 9.5 KB

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