From 2f1f166fb8eb905977b7dfc72e31cc168c09bcf5 Mon Sep 17 00:00:00 2001 From: zhouyonggao <1971162852@qq.com> Date: Fri, 26 Dec 2025 11:22:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E7=BB=9F=E4=B8=80=E6=9C=AA?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=89=8D=E7=AB=AF=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请求添加对401错误的特殊处理,显示服务端返回的具体错误信息 --- server/internal/api/middleware.go | 4 ++-- web/modules/api.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/server/internal/api/middleware.go b/server/internal/api/middleware.go index 529936f..80dec46 100644 --- a/server/internal/api/middleware.go +++ b/server/internal/api/middleware.go @@ -100,7 +100,7 @@ func withAuth(apiDomain string) func(http.Handler) http.Handler { // 获取 token token := r.Header.Get("token") if token == "" { - http.Error(w, "{\"code\":401,\"message\":\"未提供认证token\",\"data\":null}", http.StatusUnauthorized) + http.Error(w, "{\"code\":401,\"message\":\"请先登录\",\"data\":null}", http.StatusUnauthorized) return } @@ -108,7 +108,7 @@ func withAuth(apiDomain string) func(http.Handler) http.Handler { authURL := fmt.Sprintf("%s/auth/admin/dataPermission", apiDomain) req, err := http.NewRequest("GET", authURL, nil) if err != nil { - http.Error(w, "{\"code\":500,\"message\":\"创建认证请求失败\",\"data\":null}", http.StatusInternalServerError) + http.Error(w, "{\"code\":401,\"message\":\"请先登录\",\"data\":null}", http.StatusUnauthorized) return } diff --git a/web/modules/api.js b/web/modules/api.js index 4ac6969..043e30b 100644 --- a/web/modules/api.js +++ b/web/modules/api.js @@ -151,6 +151,10 @@ const get = async (endpoint, options = {}) => { headers: buildHeaders() }); if (!response.ok) { + const data = await response.json().catch(() => ({})); + if (response.status === 401 && data.message) { + throw new Error(data.message); + } throw new Error(`请求失败: ${response.status} ${response.statusText}`); } return response.json(); @@ -179,6 +183,10 @@ 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 errorText = await response.text(); throw new Error(errorText || `请求失败: ${response.status}`); } @@ -211,6 +219,10 @@ 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 errorText = await response.text(); throw new Error(errorText || `请求失败: ${response.status}`); } @@ -236,6 +248,10 @@ const del = async (endpoint, options = {}) => { headers: buildHeaders() }); if (!response.ok) { + const data = await response.json().catch(() => ({})); + if (response.status === 401 && data.message) { + throw new Error(data.message); + } const errorText = await response.text(); throw new Error(errorText || `请求失败: ${response.status}`); }