From 5d4d477d078a6352880482e63eb793ffe8a2b078 Mon Sep 17 00:00:00 2001 From: admin <1297598740@qq.com> Date: Mon, 26 Jan 2026 15:43:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20API=20=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=EF=BC=8C=E6=8F=90=E4=BE=9B=20HTTP=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E3=80=81=E8=AE=A4=E8=AF=81=E5=88=B7=E6=96=B0=E5=92=8C?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/api.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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) {