From 9813be8873ccf5a5e0548fa3a7dee9f743174f5b Mon Sep 17 00:00:00 2001 From: maguodong Date: Thu, 16 Oct 2025 15:26:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0JSON=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=87=AA=E5=8A=A8=E6=B8=85=E7=90=86=E7=A9=BA?= =?UTF-8?q?=E5=80=BC=E5=8A=9F=E8=83=BD=20-=20=E5=9C=A8DoRequest=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=B8=AD=E9=9B=86=E6=88=90JSON=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=B8=85=E7=90=86=E9=80=BB=E8=BE=91=20-=20=E6=96=B0=E5=A2=9ECl?= =?UTF-8?q?eanJSON=E5=87=BD=E6=95=B0=E7=94=A8=E4=BA=8E=E7=A7=BB=E9=99=A4JS?= =?UTF-8?q?ON=E4=B8=AD=E7=9A=84=E7=A9=BA=E5=80=BC=E5=92=8C0=E5=80=BC?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=20-=20=E6=B7=BB=E5=8A=A0=E9=80=92=E5=BD=92?= =?UTF-8?q?=E6=B8=85=E7=90=86map=E5=92=8Cslice=E6=95=B0=E6=8D=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E7=9A=84=E6=94=AF=E6=8C=81=20-=20=E5=AE=9E=E7=8E=B0is?= =?UTF-8?q?EmptyValue=E5=87=BD=E6=95=B0=E6=A3=80=E6=B5=8B=E5=90=84?= =?UTF-8?q?=E7=A7=8D=E7=B1=BB=E5=9E=8B=E7=9A=84=E7=A9=BA=E5=80=BC=20-=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0isCleanedValueEmpty=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=B8=85=E7=90=86=E5=90=8E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E4=B8=BA=E7=A9=BA=20-=20=E5=BC=95=E5=85=A5gj?= =?UTF-8?q?son=E5=BA=93=E7=94=A8=E4=BA=8EJSON=E7=BC=96=E8=A7=A3=E7=A0=81?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lklsdk/common/client.go | 11 +++- lklsdk/common/json_cleaner.go | 118 ++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 lklsdk/common/json_cleaner.go diff --git a/lklsdk/common/client.go b/lklsdk/common/client.go index 4c6e80b..52deba3 100644 --- a/lklsdk/common/client.go +++ b/lklsdk/common/client.go @@ -15,6 +15,7 @@ import ( "time" "github.com/black1552/base-common/utils" + "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/util/gconv" ) @@ -91,8 +92,16 @@ func (c *Client[T]) loadPrivateKey() (*rsa.PrivateKey, error) { // DoRequest 发送HTTP请求 func (c *Client[T]) DoRequest(url string, reqData interface{}) (*T, error) { + jsonStr, err := gjson.EncodeString(reqData) + if err != nil { + return nil, fmt.Errorf("请求参数转JSON字符串失败: %v", err) + } + reqJson, err := CleanJSON(jsonStr) + if err != nil { + return nil, fmt.Errorf("清理JSON空值失败: %v", err) + } // 序列化为JSON - jsonData, err := json.Marshal(reqData) + jsonData, err := json.Marshal(reqJson) if err != nil { return nil, fmt.Errorf("序列化请求数据失败: %v", err) } diff --git a/lklsdk/common/json_cleaner.go b/lklsdk/common/json_cleaner.go new file mode 100644 index 0000000..33d46c9 --- /dev/null +++ b/lklsdk/common/json_cleaner.go @@ -0,0 +1,118 @@ +package common + +import ( + "encoding/json" + "reflect" +) + +// CleanJSON 清理JSON字符串中的空值和0值字段 +func CleanJSON(jsonStr string) (string, error) { + var data interface{} + + // 解析JSON字符串 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { + return "", err + } + + // 递归清理数据 + cleaned := cleanData(data) + + // 转换回JSON字符串 + result, err := json.Marshal(cleaned) + if err != nil { + return "", err + } + + return string(result), nil +} + +// cleanData 递归清理map或slice中的空值和0值 +func cleanData(data interface{}) interface{} { + if data == nil { + return nil + } + + v := reflect.ValueOf(data) + + switch v.Kind() { + case reflect.Map: + cleanedMap := make(map[string]interface{}) + for _, key := range v.MapKeys() { + val := v.MapIndex(key) + if val.Interface() != nil && !isEmptyValue(val) { + cleanedVal := cleanData(val.Interface()) + // 只有清理后的值不为空才添加 + if !isCleanedValueEmpty(cleanedVal) { + cleanedMap[key.String()] = cleanedVal + } + } + } + return cleanedMap + + case reflect.Slice: + cleanedSlice := make([]interface{}, 0) + for i := 0; i < v.Len(); i++ { + item := v.Index(i) + if item.Interface() != nil && !isEmptyValue(item) { + cleanedItem := cleanData(item.Interface()) + if !isCleanedValueEmpty(cleanedItem) { + cleanedSlice = append(cleanedSlice, cleanedItem) + } + } + } + // 如果slice为空,则返回nil而不是空slice + if len(cleanedSlice) == 0 { + return nil + } + return cleanedSlice + + default: + return data + } +} + +// isEmptyValue 检查值是否为空值或0值 +func isEmptyValue(v reflect.Value) bool { + switch v.Kind() { + case reflect.Interface, reflect.Ptr: + return v.IsNil() + case reflect.Slice, reflect.Map: + return v.Len() == 0 + case reflect.String: + return v.String() == "" || v.String() == "0" + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Bool: + return !v.Bool() + } + + // 特殊处理nil值 + if !v.IsValid() { + return true + } + + return false +} + +// isCleanedValueEmpty 检查清理后的值是否为空 +func isCleanedValueEmpty(val interface{}) bool { + if val == nil { + return true + } + + v := reflect.ValueOf(val) + switch v.Kind() { + case reflect.Map: + return v.Len() == 0 + case reflect.Slice: + return v.Len() == 0 + case reflect.String: + return val == "" + } + + return false +}