From 9aeeee0ac00cd7a1030468efca2a6a5c58867fcf Mon Sep 17 00:00:00 2001 From: bootx Date: Thu, 8 Feb 2024 23:46:10 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E7=AD=BE=E5=90=8D=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=AF=B9=E5=B5=8C=E5=A5=97=E5=AF=B9=E8=B1=A1=E5=A4=84?= =?UTF-8?q?=E7=90=86=E8=80=83=E8=99=91=E4=B8=8D=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/daxpay/sdk/util/PaySignUtil.java | 20 ++++++++++++++++--- .../platform/daxpay/util/PaySignUtil.java | 13 +++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/daxpay-single-sdk/src/main/java/cn/bootx/platform/daxpay/sdk/util/PaySignUtil.java b/daxpay-single-sdk/src/main/java/cn/bootx/platform/daxpay/sdk/util/PaySignUtil.java index 9ad1f5640..a86764b3f 100644 --- a/daxpay-single-sdk/src/main/java/cn/bootx/platform/daxpay/sdk/util/PaySignUtil.java +++ b/daxpay-single-sdk/src/main/java/cn/bootx/platform/daxpay/sdk/util/PaySignUtil.java @@ -1,5 +1,6 @@ package cn.bootx.platform.daxpay.sdk.util; +import cn.bootx.platform.daxpay.sdk.param.SortMapParam; import cn.hutool.core.util.ClassUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.digest.HmacAlgorithm; @@ -38,6 +39,7 @@ public class PaySignUtil { /** * 将参数转换为map对象. 使用ChatGPT生成, 仅局限于对请求支付相关参数进行签名 */ + @SuppressWarnings({"unchecked", "rawtypes"}) @SneakyThrows private void toMap(Object object, Map map) { Class clazz = object.getClass(); @@ -53,6 +55,12 @@ public class PaySignUtil { String fieldValueString = String.valueOf(fieldValue); map.put(fieldName, fieldValueString); } + // map类型 + else if (Map.class.isAssignableFrom(field.getType())) { + Map m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + m.putAll((Map) fieldValue); + toMap(fieldValue, m); + } // 集合类型 else if (Collection.class.isAssignableFrom(field.getType())) { Collection collection = (Collection) fieldValue; @@ -67,11 +75,17 @@ public class PaySignUtil { .collect(Collectors.toList()); map.put(fieldName, JSONUtil.toJsonStr(maps)); } - // 其他类型 - } else { + } + // 可以转换为MAP并进行排序的类型 + else if (SortMapParam.class.isAssignableFrom(field.getType())) { Map nestedMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); toMap(fieldValue, nestedMap); - String nestedJson = JSONUtil.toJsonStr(map); + String nestedJson = JSONUtil.toJsonStr(nestedMap); + map.put(fieldName, nestedJson); + } + else { + // 其他类型 + String nestedJson = JSONUtil.toJsonStr(fieldValue); map.put(fieldName, nestedJson); } } diff --git a/daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/util/PaySignUtil.java b/daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/util/PaySignUtil.java index c6f53a396..48012f478 100644 --- a/daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/util/PaySignUtil.java +++ b/daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/util/PaySignUtil.java @@ -41,6 +41,7 @@ public class PaySignUtil { /** * 将参数转换为map对象. 使用ChatGPT生成, 仅局限于对请求支付相关参数进行签名 */ + @SuppressWarnings({"unchecked", "rawtypes"}) @SneakyThrows private void toMap(Object object, Map map) { Class clazz = object.getClass(); @@ -60,9 +61,15 @@ public class PaySignUtil { // java8时间类型 转为时间戳 else if (field.getType().equals(LocalDateTime.class)) { LocalDateTime localDateTime = (LocalDateTime) fieldValue; - long timestamp = LocalDateTimeUtil.timestamp(localDateTime); + long timestamp = LocalDateTimeUtil.timestamp(localDateTime)/1000; map.put(fieldName, String.valueOf(timestamp)); } + // map类型 + else if (Map.class.isAssignableFrom(field.getType())) { + Map m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + m.putAll((Map) fieldValue); + map.put(fieldName, JSONUtil.toJsonStr(m)); + } // 集合类型 else if (Collection.class.isAssignableFrom(field.getType())) { Collection collection = (Collection) fieldValue; @@ -77,11 +84,11 @@ public class PaySignUtil { .collect(Collectors.toList()); map.put(fieldName, JSONUtil.toJsonStr(maps)); } - // 其他类型 + // 其他类型直接转换为json } else { Map nestedMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); toMap(fieldValue, nestedMap); - String nestedJson = JSONUtil.toJsonStr(map); + String nestedJson = JSONUtil.toJsonStr(fieldValue); map.put(fieldName, nestedJson); } }