删除评论

This commit is contained in:
2025-12-23 17:14:13 +08:00
parent 204ea2a68e
commit 986066017c
2 changed files with 41 additions and 4 deletions
@@ -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());
+40 -3
View File
@@ -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);
}
}