From e92fbfe2c310677ec606da9325430fe1c093a84f Mon Sep 17 00:00:00 2001 From: zhouyonggao <1971162852@qq.com> Date: Fri, 26 Dec 2025 11:27:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E4=B8=BAAPI=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A4=E8=AF=81=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 在API请求失败时,区分认证错误和其他错误类型 修改错误处理逻辑以显示不同的错误消息 --- web/main.js | 30 +++++++++++++++++++----------- web/modules/api.js | 16 ++++++++++++---- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/web/main.js b/web/main.js index 9ec9500..c02adbb 100644 --- a/web/main.js +++ b/web/main.js @@ -503,16 +503,20 @@ const app = createApp({ // ==================== 模板管理 ==================== /** - * 加载模板列表 - */ - const loadTemplates = async () => { - try { - state.templates = await Api.fetchTemplates(); - } catch (error) { - showMessage('加载模板失败', 'error'); - state.templates = []; - } - }; + * 加载模板列表 + */ +const loadTemplates = async () => { + try { + state.templates = await Api.fetchTemplates(); + } catch (error) { + if (error.isAuthError) { + showMessage('请先登录', 'error'); + } else { + showMessage('加载模板失败', 'error'); + } + state.templates = []; + } +}; /** * 创建模板 @@ -993,7 +997,11 @@ const app = createApp({ state.sqlText = ''; state.sqlExplainDesc = ''; state.sqlVisible = false; - showMessage('加载SQL失败', 'error'); + if (error.isAuthError) { + showMessage('请先登录', 'error'); + } else { + showMessage('加载SQL失败', 'error'); + } } }; diff --git a/web/modules/api.js b/web/modules/api.js index 043e30b..feeabdb 100644 --- a/web/modules/api.js +++ b/web/modules/api.js @@ -153,7 +153,9 @@ const get = async (endpoint, options = {}) => { if (!response.ok) { const data = await response.json().catch(() => ({})); if (response.status === 401 && data.message) { - throw new Error(data.message); + const authError = new Error(data.message); + authError.isAuthError = true; + throw authError; } throw new Error(`请求失败: ${response.status} ${response.statusText}`); } @@ -185,7 +187,9 @@ const post = async (endpoint, body, options = {}) => { if (!response.ok) { const data = await response.json().catch(() => ({})); if (response.status === 401 && data.message) { - throw new Error(data.message); + const authError = new Error(data.message); + authError.isAuthError = true; + throw authError; } const errorText = await response.text(); throw new Error(errorText || `请求失败: ${response.status}`); @@ -221,7 +225,9 @@ const patch = async (endpoint, body) => { if (!response.ok) { const data = await response.json().catch(() => ({})); if (response.status === 401 && data.message) { - throw new Error(data.message); + const authError = new Error(data.message); + authError.isAuthError = true; + throw authError; } const errorText = await response.text(); throw new Error(errorText || `请求失败: ${response.status}`); @@ -250,7 +256,9 @@ const del = async (endpoint, options = {}) => { if (!response.ok) { const data = await response.json().catch(() => ({})); if (response.status === 401 && data.message) { - throw new Error(data.message); + const authError = new Error(data.message); + authError.isAuthError = true; + throw authError; } const errorText = await response.text(); throw new Error(errorText || `请求失败: ${response.status}`);