From cb7721dd7acc7a5e79a71a6cf9fd00cfc45bc2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 26 Aug 2023 22:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=BB=98=E8=AE=A4=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httpclient/define.go | 6 ++++-- httpclient/request.go | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/httpclient/define.go b/httpclient/define.go index 0c8a70d..091f173 100644 --- a/httpclient/define.go +++ b/httpclient/define.go @@ -47,8 +47,10 @@ const ( DefaultResponseMessageField = "message" // DefaultResponseDataField 默认数据字段 DefaultResponseDataField = "data" - // DefaultMessage 接口响应失败 - DefaultMessage = "api request fail" + // DefaultFailMessage 接口响应失败 + DefaultFailMessage = "api request fail" + // DefaultSuccessMessage 接口响应成功 + DefaultSuccessMessage = "api request success" ) var ( diff --git a/httpclient/request.go b/httpclient/request.go index 048b834..b269ab5 100644 --- a/httpclient/request.go +++ b/httpclient/request.go @@ -306,9 +306,6 @@ func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse { } // 提取响应文案 response.Message = gjson.GetBytes(responseByte, response.RequestConfig.ResponseMessageField).String() - if len(response.Message) == 0 { - response.Message = DefaultMessage - } // 判断响应状态码是否成功 isBusinessCodeSuccess := false @@ -319,6 +316,14 @@ func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse { } } + if len(response.Message) == 0 { + if isBusinessCodeSuccess { + response.Message = DefaultSuccessMessage + } else { + response.Message = DefaultFailMessage + } + } + if !isBusinessCodeSuccess { response.Exception = exception.New(ResponseCodeNotFound, response.Response.StatusCode, map[string]string{}, response.Message) return response