mirror of
https://github.com/Zhuym07/Tsumugiboshi.git
synced 2025-05-20 13:57:28 +08:00
更新index.html,优化API调用函数,简化请求处理逻辑并增强错误处理
This commit is contained in:
parent
f923ed0695
commit
efc0c7cb50
35
index.html
35
index.html
@ -493,12 +493,11 @@
|
|||||||
localStorage.setItem('apiLogs', JSON.stringify(updatedLogs));
|
localStorage.setItem('apiLogs', JSON.stringify(updatedLogs));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleApiCall = async (endpoint, method = 'GET', formData = null, config = {}) => {
|
const handleApiCall = async (endpoint, method = 'GET', formData = null, config) => {
|
||||||
try {
|
try {
|
||||||
const options = {
|
const options = {
|
||||||
method,
|
method,
|
||||||
headers: {},
|
headers: { 'Content-Type': 'application/json' }
|
||||||
credentials: 'include', // 支持跨域请求携带 Cookie
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let url = `${apiBase}${endpoint}`;
|
let url = `${apiBase}${endpoint}`;
|
||||||
@ -507,37 +506,19 @@
|
|||||||
if (method === 'GET' && config.queryParams) {
|
if (method === 'GET' && config.queryParams) {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
config.queryParams.forEach(param => {
|
config.queryParams.forEach(param => {
|
||||||
if (formData && formData[param]) {
|
if (formData[param]) params.append(param, formData[param]);
|
||||||
params.append(param, formData[param]);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
url += `?${params.toString()}`;
|
url += `?${params.toString()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理请求体
|
// 处理POST请求体
|
||||||
if (['POST', 'PUT', 'PATCH'].includes(method)) {
|
if (method === 'POST') {
|
||||||
if (formData instanceof FormData) {
|
if (config.requestFormat === 'json') {
|
||||||
// 文件上传时,不设置 Content-Type,浏览器会自动处理
|
|
||||||
options.body = formData;
|
|
||||||
} else if (config.requestFormat === 'json') {
|
|
||||||
options.headers['Content-Type'] = 'application/json';
|
|
||||||
options.body = JSON.stringify(formData);
|
options.body = JSON.stringify(formData);
|
||||||
} else {
|
|
||||||
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
||||||
options.body = new URLSearchParams(formData).toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
const res = await fetch(url, options);
|
const res = await fetch(url, options);
|
||||||
|
|
||||||
// 检查 HTTP 状态码
|
|
||||||
if (!res.ok) {
|
|
||||||
const errorData = await res.json();
|
|
||||||
throw new Error(errorData.message || `HTTP error! Status: ${res.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析响应数据
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
setResponse(JSON.stringify(data, null, 2));
|
setResponse(JSON.stringify(data, null, 2));
|
||||||
|
|
||||||
@ -560,13 +541,11 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置提示消息
|
// 根据状态设置提示消息
|
||||||
setSnackbarMessage(data.info || '操作成功');
|
setSnackbarMessage(data.info || '操作成功');
|
||||||
setSnackbarOpen(true);
|
setSnackbarOpen(true);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 错误处理
|
|
||||||
console.error('API call failed:', error);
|
|
||||||
setResponse(`Error: ${error.message}`);
|
setResponse(`Error: ${error.message}`);
|
||||||
setSnackbarMessage(`错误: ${error.message}`);
|
setSnackbarMessage(`错误: ${error.message}`);
|
||||||
setSnackbarOpen(true);
|
setSnackbarOpen(true);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user