优化默认请求消息

This commit is contained in:
白茶清欢 2023-08-26 22:13:25 +08:00
parent 0cae2e48bc
commit cb7721dd7a
2 changed files with 12 additions and 5 deletions

View File

@ -47,8 +47,10 @@ const (
DefaultResponseMessageField = "message" DefaultResponseMessageField = "message"
// DefaultResponseDataField 默认数据字段 // DefaultResponseDataField 默认数据字段
DefaultResponseDataField = "data" DefaultResponseDataField = "data"
// DefaultMessage 接口响应失败 // DefaultFailMessage 接口响应失败
DefaultMessage = "api request fail" DefaultFailMessage = "api request fail"
// DefaultSuccessMessage 接口响应成功
DefaultSuccessMessage = "api request success"
) )
var ( var (

View File

@ -306,9 +306,6 @@ func send(apiConfig *ApiRequestConfig, header map[string]string) *ApiResponse {
} }
// 提取响应文案 // 提取响应文案
response.Message = gjson.GetBytes(responseByte, response.RequestConfig.ResponseMessageField).String() response.Message = gjson.GetBytes(responseByte, response.RequestConfig.ResponseMessageField).String()
if len(response.Message) == 0 {
response.Message = DefaultMessage
}
// 判断响应状态码是否成功 // 判断响应状态码是否成功
isBusinessCodeSuccess := false 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 { if !isBusinessCodeSuccess {
response.Exception = exception.New(ResponseCodeNotFound, response.Response.StatusCode, map[string]string{}, response.Message) response.Exception = exception.New(ResponseCodeNotFound, response.Response.StatusCode, map[string]string{}, response.Message)
return response return response