feat: 新增 API 服务,提供 HTTP 请求、认证刷新和错误处理功能

This commit is contained in:
2026-01-26 15:43:18 +08:00
parent 22e9478389
commit 5d4d477d07

View File

@@ -4,7 +4,10 @@
* Validates: Requirements 12.3, 12.4 * 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 // Token storage keys
const ACCESS_TOKEN_KEY = 'access_token'; const ACCESS_TOKEN_KEY = 'access_token';
@@ -52,7 +55,15 @@ function buildUrl(
endpoint: string, endpoint: string,
params?: Record<string, string | number | boolean | undefined> params?: Record<string, string | number | boolean | undefined>
): string { ): 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) { if (params) {
Object.entries(params).forEach(([key, value]) => { Object.entries(params).forEach(([key, value]) => {
if (value !== undefined) { if (value !== undefined) {