From 25a21e284b5387d5cd8f9eced935f73584eb3316 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Fri, 28 Nov 2025 02:25:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BA=86config.js=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.json | 2 +- static/js/api.js | 29 ++++++++++------------------- static/js/config.js | 43 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/config.json b/config.json index a9c3c9c..cd70280 100644 --- a/config.json +++ b/config.json @@ -58,4 +58,4 @@ "maxBackups": 10, "maxAge": 30 } -} +} \ No newline at end of file diff --git a/static/js/api.js b/static/js/api.js index 9aeed62..9ae67f9 100644 --- a/static/js/api.js +++ b/static/js/api.js @@ -35,26 +35,17 @@ async function apiRequest(endpoint, method = 'GET', data = null) { const responseText = await response.text(); if (!response.ok) { - // 尝试解析错误响应 - let errorData = {}; + // 优化错误响应处理 + console.warn(`API请求失败: ${response.status}`); + + // 尝试解析JSON,但如果失败,直接使用原始文本作为错误信息 try { - // 首先检查响应文本是否为空或不是有效JSON - if (!responseText || responseText.trim() === '') { - console.warn('错误响应为空'); - } else { - try { - errorData = JSON.parse(responseText); - } catch (parseError) { - console.error('无法解析错误响应为JSON:', parseError); - console.error('原始错误响应文本:', responseText); - } - } - // 直接返回错误信息,而不是抛出异常,让上层处理 - console.warn(`API请求失败: ${response.status}`, errorData); - return { error: errorData.error || `请求失败: ${response.status}` }; - } catch (e) { - console.error('处理错误响应时出错:', e); - return { error: `请求处理失败: ${e.message}` }; + const errorData = JSON.parse(responseText); + return { error: errorData.error || responseText || `请求失败: ${response.status}` }; + } catch (parseError) { + // 当响应不是有效的JSON时(如中文错误信息),直接使用原始文本 + console.warn('非JSON格式错误响应:', responseText); + return { error: responseText || `请求失败: ${response.status}` }; } } diff --git a/static/js/config.js b/static/js/config.js index c7c21e5..6996019 100644 --- a/static/js/config.js +++ b/static/js/config.js @@ -37,10 +37,18 @@ function initConfigPage() { async function loadConfig() { try { showLoading(true); - const config = await api.getConfig(); - populateConfigForm(config); + const result = await api.getConfig(); + + // 检查API返回的错误 + if (result && result.error) { + showErrorMessage('加载配置失败: ' + result.error); + return; + } + + populateConfigForm(result); } catch (error) { - showErrorMessage('加载配置失败: ' + error.message); + // 捕获可能的异常(虽然apiRequest不应该再抛出异常) + showErrorMessage('加载配置失败: ' + (error.message || '未知错误')); } finally { showLoading(false); } @@ -67,7 +75,8 @@ function populateConfigForm(config) { setElementValue('shield-local-rules-file', getSafeValue(shieldConfig.LocalRulesFile, 'data/rules.txt')); setElementValue('shield-update-interval', getSafeValue(shieldConfig.UpdateInterval, 3600)); setElementValue('shield-hosts-file', getSafeValue(shieldConfig.HostsFile, 'data/hosts.txt')); - setElementValue('shield-block-method', getSafeValue(shieldConfig.BlockMethod, '0.0.0.0')); + // 使用服务器端接受的屏蔽方法值,默认使用NXDOMAIN + setElementValue('shield-block-method', getSafeValue(shieldConfig.BlockMethod, 'NXDOMAIN')); } // 工具函数:安全设置元素值 @@ -98,10 +107,18 @@ async function handleSaveConfig() { try { showLoading(true); - await api.saveConfig(formData); + const result = await api.saveConfig(formData); + + // 检查API返回的错误 + if (result && result.error) { + showErrorMessage('保存配置失败: ' + result.error); + return; + } + showSuccessMessage('配置保存成功'); } catch (error) { - showErrorMessage('保存配置失败: ' + error.message); + // 捕获可能的异常(虽然apiRequest不应该再抛出异常) + showErrorMessage('保存配置失败: ' + (error.message || '未知错误')); } finally { showLoading(false); } @@ -113,10 +130,18 @@ async function handleRestartService() { try { showLoading(true); - await api.restartService(); + const result = await api.restartService(); + + // 检查API返回的错误 + if (result && result.error) { + showErrorMessage('服务重启失败: ' + result.error); + return; + } + showSuccessMessage('服务重启成功'); } catch (error) { - showErrorMessage('重启服务失败: ' + error.message); + // 捕获可能的异常(虽然apiRequest不应该再抛出异常) + showErrorMessage('重启服务失败: ' + (error.message || '未知错误')); } finally { showLoading(false); } @@ -172,7 +197,7 @@ function collectFormData() { LocalRulesFile: getElementValue('shield-local-rules-file') || './data/rules.txt', UpdateInterval: updateInterval, HostsFile: getElementValue('shield-hosts-file') || './data/hosts.txt', - BlockMethod: getElementValue('shield-block-method') || '0.0.0.0' + BlockMethod: getElementValue('shield-block-method') || 'NXDOMAIN' } }; }