From 986066017cc83403e55bac73797f279f62472b32 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Tue, 23 Dec 2025 17:14:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=AF=84=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PostCommentController.php | 2 +- app/common/service/PostCommentService.php | 43 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/common/controller/PostCommentController.php b/app/common/controller/PostCommentController.php index 9e248a1..383feaa 100644 --- a/app/common/controller/PostCommentController.php +++ b/app/common/controller/PostCommentController.php @@ -99,7 +99,7 @@ class PostCommentController extends BaseController public function delete(Request $request) { try { - $count = $this->service->serviceDelete($request); + $count = $this->service->serviceDelete($request,'mini'); return $this->success([], "删除评论成功"); } catch (\Throwable $e) { return $this->error($e->getMessage().$e->getLine().$e->getFile()); diff --git a/app/common/service/PostCommentService.php b/app/common/service/PostCommentService.php index 8087436..a5e3fca 100644 --- a/app/common/service/PostCommentService.php +++ b/app/common/service/PostCommentService.php @@ -139,7 +139,36 @@ class PostCommentService /** * 添加后置处理 */ - public function serviceDelete(Request $request) + public function serviceDelete(Request $request,$scene = 'admin') + { + $param = $request->all(); + + if (empty($param['id'])) { + throw new \Exception("参数错误"); + } + + $where = []; + $where['id'] = $param['id']; + $comment = PostComment::where($where)->first(); + + $post = Post::where(['id' => $comment['post_id']])->first(); + + if($scene == 'mini' && ($comment['user_id'] != $request->user->uid || $post['user_id'] == $request->user->uid)){ + throw new \Exception("只能删除本人评论"); + } + $post['comment'] = $post['comment']-1; + $post->save(); + + $count = $comment->delete(); + + return $count; + } + + + /** + * 删除前置处理 + */ + public function beforeServiceDestroy($data, Request $request) { $param = $request->all(); @@ -155,8 +184,16 @@ class PostCommentService $post['comment'] = $post['comment']-1; $post->save(); - $count = $comment->delete(); + return $data; + } - return $count; + /** 删除 */ + public function serviceDestroy(Request $request, string $scene = 'destroy') + { + $data = $request->all(); + $this->preProcess(ucfirst($scene), $data, $request); + $config = $this->buildQueryConfig($request, $scene); + $deleted = $this->logicDeleteBy($config); + return $this->postProcess(ucfirst($scene), $deleted); } } \ No newline at end of file