'application/json'] ): Response { return $this->result('success', $code, $httpStatus, $message, $data, $header); } /** * 返回错误响应 * * @param string $message 响应错误消息,默认“请求失败” * @param int $code 内部状态码,默认 1 表示错误 * @param int $httpStatus HTTP 状态码,默认 400 * @param mixed $data 可选的错误数据 * @param array $header 响应头,默认 JSON 格式 * @return Response */ public function error( string $message = '请求失败', int $code = 1, int $httpStatus = 400, $data = [], array $header = ['Content-Type' => 'application/json'] ): Response { return $this->result('error', $code, $httpStatus, $message, $data, $header); } /** * 统一响应格式方法 * * @param string $status 响应状态,例如 "success" 或 "error" * @param int $code 内部状态码 * @param int $httpStatus HTTP 状态码 * @param string $message 响应消息 * @param mixed $data 响应数据 * @param array $header 响应头 * @return Response */ protected function result( string $status, int $code, int $httpStatus, string $message, $data, array $header ): Response { $body = [ 'status' => $status, 'code' => $code, 'message' => $message, 'data' => $data, ]; return response(json_encode($body, JSON_UNESCAPED_UNICODE), $httpStatus, $header); } }