diff --git a/src/components/settings/DebugSection.tsx b/src/components/settings/DebugSection.tsx index 2e0ac203..c923cbca 100644 --- a/src/components/settings/DebugSection.tsx +++ b/src/components/settings/DebugSection.tsx @@ -139,14 +139,20 @@ export function DebugSection() { } }; - const webServerAddress = (() => { - if (!webServerPort) return null; - if (allowLanAccess) { - const host = isTauri() ? lanIp || 'localhost' : window.location.hostname; - return `http://${host}:${webServerPort}`; - } - return `http://localhost:${webServerPort}`; - })(); +const webServerAddress = (() => { + + if (window.location.host && !isTauri()) { + return window.location.origin; + } + + // Tauri 桌面端直连后端 + if (!webServerPort) return null; + + const host = allowLanAccess + ? (lanIp || 'localhost') + : 'localhost'; + return `http://${host}:${webServerPort}`; +})(); const handleOpenWebServer = useCallback(async () => { if (!webServerAddress) return; diff --git a/src/services/wsService.ts b/src/services/wsService.ts index fd4168e1..5588cf14 100644 --- a/src/services/wsService.ts +++ b/src/services/wsService.ts @@ -91,12 +91,16 @@ export function setServerPort(port: number): void { function getWsUrl(): string { const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - if (serverPort) { - const hostname = window.location.hostname; - return `${protocol}//${hostname}:${serverPort}/api/ws`; + + // 正常情况:通过 Nginx 反向代理 + if (window.location.host) { + return `${protocol}//${window.location.host}/api/ws`; } - const host = window.location.host; - return `${protocol}//${host}/api/ws`; + + // Fallback:直连后端 + const hostname = window.location.hostname || '127.0.0.1'; + const port = serverPort || 12701; + return `${protocol}//${hostname}:${port}/api/ws`; } // ============================================================================ diff --git a/src/utils/backendApi.ts b/src/utils/backendApi.ts index 80c58e35..077d6a83 100644 --- a/src/utils/backendApi.ts +++ b/src/utils/backendApi.ts @@ -16,11 +16,16 @@ export function setBackendPort(port: number): void { } export function getApiBase(): string { - if (backendPort) { - const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:'; - return `${protocol}//${window.location.hostname}:${backendPort}/api`; + // 正常情况:通过 Nginx 反向代理,用相对路径 + if (window.location.host) { + return '/api'; } - return '/api'; + + // Fallback:直连后端 + const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:'; + const hostname = window.location.hostname || '127.0.0.1'; + const port = backendPort || 12701; + return `${protocol}//${hostname}:${port}/api`; } /** 安全解析 JSON 响应,204 / 空 body 时返回 undefined */