From f923ed0695e94981f5df8387c4f1c9771b9032b5 Mon Sep 17 00:00:00 2001 From: Zhuym Date: Thu, 30 Jan 2025 21:11:32 +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=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 56 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 17 deletions(-) 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 @@ ⚠️部分后端地址已设置访问认证保护,使用前需完成身份认证。 +