对返回的各种数据格式做归一化成对象的处理
This commit is contained in:
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 ||
|
||||
|
Reference in New Issue
Block a user