|
@@ -14,16 +14,259 @@ use App\Models\Post;
|
|
|
use App\Models\PostCollect;
|
|
|
use App\Models\PostComment;
|
|
|
use App\Models\PostData;
|
|
|
+use App\Models\PostImgs;
|
|
|
use App\Models\PostLike;
|
|
|
+use App\Models\Topic;
|
|
|
+use App\Service\DetectionService;
|
|
|
+use App\Service\RabbitMqUtil;
|
|
|
+use App\Traits\PostTrait;
|
|
|
+use App\Traits\UserTrait;
|
|
|
+use Illuminate\Database\QueryException;
|
|
|
+use Illuminate\Support\Carbon;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class PostRepositories
|
|
|
{
|
|
|
- public function __construct(Post $post,PostComment $postComment)
|
|
|
+ use UserTrait;
|
|
|
+ use PostTrait;
|
|
|
+ public function __construct(Post $post,
|
|
|
+ PostData $postData,
|
|
|
+ PostImgs $postImgs,
|
|
|
+ PostComment $postComment,
|
|
|
+ DetectionService $detectionService,
|
|
|
+ RabbitMqUtil $rabbitMqUtil,
|
|
|
+ Topic $topic)
|
|
|
{
|
|
|
$this->post = $post;
|
|
|
+ $this->postData = $postData;
|
|
|
+ $this->postImgs = $postImgs;
|
|
|
$this->postComment = $postComment;
|
|
|
+ $this->detectionService = $detectionService;
|
|
|
+ $this->rabbitMqUtil = $rabbitMqUtil;
|
|
|
+ $this->topic = $topic;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发布内容
|
|
|
+ */
|
|
|
+ public function create($request)
|
|
|
+ {
|
|
|
+ //验证小号
|
|
|
+ $userInfo = $this->getUserInfo();
|
|
|
+ if (empty($userInfo)) {
|
|
|
+ Log::info('获取用户信息失败');
|
|
|
+ return jsonError('获取用户信息失败');
|
|
|
+ }
|
|
|
+ $isValid = 0;
|
|
|
+ if($userInfo['strength']){
|
|
|
+ $isValid = 1;
|
|
|
+ }
|
|
|
+ $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
|
|
|
+ $oneHourPostCount = $this->post->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
|
|
|
+ if($oneHourPostCount > 5){
|
|
|
+ return jsonError('创作欲望太强啦,休息一下,看看其他用户的内容吧!');
|
|
|
+ }
|
|
|
+
|
|
|
+ $detectionText = $request['title'] .','. $request['content'];
|
|
|
+ $detectionTextResult = $this->detectionService->checkText($detectionText);
|
|
|
+ if ($detectionTextResult['code']<0) {
|
|
|
+ return jsonError('内容违规,请修正哦');
|
|
|
+ }
|
|
|
+
|
|
|
+ $topicIds = json_decode($request['topic_ids'], true);
|
|
|
+ $topicCount = count($topicIds);
|
|
|
+ if($topicCount == 0 || $topicCount > 5){
|
|
|
+ return jsonError('所选话题必须1-5个');
|
|
|
+ }
|
|
|
+ //验证话题
|
|
|
+ $hasTopicCount = $this->topic->whereIn('id', $topicIds)->count();
|
|
|
+ if($topicCount != $hasTopicCount){
|
|
|
+ Log::error('所选话题非法'.$request['topic_ids']);
|
|
|
+ return jsonError('所选话题非法');
|
|
|
+ }
|
|
|
+ $imgs = [];
|
|
|
+ if($request['type'] == 'image'){
|
|
|
+ $imgs = json_decode($request['imgs'], true);
|
|
|
+ $imgCount = count($imgs);
|
|
|
+ if($imgCount == 0 || $imgCount > 9){
|
|
|
+ return jsonError('所传图集必须1-9个');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $allImg = array_merge($imgs, [$request['img']]);
|
|
|
+ $detectionImageResult = $this->detectionService->checkImg($allImg);
|
|
|
+ if ($detectionImageResult['code']<0) {
|
|
|
+ return jsonError('图片违规,请修正哦');
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'uid' => $userInfo['uid'],
|
|
|
+ 'username' => $userInfo['username'],
|
|
|
+ 'mobile' => $userInfo['mobile'],
|
|
|
+ 'avatar' => $userInfo['avatar']??'',
|
|
|
+ 'type' => $request['type'],
|
|
|
+ 'img' => $request['img'],
|
|
|
+ 'video' => isset($request['video'])? $request['video'] : '',
|
|
|
+ 'topic_ids' => implode(',', $topicIds),
|
|
|
+ 'title' => isset($request['title'])? $request['title'] : '',
|
|
|
+ 'content' => $request['content'],
|
|
|
+ 'location' => isset($request['location'])? $request['location'] : '',
|
|
|
+ 'is_suggest' => 0,
|
|
|
+ 'is_hide' => 0
|
|
|
+ ];
|
|
|
+
|
|
|
+ $date = date('Y-m-d H:i:s');
|
|
|
+
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ try{
|
|
|
+ $post = $this->post->create($data);
|
|
|
+
|
|
|
+ $this->postData->create([
|
|
|
+ 'post_id' => $post->id,
|
|
|
+ 'pv' => 0,
|
|
|
+ 'pv_real' => 0,
|
|
|
+ 'dislike_count' => 0,
|
|
|
+ 'praise_count' => 0,
|
|
|
+ 'praise_real_count' => 0,
|
|
|
+ 'share_count' => 0,
|
|
|
+ 'share_real_count' => 0,
|
|
|
+ 'comment_count' => 0,
|
|
|
+ 'collect_count' => 0,
|
|
|
+ 'collect_real_count' => 0,
|
|
|
+ 'available_bean' => $this->availableBean(),
|
|
|
+ 'will_collect_bean' => rand(100, 200),
|
|
|
+ 'collect_bean' => 0,
|
|
|
+ 'weight' => 0
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if($imgs){
|
|
|
+ $imgData = [];
|
|
|
+ foreach($imgs as $img){
|
|
|
+ $imgData[] = [
|
|
|
+ 'post_id' => $post->id,
|
|
|
+ 'img' => $img,
|
|
|
+ 'created_at' => $date,
|
|
|
+ 'updated_at' => $date
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $this->postImgs->insert($imgData);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ Redis::zadd('post_trigger_type', $isValid, $post->id);
|
|
|
+ return jsonSuccess();
|
|
|
+
|
|
|
+ }catch (QueryException $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ Log::debug('发布内容失败:'.$exception->getMessage());
|
|
|
+ return jsonError('发布内容失败,请重试');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 评论&回复
|
|
|
+ */
|
|
|
+ public function comment($request)
|
|
|
+ {
|
|
|
+ //验证小号
|
|
|
+ $userInfo = $this->getUserInfo();
|
|
|
+ if (empty($userInfo)) {
|
|
|
+ Log::info('获取用户信息失败');
|
|
|
+ return jsonError('获取用户信息失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
|
|
|
+ $oneHourCommentCount = $this->postComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
|
|
|
+ if($oneHourCommentCount > 59){
|
|
|
+ return jsonError('回复了这么多,休息休息,喝口水吧!');
|
|
|
+ }
|
|
|
+
|
|
|
+ $detectionTextResult = $this->detectionService->checkText($request['content']);
|
|
|
+ if ($detectionTextResult['code']<0) {
|
|
|
+ return jsonError('内容违规,请修正哦');
|
|
|
+ }
|
|
|
+
|
|
|
+ $post = $this->post->find($request['post_id']);
|
|
|
+ if(!$post){
|
|
|
+ return jsonError('获取内容信息失败');
|
|
|
+ }
|
|
|
+ $data = [
|
|
|
+ 'uid' => $userInfo['uid'],
|
|
|
+ 'post_id' => $request['post_id'],
|
|
|
+ 'parent_id' => 0,
|
|
|
+ 'username' => $userInfo['username'],
|
|
|
+ 'reply_uid' => 0,
|
|
|
+ 'reply_username' => '',
|
|
|
+ 'avatar' => $userInfo['avatar']??'',
|
|
|
+ 'content' => $request['content'],
|
|
|
+ 'is_delete' => 0,
|
|
|
+ ];
|
|
|
+ if(isset($request['parent_id']) && $request['parent_id'] != 0){
|
|
|
+ $comment = $this->postComment->find($request['parent_id']);
|
|
|
+ if(!$comment || $comment->post_id != $post->id){
|
|
|
+ return jsonError('获取评论信息失败');
|
|
|
+ }
|
|
|
+ if($comment->parent_id){
|
|
|
+ return jsonError('只能回复评论');
|
|
|
+ }
|
|
|
+ $data['parent_id'] = $request['parent_id'];
|
|
|
+
|
|
|
+ if(isset($request['reply_uid']) && isset($request['reply_username'])){
|
|
|
+ $data['reply_uid'] = $request['reply_uid'];
|
|
|
+ $data['reply_username'] = $request['reply_username'];
|
|
|
+ $this->rabbitMqUtil->push('add_message', [
|
|
|
+ 'uid' => $request['reply_uid'],
|
|
|
+ 'message_show_type' => 'post_reply_main',
|
|
|
+ 'param' => [
|
|
|
+ 'uid' => $userInfo['uid'],
|
|
|
+ 'username' => $userInfo['username'],
|
|
|
+ 'post_id' => $post->id,
|
|
|
+ 'content' => subtext($request['content'], 20),
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ }else{
|
|
|
+ $data['reply_uid'] = 0;
|
|
|
+ $data['reply_username'] = '';
|
|
|
+ $this->rabbitMqUtil->push('add_message', [
|
|
|
+ 'uid' => $comment->uid,
|
|
|
+ 'message_show_type' => 'post_reply',
|
|
|
+ 'param' => [
|
|
|
+ 'uid' => $userInfo['uid'],
|
|
|
+ 'username' => $userInfo['username'],
|
|
|
+ 'post_id' => $post->id,
|
|
|
+ 'content' => subtext($request['content'], 20),
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $this->rabbitMqUtil->push('add_message', [
|
|
|
+ 'uid' => $post->uid,
|
|
|
+ 'message_show_type' => 'post_comment',
|
|
|
+ 'param' => [
|
|
|
+ 'uid' => $userInfo['uid'],
|
|
|
+ 'username' => $userInfo['username'],
|
|
|
+ 'post_id' => $post->id,
|
|
|
+ 'content' => subtext($request['content'], 20),
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ try{
|
|
|
+ $this->postComment->create($data);
|
|
|
+ $post->data->comment_count += 1;
|
|
|
+ $post->data->save();
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return jsonSuccess();
|
|
|
+
|
|
|
+ }catch (QueryException $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ Log::debug('评论内容失败:'.$exception->getMessage());
|
|
|
+ return jsonError('评论内容失败,请重试');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -46,6 +289,17 @@ class PostRepositories
|
|
|
->paginate($perPage);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 内容详情
|
|
|
+ */
|
|
|
+ public function detail($id)
|
|
|
+ {
|
|
|
+ return $this->post
|
|
|
+ ->join('post_data', 'post_data.post_id', '=', 'post.id')
|
|
|
+ ->select('post.*')
|
|
|
+ ->find($id);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 推荐内容列表
|
|
|
*/
|