修复编译错误

This commit is contained in:
北枳 2025-07-05 22:00:36 +08:00
parent 13dea8d7ec
commit dc2e9ff656

View File

@ -13,10 +13,10 @@ export const apiRequest = async (
const token = getToken();
// 构建请求头
const headers: HeadersInit = {
const headers: Record<string, string> = {
'Accept': 'application/json',
'Content-Type': 'application/json',
...options.headers,
...(options.headers as Record<string, string>),
};
// 添加token到请求头如果存在
@ -102,11 +102,13 @@ export const apiDelete = (endpoint: string, options: RequestInit = {}) => {
/**
*
*/
export const apiUpload = (endpoint: string, formData: FormData, options: RequestInit = {}) => {
export const apiUpload = async (endpoint: string, formData: FormData, options: RequestInit = {}) => {
const token = getToken();
const headers: HeadersInit = {
...options.headers,
// 构建请求头文件上传时不设置Content-Type让浏览器自动设置
const headers: Record<string, string> = {
'Accept': 'application/json',
...(options.headers as Record<string, string>),
};
// 添加token到请求头如果存在
@ -114,10 +116,37 @@ export const apiUpload = (endpoint: string, formData: FormData, options: Request
headers['X-EASE-ADMIN-TOKEN'] = token;
}
return apiRequest(endpoint, {
...options,
method: 'POST',
headers,
body: formData,
});
try {
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
...options,
method: 'POST',
headers,
body: formData,
});
// 检查响应状态
if (!response.ok) {
if (response.status === 401) {
// Token过期或无效
clearAuthData();
window.location.href = '/login';
throw new Error('身份验证失败,请重新登录');
}
throw new Error(`请求失败: ${response.status}`);
}
const data = await response.json();
// 检查业务状态码
if (data.code === '401' || data.status === 401) {
clearAuthData();
window.location.href = '/login';
throw new Error('身份验证失败,请重新登录');
}
return data;
} catch (error) {
console.error('API request failed:', error);
throw error;
}
};