diff --git a/index.html b/index.html index 4c663de..ad0dd29 100644 --- a/index.html +++ b/index.html @@ -493,63 +493,44 @@ 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: {}, - credentials: 'include', // 支持跨域请求携带 Cookie + headers: { 'Content-Type': 'application/json' } }; let url = `${apiBase}${endpoint}`; - - // 处理 GET 请求参数 + + // 处理GET请求参数 if (method === 'GET' && config.queryParams) { const params = new URLSearchParams(); config.queryParams.forEach(param => { - if (formData && formData[param]) { - params.append(param, formData[param]); - } + if (formData[param]) params.append(param, formData[param]); }); url += `?${params.toString()}`; } - - // 处理请求体 - 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'; + + // 处理POST请求体 + if (method === 'POST') { + if (config.requestFormat === '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') @@ -559,14 +540,12 @@ } }); } - - // 设置提示消息 + + // 根据状态设置提示消息 setSnackbarMessage(data.info || '操作成功'); setSnackbarOpen(true); - + } catch (error) { - // 错误处理 - console.error('API call failed:', error); setResponse(`Error: ${error.message}`); setSnackbarMessage(`错误: ${error.message}`); setSnackbarOpen(true);