From 7a28bdb453dffe834f33ebae37dc2358ce43e0b0 Mon Sep 17 00:00:00 2001 From: Zhuym Date: Thu, 30 Jan 2025 21:52:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0index.html=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96API=E8=B0=83=E7=94=A8=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E5=85=BC=E5=AE=B9=E9=9D=9EJSON=E5=93=8D?= =?UTF-8?q?=E5=BA=94=EF=BC=8C=E4=BF=AE=E6=94=B9=E8=AE=A4=E8=AF=81=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E6=96=87=E6=9C=AC=E4=BB=A5=E6=9B=B4=E6=B8=85=E6=99=B0?= =?UTF-8?q?=E5=9C=B0=E8=AF=B4=E6=98=8E=E8=BA=AB=E4=BB=BD=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E8=A6=81=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 94 +++++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/index.html b/index.html index ad0dd29..0633745 100644 --- a/index.html +++ b/index.html @@ -223,7 +223,7 @@ } .MuiTypography-h6 { - font-size: 1.1rem !important; + font-size: 1.1rem !重要; } /* 表单样式 */ @@ -493,64 +493,71 @@ 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' } + 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()}`; + + // 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') { - if (config.requestFormat === 'json') { - options.body = JSON.stringify(formData); - } + + // POST请求体处理 + if (method === "POST" && config?.requestFormat === "json") { + options.body = JSON.stringify(formData); } const res = await fetch(url, options); - const data = await res.json(); - setResponse(JSON.stringify(data, null, 2)); + const rawText = await res.text(); // 先获取原始文本 - // 保存日志 - saveLog(endpoint, formData, data); - - // 处理登录响应 - if (config.endpoint === '/qr' && data.userId) { - setUserId(data.userId.toString()); - setCookie('userId', data.userId.toString(), 7); - - // 遍历所有表单配置,更新包含userid/userId字段的表单 - 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(兼容非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); - // 根据状态设置提示消息 - setSnackbarMessage(data.info || '操作成功'); + 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}`); + setSnackbarMessage(`网络错误: ${error.message}`); setSnackbarOpen(true); } - }; + }; const handleMusicChange = (e) => { const { name, value } = e.target; @@ -799,8 +806,7 @@ {/* 新增认证提示区域 */} - ⚠️部分后端地址已设置访问认证保护,使用前需完成身份认证。 - + ⚠️部分后端地址受认证保护 使用前可能需要身份认证