diff --git a/app/ExceptionHandle.php b/app/ExceptionHandle.php index 767b9b1..1ff62c4 100644 --- a/app/ExceptionHandle.php +++ b/app/ExceptionHandle.php @@ -56,17 +56,17 @@ class ExceptionHandle extends Handle { // 验证器异常 if ($e instanceof ValidateException) { - return $this->error($e->getError(), 422); // 验证器没通过 + return $this->error(422, $e->getError()); // 验证器没通过 }; // 请求异常 if ($e instanceof HttpException) { - return $this->error($e->getMessage(), $e->getStatusCode()); + return $this->error($e->getStatusCode(), $e->getMessage()); }; // 处理【自定义业务异常】(在 Service 层主动 throw 的错误) if ($e instanceof \think\Exception) { - return $this->error($e->getMessage(), $e->getCode() ?: 400); + return $this->error($e->getCode() ?: 400, $e->getMessage()); } // 其他错误交给系统处理 diff --git a/app/admin/controller/Ticket.php b/app/admin/controller/Ticket.php index fa3bb2b..34a1e44 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('查询成功', $result); + return $this->success(200, '查询成功', $result); } @@ -42,9 +42,9 @@ class Ticket extends BaseController try { $result = $ticketService->createTicket($data); - return $this->success('创建成功', $result); + return $this->success(200, '创建成功', $result); } catch (\Exception $e) { - return $this->error($e->getMessage()); + return $this->error(400, $e->getMessage()); } } @@ -62,9 +62,9 @@ class Ticket extends BaseController try { $result = $ticketService->updateTicket($id, $data, $allowFields, $adminId, true); - return $this->success('更新成功', $result); + return $this->success(200, '更新成功', $result); } catch (\Exception $e) { - return $this->error($e->getMessage()); + return $this->error(400, $e->getMessage()); } } @@ -75,7 +75,7 @@ class Ticket extends BaseController public function getTicketDashboardStats(TicketService $ticketService) { $result = $ticketService->getTicketDashboardStats(); - return $this->success('查询成功', $result); + return $this->success(200, '查询成功', $result); } /** @@ -88,16 +88,16 @@ class Ticket extends BaseController $ids = $data['ids'] ?? []; if (!is_array($ids) || empty($ids)) { - return $this->error('ids 必须是非空数组'); + return $this->error(400, 'ids 必须是非空数组'); } $allowFields = ['status', 'lock_time']; try { $result = $ticketService->batchUpdateTickets($ids, $data, $allowFields, $adminId, true); - return $this->success('批量更新成功', $result); + return $this->success(200, '批量更新成功', $result); } catch (\Exception $e) { - return $this->error($e->getMessage()); + return $this->error(400, $e->getMessage()); } } @@ -107,10 +107,10 @@ class Ticket extends BaseController public function deleteTicket($id) { $ticket = Tickets::find($id); - if (!$ticket) return $this->error('工单不存在'); + if (!$ticket) return $this->error(400, '工单不存在'); return $ticket->delete() - ? $this->success('删除成功', []) - : $this->error('删除失败', []); + ? $this->success(200, '删除成功', []) + : $this->error(400, '删除失败', []); } } diff --git a/app/common/Trait/ApiResponse.php b/app/common/Trait/ApiResponse.php index 62d2fc5..0a48ed7 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(string $message = '操作成功', mixed $data = [], int $code = 200): Json + protected function success(int $code = 200, string $message = '操作成功', mixed $data = []): Json { - return $this->jsonResponse($message, $data, $code); + return $this->jsonResponse($code, $message, $data); } /** @@ -26,15 +26,15 @@ trait ApiResponse * @param mixed $data 额外的错误数据(如验证未通过的具体字段) * @return Json */ - protected function error(string $message = '操作失败', mixed $data = [], int $code = 400): Json + protected function error(int $code = 400, string $message = '操作失败', mixed $data = []): Json { - return $this->jsonResponse($message, $data, $code); + return $this->jsonResponse($code, $message, $data); } /** * 统一 JSON 返回格式 */ - private function jsonResponse(string $message, mixed $data, int $code): Json + private function jsonResponse(int $code, string $message, mixed $data): Json { $result = [ 'code' => $code,