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