This commit is contained in:
2026-08-05 11:06:20 +08:00
parent b4e5e98353
commit 8380b8797b
4 changed files with 24 additions and 24 deletions

View File

@@ -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);
}
// 其他错误交给系统处理

View File

@@ -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('删除失败', []);
}
}

View File

@@ -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,

View File

@@ -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('退出成功');
}
}