update data handler

This commit is contained in:
白茶清欢 2024-06-08 16:02:22 +08:00
parent 83b5e41105
commit 6b5db7cdbd
2 changed files with 9 additions and 8 deletions

View File

@ -208,7 +208,7 @@ func (hc *HttpClient) newResponse() *define.Response {
return &define.Response{
Header: map[string]string{},
Cookie: map[string]string{},
Body: "",
Body: map[string]any{},
Code: "",
Message: "",
Data: "",
@ -262,13 +262,13 @@ func (hc *HttpClient) fillResponseCookie(response *define.Response) {
//
// Date : 21:38 2024/6/5
func (hc *HttpClient) fillResponseBody(response *define.Response) {
response.Body = string(response.RestyResponse.Body())
response.Code = gjson.Get(response.Body, hc.reqConfig.CodeField).String()
response.Message = gjson.Get(response.Body, hc.reqConfig.MessageField).String()
businessData := gjson.Get(response.Body, hc.reqConfig.DataField)
response.Data = string(response.RestyResponse.Body())
response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String()
response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String()
businessData := gjson.Get(response.Data, hc.reqConfig.DataField)
if businessData.Value() == nil {
// data为空指针, 归一化成空对象
response.Data = "{}"
response.Body = map[string]any{}
} else {
if businessData.IsArray() {
// 数组类型的转换
@ -284,10 +284,11 @@ func (hc *HttpClient) fillResponseBody(response *define.Response) {
})
}
}
_ = serialize.JSON.UnmarshalWithNumber([]byte(response.Data), &response.Body)
}
response.ExtendData = map[string]string{}
gjson.Parse(response.Body).ForEach(func(key, value gjson.Result) bool {
gjson.Parse(response.Data).ForEach(func(key, value gjson.Result) bool {
if key.String() == hc.reqConfig.CodeField ||
key.String() == hc.reqConfig.MessageField ||
key.String() == hc.reqConfig.DataField {

View File

@ -22,7 +22,7 @@ type Response struct {
Data string `json:"data"` // 响应body
Code string `json:"code"` // 业务状态码
Message string `json:"message"` // 业务状态码描述
Body string `json:"body"` // 响应数据, 如果 data 本身是 list, 会自动转成 {"list": 真实数据}
Body map[string]any `json:"body"` // 响应数据
ExtendData map[string]string `json:"extend_data"` // 除去 code / message / data 之外的其他数据
HttpCode int `json:"http_code"` // http状态码
HttpCodeStatus string `json:"http_code_status"` // http状态码描述