wzq 5 rokov pred
rodič
commit
fe2a7d4d6f

+ 16 - 0
app/Http/Controllers/V1/PostController.php

@@ -54,6 +54,22 @@ class PostController extends Controller
         return  $this->postRepositories->create($request->all());
     }
 
+    /**
+     * 评论&回复
+     */
+    public function comment(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'post_id' => 'required|integer',
+            'content' => 'required|string|max:150',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+
+        return  $this->postRepositories->comment($request->all());
+    }
+
     /**
      * 内容列表
      */

+ 109 - 0
app/Repositories/PostRepositories.php

@@ -18,6 +18,7 @@ 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;
@@ -34,6 +35,7 @@ class PostRepositories
                                 PostImgs $postImgs,
                                 PostComment $postComment,
                                 DetectionService $detectionService,
+                                RabbitMqUtil $rabbitMqUtil,
                                 Topic $topic)
     {
         $this->post = $post;
@@ -41,6 +43,7 @@ class PostRepositories
         $this->postImgs = $postImgs;
         $this->postComment = $postComment;
         $this->detectionService = $detectionService;
+        $this->rabbitMqUtil = $rabbitMqUtil;
         $this->topic = $topic;
     }
 
@@ -151,6 +154,112 @@ class PostRepositories
         }
     }
 
+    /**
+     * 评论&回复
+     */
+    public function comment($request)
+    {
+        //验证小号
+        $userInfo = $this->getUserInfo();
+        if (empty($userInfo)) {
+            Log::info('获取用户信息失败');
+            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){
+                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'] = 0;
+                $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'],
+                    ]
+                ]);
+            }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),
+                    ]
+                ]);
+            }
+            $this->rabbitMqUtil->push('add_message', [
+                'uid' => $comment->uid,
+                'message_show_type' => 'post_reply_main',
+                '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('评论内容失败,请重试');
+        }
+    }
+
     /**
      * 内容列表
      */

+ 1 - 0
routes/api.php

@@ -23,6 +23,7 @@ $api->version('v1', [
         $api->post('post', 'PostController@create');
         $api->get('post', 'PostController@index');
         $api->get('post/suggest', 'PostController@suggestPost');
+        $api->post('post/comment', 'PostController@comment');
         $api->get('post/comment', 'PostController@commentList');
         $api->get('post/reply', 'PostController@replyList');
         $api->get('topicCategory', 'CategoryController@index');