diff --git a/src/services/api.ts b/src/services/api.ts index de360b6..a25b0ef 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -4,7 +4,10 @@ * Validates: Requirements 12.3, 12.4 */ -const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:2612/api/v1'; +const isLocal = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'; +const API_BASE_URL = isLocal + ? (import.meta.env.VITE_API_BASE_URL || 'http://localhost:2612/api/v1') + : '/api/v1'; // Token storage keys const ACCESS_TOKEN_KEY = 'access_token'; @@ -52,7 +55,15 @@ function buildUrl( endpoint: string, params?: Record ): string { - const url = new URL(`${API_BASE_URL}${endpoint}`); + // 处理相对路径问题 + let fullUrlString; + if (API_BASE_URL.startsWith('/')) { + fullUrlString = `${window.location.origin}${API_BASE_URL}${endpoint}`; + } else { + fullUrlString = `${API_BASE_URL}${endpoint}`; + } + + const url = new URL(fullUrlString); if (params) { Object.entries(params).forEach(([key, value]) => { if (value !== undefined) {