This commit is contained in:
2026-08-05 10:31:38 +08:00
parent 83c0865272
commit b4e5e98353
3 changed files with 20 additions and 20 deletions

View File

@@ -56,17 +56,17 @@ class ExceptionHandle extends Handle
{ {
// 验证器异常 // 验证器异常
if ($e instanceof ValidateException) { if ($e instanceof ValidateException) {
return $this->error($e->getError(), 422); // 验证器没通过 return $this->error(422, $e->getError()); // 验证器没通过
}; };
// 请求异常 // 请求异常
if ($e instanceof HttpException) { if ($e instanceof HttpException) {
return $this->error($e->getMessage(), $e->getStatusCode()); return $this->error($e->getStatusCode(), $e->getMessage());
}; };
// 处理【自定义业务异常】(在 Service 层主动 throw 的错误) // 处理【自定义业务异常】(在 Service 层主动 throw 的错误)
if ($e instanceof \think\Exception) { if ($e instanceof \think\Exception) {
return $this->error($e->getMessage(), $e->getCode() ?: 400); return $this->error($e->getCode() ?: 400, $e->getMessage());
} }
// 其他错误交给系统处理 // 其他错误交给系统处理

View File

@@ -29,7 +29,7 @@ class Ticket extends BaseController
$result = $ticketService->getTicketList($params); $result = $ticketService->getTicketList($params);
return $this->success('查询成功', $result); return $this->success(200, '查询成功', $result);
} }
@@ -42,9 +42,9 @@ class Ticket extends BaseController
try { try {
$result = $ticketService->createTicket($data); $result = $ticketService->createTicket($data);
return $this->success('创建成功', $result); return $this->success(200, '创建成功', $result);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->error($e->getMessage()); return $this->error(400, $e->getMessage());
} }
} }
@@ -62,9 +62,9 @@ class Ticket extends BaseController
try { try {
$result = $ticketService->updateTicket($id, $data, $allowFields, $adminId, true); $result = $ticketService->updateTicket($id, $data, $allowFields, $adminId, true);
return $this->success('更新成功', $result); return $this->success(200, '更新成功', $result);
} catch (\Exception $e) { } 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) public function getTicketDashboardStats(TicketService $ticketService)
{ {
$result = $ticketService->getTicketDashboardStats(); $result = $ticketService->getTicketDashboardStats();
return $this->success('查询成功', $result); return $this->success(200, '查询成功', $result);
} }
/** /**
@@ -88,16 +88,16 @@ class Ticket extends BaseController
$ids = $data['ids'] ?? []; $ids = $data['ids'] ?? [];
if (!is_array($ids) || empty($ids)) { if (!is_array($ids) || empty($ids)) {
return $this->error('ids 必须是非空数组'); return $this->error(400, 'ids 必须是非空数组');
} }
$allowFields = ['status', 'lock_time']; $allowFields = ['status', 'lock_time'];
try { try {
$result = $ticketService->batchUpdateTickets($ids, $data, $allowFields, $adminId, true); $result = $ticketService->batchUpdateTickets($ids, $data, $allowFields, $adminId, true);
return $this->success('批量更新成功', $result); return $this->success(200, '批量更新成功', $result);
} catch (\Exception $e) { } 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) public function deleteTicket($id)
{ {
$ticket = Tickets::find($id); $ticket = Tickets::find($id);
if (!$ticket) return $this->error('工单不存在'); if (!$ticket) return $this->error(400, '工单不存在');
return $ticket->delete() return $ticket->delete()
? $this->success('删除成功', []) ? $this->success(200, '删除成功', [])
: $this->error('删除失败', []); : $this->error(400, '删除失败', []);
} }
} }

View File

@@ -14,9 +14,9 @@ trait ApiResponse
* @param int $code 自定义业务状态码 * @param int $code 自定义业务状态码
* @return Json * @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 额外的错误数据(如验证未通过的具体字段) * @param mixed $data 额外的错误数据(如验证未通过的具体字段)
* @return Json * @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 返回格式 * 统一 JSON 返回格式
*/ */
private function jsonResponse(string $message, mixed $data, int $code): Json private function jsonResponse(int $code, string $message, mixed $data): Json
{ {
$result = [ $result = [
'code' => $code, 'code' => $code,