fix(认证中间件): 统一认证失败的错误提示为"请先登录"
简化认证失败时的错误提示,不再使用原始业务错误信息,统一返回401状态码和"请先登录"提示,提升用户体验
This commit is contained in:
parent
e92fbfe2c3
commit
e2be6e67dd
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue