diff --git a/client.go b/client.go index ed593fd..b79dcd6 100644 --- a/client.go +++ b/client.go @@ -8,6 +8,7 @@ package httpclient import ( + "fmt" "git.zhangdeman.cn/gateway/httpclient/cache" "git.zhangdeman.cn/gateway/httpclient/define" "git.zhangdeman.cn/gateway/httpclient/validate" @@ -264,7 +265,27 @@ 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() - response.Data = gjson.Get(response.Body, hc.reqConfig.DataField).String() + businessData := gjson.Get(response.Body, hc.reqConfig.DataField) + if businessData.Value() == nil { + // data为空指针, 归一化成空对象 + response.Data = "{}" + } else { + if businessData.IsArray() { + // 数组类型的转换 + response.Data = fmt.Sprintf(`{"list":` + businessData.String() + "}") + } else { + if businessData.IsObject() { + // 返回的就是对象 + response.Data = businessData.String() + } else { + // 返回是普通类型 + response.Data = serialize.JSON.MarshalForString(map[string]any{ + "value": businessData.Value(), + }) + } + } + } + response.ExtendData = map[string]string{} gjson.Parse(response.Body).ForEach(func(key, value gjson.Result) bool { if key.String() == hc.reqConfig.CodeField || diff --git a/define/response.go b/define/response.go index c2a9701..d1aa5b8 100644 --- a/define/response.go +++ b/define/response.go @@ -22,7 +22,7 @@ type Response struct { Data string `json:"data"` // 响应body Code string `json:"code"` // 业务状态码 Message string `json:"message"` // 业务状态码描述 - Body string `json:"body"` // 响应数据 + Body string `json:"body"` // 响应数据, 如果 data 本身是 list, 会自动转成 {"list": 真实数据} ExtendData map[string]string `json:"extend_data"` // 除去 code / message / data 之外的其他数据 HttpCode int `json:"http_code"` // http状态码 HttpCodeStatus string `json:"http_code_status"` // http状态码描述