对返回的各种数据格式做归一化成对象的处理
This commit is contained in:
parent
5b41563619
commit
83b5e41105
23
client.go
23
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 ||
|
||||
|
@ -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状态码描述
|
||||
|
Loading…
Reference in New Issue
Block a user