diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8f30c5a3c..8c90d0d44 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -689,34 +689,43 @@ async fn test_api( let s = state.read().await; let base_url = format!("http://{}:{}", s.config.server.host, s.config.server.port); let api_key = &s.config.server.api_key; - - let client = reqwest::Client::new(); + + // 创建一个禁用代理的客户端 + let client = reqwest::Client::builder() + .no_proxy() + .build() + .map_err(|e| e.to_string())?; + let url = format!("{}{}", base_url, path); - + + tracing::info!("Testing API: {} {}", method, url); + let start = std::time::Instant::now(); - + let mut req = match method.as_str() { "GET" => client.get(&url), "POST" => client.post(&url), _ => return Err("Unsupported method".to_string()), }; - + req = req.header("Content-Type", "application/json"); - + if auth { req = req.header("Authorization", format!("Bearer {}", api_key)); } - + if let Some(b) = body { req = req.body(b); } - + match req.send().await { Ok(resp) => { let status = resp.status().as_u16(); let body = resp.text().await.unwrap_or_default(); let time_ms = start.elapsed().as_millis() as u64; - + + tracing::info!("API test result: status={}, body_len={}", status, body.len()); + Ok(TestResult { success: status >= 200 && status < 300, status, @@ -724,7 +733,10 @@ async fn test_api( time_ms, }) } - Err(e) => Err(e.to_string()), + Err(e) => { + tracing::error!("API test error: {}", e); + Err(e.to_string()) + } } } diff --git a/src/components/Dashboard.tsx b/src/components/Dashboard.tsx index 2a54b6344..02e9fe0a5 100644 --- a/src/components/Dashboard.tsx +++ b/src/components/Dashboard.tsx @@ -17,6 +17,7 @@ interface TestState { status: "idle" | "loading" | "success" | "error"; response?: string; time?: number; + httpStatus?: number; } export function Dashboard() { @@ -144,22 +145,27 @@ export function Dashboard() { endpoint.needsAuth // maps to 'auth' parameter ); + // 添加调试日志 + console.log(`Test ${endpoint.id}:`, result); + setTestResults((prev) => ({ ...prev, [endpoint.id]: { endpoint: endpoint.path, status: result.success ? "success" : "error", - response: result.body, + response: result.body || `HTTP ${result.status}: 无响应内容`, time: result.time_ms, + httpStatus: result.status, }, })); } catch (e: any) { + console.error(`Test ${endpoint.id} error:`, e); setTestResults((prev) => ({ ...prev, [endpoint.id]: { endpoint: endpoint.path, status: "error", - response: e.toString(), + response: `请求失败: ${e.toString()}`, }, })); } @@ -190,7 +196,9 @@ export function Dashboard() { }; const getStatusBadge = (result?: TestState) => { - if (!result || result.status === "idle") return null; + if (!result || result.status === "idle") { + return 未测试; + } if (result.status === "loading") { return 测试中...; } @@ -201,7 +209,11 @@ export function Dashboard() { ); } - return ✗ 失败; + return ( + + ✗ 失败 {result.httpStatus ? `(${result.httpStatus})` : ''} + + ); }; return ( @@ -281,7 +293,12 @@ export function Dashboard() { {/* API 测试 */}
+ 如遇问题,请打开浏览器开发者工具查看控制台日志 +
+