1
This commit is contained in:
2025-11-26 16:17:20 +08:00
parent de6a3a8a04
commit 45cb399c17

View File

@ -8,19 +8,29 @@ const apiRequest = async <T>(
endpoint: string,
data: Record<string, any>
): Promise<{ success: boolean; data: T[]; message: string }> => {
const response = await fetch(`${API_CONFIG.BASE_URL}${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 15000);
try {
const response = await fetch(`${API_CONFIG.BASE_URL}${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
signal: controller.signal,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
} catch (e: any) {
if (e.name === 'AbortError') {
throw new Error('请求超时');
}
throw e;
} finally {
clearTimeout(timeout);
}
return response.json();
};
/**