mirror of
https://github.com/Zhuym07/Tsumugiboshi.git
synced 2025-07-07 01:24:45 +08:00
更新index.html,重构API调用函数,增强错误处理逻辑以兼容非JSON响应,并优化日志记录
This commit is contained in:
parent
7a28bdb453
commit
f8a7da1959
160
index.html
160
index.html
@ -223,7 +223,7 @@
|
||||
}
|
||||
|
||||
.MuiTypography-h6 {
|
||||
font-size: 1.1rem !重要;
|
||||
font-size: 1.1rem !important;
|
||||
}
|
||||
|
||||
/* 表单样式 */
|
||||
@ -462,6 +462,98 @@
|
||||
const [isLogExpanded, setIsLogExpanded] = React.useState(false);
|
||||
const formRefs = React.useRef({});
|
||||
|
||||
const handleApiCall = async (endpoint, method = "GET", formData = null, config) => {
|
||||
try {
|
||||
const options = {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include"
|
||||
};
|
||||
|
||||
let url = `${apiBase}${endpoint}`;
|
||||
|
||||
if (method === "GET" && config?.queryParams) {
|
||||
const params = new URLSearchParams();
|
||||
config.queryParams.forEach(param => {
|
||||
if (formData?.[param]) params.append(param, formData[param]);
|
||||
});
|
||||
url += `?${params.toString()}`;
|
||||
}
|
||||
|
||||
if (method === "POST" && config?.requestFormat === "json") {
|
||||
options.body = JSON.stringify(formData);
|
||||
}
|
||||
|
||||
const res = await fetch(url, options);
|
||||
const rawText = await res.text();
|
||||
|
||||
let responseData;
|
||||
try {
|
||||
responseData = JSON.parse(rawText);
|
||||
} catch {
|
||||
responseData = {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
rawResponse: rawText
|
||||
};
|
||||
}
|
||||
|
||||
// 将状态码添加到显示结果中
|
||||
const displayResponse = {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
...responseData
|
||||
};
|
||||
|
||||
setResponse(JSON.stringify(displayResponse, null, 2));
|
||||
|
||||
// 保存日志
|
||||
saveLog(endpoint, {
|
||||
method,
|
||||
url,
|
||||
...formData,
|
||||
status: res.status
|
||||
}, displayResponse);
|
||||
|
||||
// 特殊处理登录成功的情况
|
||||
if (config?.endpoint === "/qr" && responseData.userId) {
|
||||
setUserId(responseData.userId.toString());
|
||||
setCookie("userId", responseData.userId.toString(), 7);
|
||||
|
||||
Object.entries(formConfigs).forEach(([key, formConfig]) => {
|
||||
const userIdField = formConfig.fields.find(
|
||||
field => field.id.toLowerCase().includes("userid")
|
||||
);
|
||||
if (userIdField && formRefs.current[key]) {
|
||||
formRefs.current[key].updateField(userIdField.id, responseData.userId.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 显示消息提示
|
||||
setSnackbarMessage(responseData.info || `请求完成 (${res.status} ${res.statusText})`);
|
||||
setSnackbarOpen(true);
|
||||
|
||||
} catch (error) {
|
||||
const errorResponse = {
|
||||
error: true,
|
||||
message: error.message,
|
||||
type: error.name
|
||||
};
|
||||
|
||||
setResponse(JSON.stringify(errorResponse, null, 2));
|
||||
setSnackbarMessage(`请求发生错误: ${error.message}`);
|
||||
setSnackbarOpen(true);
|
||||
|
||||
// 保存错误日志
|
||||
saveLog(endpoint, {
|
||||
method,
|
||||
url: `${apiBase}${endpoint}`,
|
||||
...formData
|
||||
}, errorResponse);
|
||||
}
|
||||
};
|
||||
|
||||
// Cookie操作函数
|
||||
function setCookie(name, value, days) {
|
||||
const d = new Date();
|
||||
@ -493,72 +585,6 @@
|
||||
localStorage.setItem('apiLogs', JSON.stringify(updatedLogs));
|
||||
};
|
||||
|
||||
const handleApiCall = async (endpoint, method = "GET", formData = null, config) => {
|
||||
try {
|
||||
const options = {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include" // 跨域凭证
|
||||
};
|
||||
|
||||
let url = `${apiBase}${endpoint}`;
|
||||
|
||||
// GET请求参数处理
|
||||
if (method === "GET" && config?.queryParams) {
|
||||
const params = new URLSearchParams();
|
||||
config.queryParams.forEach(param => {
|
||||
if (formData?.[param]) params.append(param, formData[param]);
|
||||
});
|
||||
url += `?${params.toString()}`;
|
||||
}
|
||||
|
||||
// POST请求体处理
|
||||
if (method === "POST" && config?.requestFormat === "json") {
|
||||
options.body = JSON.stringify(formData);
|
||||
}
|
||||
|
||||
const res = await fetch(url, options);
|
||||
const rawText = await res.text(); // 先获取原始文本
|
||||
|
||||
// 尝试解析JSON(兼容非JSON响应)
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(rawText);
|
||||
} catch {
|
||||
data = { _raw: rawText }; // 解析失败时保留原始响应
|
||||
}
|
||||
|
||||
setResponse(JSON.stringify(data, null, 2)); // 显示原始内容
|
||||
|
||||
// 保存日志(包含状态码)
|
||||
saveLog(endpoint, { ...formData, _status: res.status }, data);
|
||||
|
||||
// 登录处理逻辑
|
||||
if (config?.endpoint === "/qr" && data.userId) {
|
||||
setUserId(data.userId.toString());
|
||||
setCookie("userId", data.userId.toString(), 7);
|
||||
|
||||
Object.entries(formConfigs).forEach(([key, formConfig]) => {
|
||||
const userIdField = formConfig.fields.find(
|
||||
field => field.id.toLowerCase().includes("userid")
|
||||
);
|
||||
if (userIdField && formRefs.current[key]) {
|
||||
formRefs.current[key].updateField(userIdField.id, data.userId.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 显示服务器返回的原始提示(兼容非JSON响应)
|
||||
setSnackbarMessage(data.info || data._raw || "请求完成");
|
||||
setSnackbarOpen(true);
|
||||
|
||||
} catch (error) {
|
||||
setResponse(`Error: ${error.message}`);
|
||||
setSnackbarMessage(`网络错误: ${error.message}`);
|
||||
setSnackbarOpen(true);
|
||||
}
|
||||
};
|
||||
|
||||
const handleMusicChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setMusicData(prev => ({
|
||||
|
Loading…
x
Reference in New Issue
Block a user