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}`);