From 8380b8797b99c38912b07203e249f2c5a105508e Mon Sep 17 00:00:00 2001 From: GiveMePast <1248298477@qq.com> Date: Wed, 5 Aug 2026 11:06:20 +0800 Subject: [PATCH] wip --- app/ExceptionHandle.php | 6 +++--- app/admin/controller/Ticket.php | 24 ++++++++++++------------ app/common/Trait/ApiResponse.php | 10 +++++----- app/index/controller/Login.php | 8 ++++---- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/app/ExceptionHandle.php b/app/ExceptionHandle.php index 1ff62c4..858cbcd 100644 --- a/app/ExceptionHandle.php +++ b/app/ExceptionHandle.php @@ -56,17 +56,17 @@ class ExceptionHandle extends Handle { // 验证器异常 if ($e instanceof ValidateException) { - return $this->error(422, $e->getError()); // 验证器没通过 + return $this->error($e->getError(), [], 422); // 验证器没通过 }; // 请求异常 if ($e instanceof HttpException) { - return $this->error($e->getStatusCode(), $e->getMessage()); + return $this->error($e->getMessage(), [], $e->getStatusCode()); }; // 处理【自定义业务异常】(在 Service 层主动 throw 的错误) if ($e instanceof \think\Exception) { - return $this->error($e->getCode() ?: 400, $e->getMessage()); + return $this->error($e->getMessage(), [], $e->getCode() ?: 400); } // 其他错误交给系统处理 diff --git a/app/admin/controller/Ticket.php b/app/admin/controller/Ticket.php index 34a1e44..fa3bb2b 100644 --- a/app/admin/controller/Ticket.php +++ b/app/admin/controller/Ticket.php @@ -29,7 +29,7 @@ class Ticket extends BaseController $result = $ticketService->getTicketList($params); - return $this->success(200, '查询成功', $result); + return $this->success('查询成功', $result); } @@ -42,9 +42,9 @@ class Ticket extends BaseController try { $result = $ticketService->createTicket($data); - return $this->success(200, '创建成功', $result); + return $this->success('创建成功', $result); } catch (\Exception $e) { - return $this->error(400, $e->getMessage()); + return $this->error($e->getMessage()); } } @@ -62,9 +62,9 @@ class Ticket extends BaseController try { $result = $ticketService->updateTicket($id, $data, $allowFields, $adminId, true); - return $this->success(200, '更新成功', $result); + return $this->success('更新成功', $result); } catch (\Exception $e) { - return $this->error(400, $e->getMessage()); + return $this->error($e->getMessage()); } } @@ -75,7 +75,7 @@ class Ticket extends BaseController public function getTicketDashboardStats(TicketService $ticketService) { $result = $ticketService->getTicketDashboardStats(); - return $this->success(200, '查询成功', $result); + return $this->success('查询成功', $result); } /** @@ -88,16 +88,16 @@ class Ticket extends BaseController $ids = $data['ids'] ?? []; if (!is_array($ids) || empty($ids)) { - return $this->error(400, 'ids 必须是非空数组'); + return $this->error('ids 必须是非空数组'); } $allowFields = ['status', 'lock_time']; try { $result = $ticketService->batchUpdateTickets($ids, $data, $allowFields, $adminId, true); - return $this->success(200, '批量更新成功', $result); + return $this->success('批量更新成功', $result); } catch (\Exception $e) { - return $this->error(400, $e->getMessage()); + return $this->error($e->getMessage()); } } @@ -107,10 +107,10 @@ class Ticket extends BaseController public function deleteTicket($id) { $ticket = Tickets::find($id); - if (!$ticket) return $this->error(400, '工单不存在'); + if (!$ticket) return $this->error('工单不存在'); return $ticket->delete() - ? $this->success(200, '删除成功', []) - : $this->error(400, '删除失败', []); + ? $this->success('删除成功', []) + : $this->error('删除失败', []); } } diff --git a/app/common/Trait/ApiResponse.php b/app/common/Trait/ApiResponse.php index 0a48ed7..c3193d2 100644 --- a/app/common/Trait/ApiResponse.php +++ b/app/common/Trait/ApiResponse.php @@ -14,9 +14,9 @@ trait ApiResponse * @param int $code 自定义业务状态码 * @return Json */ - protected function success(int $code = 200, string $message = '操作成功', mixed $data = []): Json + protected function success(string $message = '操作成功', mixed $data = [], int $code = 200): Json { - return $this->jsonResponse($code, $message, $data); + return $this->jsonResponse($message, $data, $code); } /** @@ -26,15 +26,15 @@ trait ApiResponse * @param mixed $data 额外的错误数据(如验证未通过的具体字段) * @return Json */ - protected function error(int $code = 400, string $message = '操作失败', mixed $data = []): Json + protected function error(string $message = '操作失败', mixed $data = [], int $code = 400): Json { - return $this->jsonResponse($code, $message, $data); + return $this->jsonResponse($message, $data, $code); } /** * 统一 JSON 返回格式 */ - private function jsonResponse(int $code, string $message, mixed $data): Json + private function jsonResponse(string $message, mixed $data, int $code): Json { $result = [ 'code' => $code, diff --git a/app/index/controller/Login.php b/app/index/controller/Login.php index 8c61fb5..658d126 100644 --- a/app/index/controller/Login.php +++ b/app/index/controller/Login.php @@ -19,14 +19,14 @@ class Login extends BaseController // 验证数据 if (empty($params['username']) || empty($params['password'])) { - return $this->error(400, '用户名和密码不能为空'); + return $this->error('用户名和密码不能为空'); } try { $result = $authService->login($params['username'], $params['password']); - return $this->success(200, '登录成功', $result); + return $this->success('登录成功', $result); } catch (\Exception $e) { - return $this->error(400, $e->getMessage()); + return $this->error($e->getMessage()); } } @@ -36,6 +36,6 @@ class Login extends BaseController public function logout() { Session::clear(); - return $this->success(200, '退出成功'); + return $this->success('退出成功'); } }