fix(认证中间件): 统一认证失败的错误提示为"请先登录"

简化认证失败时的错误提示,不再使用原始业务错误信息,统一返回401状态码和"请先登录"提示,提升用户体验
This commit is contained in:
zhouyonggao 2025-12-26 11:30:56 +08:00
parent e92fbfe2c3
commit e2be6e67dd
1 changed files with 2 additions and 8 deletions

View File

@ -140,16 +140,10 @@ func withAuth(apiDomain string) func(http.Handler) http.Handler {
// 检查认证是否成功(支持 HTTP 状态码和业务 code
if resp.StatusCode != http.StatusOK || authResp.Code != 200 {
// 优先使用业务返回的错误信息
errorMsg := authResp.Message
if errorMsg == "" {
errorMsg = "认证失败"
}
// 返回原始的业务错误码和消息
responseBody := fmt.Sprintf("{\"code\":%d,\"message\":\"%s\",\"data\":null}", authResp.Code, errorMsg)
// 所有认证错误都显示"请先登录"
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(responseBody))
w.Write([]byte("{\"code\":401,\"message\":\"请先登录\",\"data\":null}"))
return
}