diff --git a/index.html b/index.html
index 3d3deef..4c663de 100644
--- a/index.html
+++ b/index.html
@@ -223,7 +223,7 @@
}
.MuiTypography-h6 {
- font-size: 1.1rem !重要;
+ font-size: 1.1rem !important;
}
/* 表单样式 */
@@ -493,44 +493,63 @@
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 {
const options = {
method,
- headers: { 'Content-Type': 'application/json' }
+ headers: {},
+ credentials: 'include', // 支持跨域请求携带 Cookie
};
let url = `${apiBase}${endpoint}`;
-
- // 处理GET请求参数
+
+ // 处理 GET 请求参数
if (method === 'GET' && config.queryParams) {
const params = new URLSearchParams();
config.queryParams.forEach(param => {
- if (formData[param]) params.append(param, formData[param]);
+ if (formData && formData[param]) {
+ params.append(param, formData[param]);
+ }
});
url += `?${params.toString()}`;
}
-
- // 处理POST请求体
- if (method === 'POST') {
- if (config.requestFormat === 'json') {
+
+ // 处理请求体
+ if (['POST', 'PUT', 'PATCH'].includes(method)) {
+ if (formData instanceof FormData) {
+ // 文件上传时,不设置 Content-Type,浏览器会自动处理
+ options.body = formData;
+ } else if (config.requestFormat === 'json') {
+ options.headers['Content-Type'] = 'application/json';
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);
+
+ // 检查 HTTP 状态码
+ if (!res.ok) {
+ const errorData = await res.json();
+ throw new Error(errorData.message || `HTTP error! Status: ${res.status}`);
+ }
+
+ // 解析响应数据
const data = await res.json();
setResponse(JSON.stringify(data, null, 2));
-
+
// 保存日志
saveLog(endpoint, formData, data);
-
+
// 处理登录响应
if (config.endpoint === '/qr' && data.userId) {
setUserId(data.userId.toString());
setCookie('userId', data.userId.toString(), 7);
-
- // 遍历所有表单配置,更新包含userid/userId字段的表单
+
+ // 遍历所有表单配置,更新包含 userid/userId 字段的表单
Object.entries(formConfigs).forEach(([key, formConfig]) => {
const userIdField = formConfig.fields.find(
field => field.id.toLowerCase().includes('userid')
@@ -540,12 +559,14 @@
}
});
}
-
- // 根据状态设置提示消息
+
+ // 设置提示消息
setSnackbarMessage(data.info || '操作成功');
setSnackbarOpen(true);
-
+
} catch (error) {
+ // 错误处理
+ console.error('API call failed:', error);
setResponse(`Error: ${error.message}`);
setSnackbarMessage(`错误: ${error.message}`);
setSnackbarOpen(true);
@@ -800,6 +821,7 @@
⚠️部分后端地址已设置访问认证保护,使用前需完成身份认证。
+