mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	🎨 优化代码
This commit is contained in:
		| @ -3,21 +3,25 @@ package me.chanjar.weixin.common.util.json; | |||||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||||
| import com.google.gson.JsonParser; | import com.google.gson.JsonParser; | ||||||
| import com.google.gson.stream.JsonReader; | import com.google.gson.stream.JsonReader; | ||||||
|  | import lombok.NoArgsConstructor; | ||||||
|  |  | ||||||
| import java.io.Reader; | import java.io.Reader; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author niefy | ||||||
|  |  */ | ||||||
| public class GsonParser { | public class GsonParser { | ||||||
|   private static final JsonParser JSON_PARSER = new JsonParser(); |   private static final JsonParser JSON_PARSER = new JsonParser(); | ||||||
|  |  | ||||||
|   public static JsonObject parse(String json){ |   public static JsonObject parse(String json) { | ||||||
|     return JSON_PARSER.parse(json).getAsJsonObject(); |     return JSON_PARSER.parse(json).getAsJsonObject(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static JsonObject parse(Reader json){ |   public static JsonObject parse(Reader json) { | ||||||
|     return JSON_PARSER.parse(json).getAsJsonObject(); |     return JSON_PARSER.parse(json).getAsJsonObject(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static JsonObject parse(JsonReader json){ |   public static JsonObject parse(JsonReader json) { | ||||||
|     return JSON_PARSER.parse(json).getAsJsonObject(); |     return JSON_PARSER.parse(json).getAsJsonObject(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -13,7 +13,6 @@ import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||||
|  * @author chanjarster |  * @author chanjarster | ||||||
|  */ |  */ | ||||||
| public class WxGsonBuilder { | public class WxGsonBuilder { | ||||||
|  |  | ||||||
|   private static final GsonBuilder INSTANCE = new GsonBuilder(); |   private static final GsonBuilder INSTANCE = new GsonBuilder(); | ||||||
|  |  | ||||||
|   static { |   static { | ||||||
|  | |||||||
| @ -71,6 +71,7 @@ | |||||||
|       <artifactId>lombok</artifactId> |       <artifactId>lombok</artifactId> | ||||||
|     </dependency> |     </dependency> | ||||||
|     <dependency> |     <dependency> | ||||||
|  |       <!-- 待优化去掉 --> | ||||||
|       <groupId>com.fasterxml.jackson.core</groupId> |       <groupId>com.fasterxml.jackson.core</groupId> | ||||||
|       <artifactId>jackson-databind</artifactId> |       <artifactId>jackson-databind</artifactId> | ||||||
|       <version>2.9.10.4</version> |       <version>2.9.10.4</version> | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| package com.github.binarywang.wxpay.bean.payscore; | package com.github.binarywang.wxpay.bean.payscore; | ||||||
|  |  | ||||||
| import com.google.gson.annotations.SerializedName; | import com.google.gson.annotations.SerializedName; | ||||||
|  | import lombok.AllArgsConstructor; | ||||||
| import lombok.Data; | import lombok.Data; | ||||||
| import lombok.NoArgsConstructor; | import lombok.NoArgsConstructor; | ||||||
|  |  | ||||||
| @ -14,6 +15,7 @@ import java.io.Serializable; | |||||||
|  */ |  */ | ||||||
| @Data | @Data | ||||||
| @NoArgsConstructor | @NoArgsConstructor | ||||||
|  | @AllArgsConstructor | ||||||
| public class TimeRange implements Serializable { | public class TimeRange implements Serializable { | ||||||
|   private static final long serialVersionUID = 8169562173656314930L; |   private static final long serialVersionUID = 8169562173656314930L; | ||||||
|   /** |   /** | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ import lombok.Builder; | |||||||
| import lombok.Data; | import lombok.Data; | ||||||
| import lombok.NoArgsConstructor; | import lombok.NoArgsConstructor; | ||||||
| import lombok.experimental.Accessors; | import lombok.experimental.Accessors; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
|  |  | ||||||
| import java.io.Serializable; | import java.io.Serializable; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| @ -22,6 +23,10 @@ import java.util.List; | |||||||
| public class WxPayScoreRequest implements Serializable { | public class WxPayScoreRequest implements Serializable { | ||||||
|   private static final long serialVersionUID = 364764508076146082L; |   private static final long serialVersionUID = 364764508076146082L; | ||||||
|  |  | ||||||
|  |   public String toJson() { | ||||||
|  |     return WxGsonBuilder.create().toJson(this); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * out_order_no : 1234323JKHDFE1243252 |    * out_order_no : 1234323JKHDFE1243252 | ||||||
|    * appid : wxd678efh567hg6787 |    * appid : wxd678efh567hg6787 | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ package com.github.binarywang.wxpay.bean.payscore; | |||||||
| import com.google.gson.annotations.SerializedName; | import com.google.gson.annotations.SerializedName; | ||||||
| import lombok.Data; | import lombok.Data; | ||||||
| import lombok.NoArgsConstructor; | import lombok.NoArgsConstructor; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
|  |  | ||||||
| import java.io.Serializable; | import java.io.Serializable; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| @ -17,6 +18,10 @@ import java.util.Map; | |||||||
| public class WxPayScoreResult implements Serializable { | public class WxPayScoreResult implements Serializable { | ||||||
|   private static final long serialVersionUID = 8809250065540275770L; |   private static final long serialVersionUID = 8809250065540275770L; | ||||||
|  |  | ||||||
|  |   public static WxPayScoreResult fromJson(String json) { | ||||||
|  |     return WxGsonBuilder.create().fromJson(json, WxPayScoreResult.class); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * appid : wxd678efh567hg6787 |    * appid : wxd678efh567hg6787 | ||||||
|    * mchid : 1230000109 |    * mchid : 1230000109 | ||||||
|  | |||||||
| @ -8,9 +8,8 @@ import com.github.binarywang.wxpay.exception.WxPayException; | |||||||
| import com.github.binarywang.wxpay.service.PayScoreService; | import com.github.binarywang.wxpay.service.PayScoreService; | ||||||
| import com.github.binarywang.wxpay.service.WxPayService; | import com.github.binarywang.wxpay.service.WxPayService; | ||||||
| import com.github.binarywang.wxpay.v3.util.AesUtils; | import com.github.binarywang.wxpay.v3.util.AesUtils; | ||||||
| import com.google.gson.Gson; |  | ||||||
| import com.google.gson.GsonBuilder; |  | ||||||
| import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
| import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||||
| import org.apache.http.client.utils.URIBuilder; | import org.apache.http.client.utils.URIBuilder; | ||||||
|  |  | ||||||
| @ -26,7 +25,6 @@ import java.util.Map; | |||||||
|  */ |  */ | ||||||
| @RequiredArgsConstructor | @RequiredArgsConstructor | ||||||
| public class PayScoreServiceImpl implements PayScoreService { | public class PayScoreServiceImpl implements PayScoreService { | ||||||
|   private static final Gson GSON = new GsonBuilder().create(); |  | ||||||
|   private final WxPayService payService; |   private final WxPayService payService; | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -37,8 +35,8 @@ public class PayScoreServiceImpl implements PayScoreService { | |||||||
|     request.setAppid(config.getAppId()); |     request.setAppid(config.getAppId()); | ||||||
|     request.setServiceId(config.getServiceId()); |     request.setServiceId(config.getServiceId()); | ||||||
|     request.setNotifyUrl(config.getPayScoreNotifyUrl()); |     request.setNotifyUrl(config.getPayScoreNotifyUrl()); | ||||||
|     String result = payService.postV3(url, GSON.toJson(request)); |     String result = this.payService.postV3(url, request.toJson()); | ||||||
|     WxPayScoreResult wxPayScoreCreateResult = GSON.fromJson(result, WxPayScoreResult.class); |     WxPayScoreResult wxPayScoreCreateResult = WxPayScoreResult.fromJson(result); | ||||||
|  |  | ||||||
|     //补充算一下签名给小程序跳转用 |     //补充算一下签名给小程序跳转用 | ||||||
|     String currentTimeMillis = System.currentTimeMillis() + ""; |     String currentTimeMillis = System.currentTimeMillis() + ""; | ||||||
| @ -84,7 +82,7 @@ public class PayScoreServiceImpl implements PayScoreService { | |||||||
|     uriBuilder.setParameter("appid", config.getAppId()); |     uriBuilder.setParameter("appid", config.getAppId()); | ||||||
|     try { |     try { | ||||||
|       String result = payService.getV3(uriBuilder.build()); |       String result = payService.getV3(uriBuilder.build()); | ||||||
|       return GSON.fromJson(result, WxPayScoreResult.class); |       return WxPayScoreResult.fromJson(result); | ||||||
|     } catch (URISyntaxException e) { |     } catch (URISyntaxException e) { | ||||||
|       throw new WxPayException("未知异常!", e); |       throw new WxPayException("未知异常!", e); | ||||||
|     } |     } | ||||||
| @ -95,12 +93,12 @@ public class PayScoreServiceImpl implements PayScoreService { | |||||||
|   public WxPayScoreResult cancelServiceOrder(String outOrderNo, String reason) throws WxPayException { |   public WxPayScoreResult cancelServiceOrder(String outOrderNo, String reason) throws WxPayException { | ||||||
|     WxPayConfig config = this.payService.getConfig(); |     WxPayConfig config = this.payService.getConfig(); | ||||||
|     String url = String.format("%s/v3/payscore/serviceorder/%s/cancel", this.payService.getPayBaseUrl(), outOrderNo); |     String url = String.format("%s/v3/payscore/serviceorder/%s/cancel", this.payService.getPayBaseUrl(), outOrderNo); | ||||||
|     HashMap<String, Object> map = new HashMap<>(4); |     Map<String, Object> map = new HashMap<>(4); | ||||||
|     map.put("appid", config.getAppId()); |     map.put("appid", config.getAppId()); | ||||||
|     map.put("service_id", config.getServiceId()); |     map.put("service_id", config.getServiceId()); | ||||||
|     map.put("reason", reason); |     map.put("reason", reason); | ||||||
|     String result = payService.postV3(url, GSON.toJson(map)); |     String result = payService.postV3(url, WxGsonBuilder.create().toJson(map)); | ||||||
|     return GSON.fromJson(result, WxPayScoreResult.class); |     return WxPayScoreResult.fromJson(result); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -111,8 +109,8 @@ public class PayScoreServiceImpl implements PayScoreService { | |||||||
|     request.setAppid(config.getAppId()); |     request.setAppid(config.getAppId()); | ||||||
|     request.setServiceId(config.getServiceId()); |     request.setServiceId(config.getServiceId()); | ||||||
|     request.setOutOrderNo(null); |     request.setOutOrderNo(null); | ||||||
|     String result = payService.postV3(url, GSON.toJson(request)); |     String result = payService.postV3(url, request.toJson()); | ||||||
|     return GSON.fromJson(result, WxPayScoreResult.class); |     return WxPayScoreResult.fromJson(result); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -123,19 +121,19 @@ public class PayScoreServiceImpl implements PayScoreService { | |||||||
|     request.setAppid(config.getAppId()); |     request.setAppid(config.getAppId()); | ||||||
|     request.setServiceId(config.getServiceId()); |     request.setServiceId(config.getServiceId()); | ||||||
|     request.setOutOrderNo(null); |     request.setOutOrderNo(null); | ||||||
|     String result = payService.postV3(url, GSON.toJson(request)); |     String result = payService.postV3(url, request.toJson()); | ||||||
|     return GSON.fromJson(result, WxPayScoreResult.class); |     return WxPayScoreResult.fromJson(result); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxPayScoreResult payServiceOrder(String outOrderNo) throws WxPayException { |   public WxPayScoreResult payServiceOrder(String outOrderNo) throws WxPayException { | ||||||
|     WxPayConfig config = this.payService.getConfig(); |     WxPayConfig config = this.payService.getConfig(); | ||||||
|     String url = String.format("%s/v3/payscore/serviceorder/%s/pay", this.payService.getPayBaseUrl(), outOrderNo); |     String url = String.format("%s/v3/payscore/serviceorder/%s/pay", this.payService.getPayBaseUrl(), outOrderNo); | ||||||
|     HashMap<String, Object> map = new HashMap<>(2); |     Map<String, Object> map = new HashMap<>(2); | ||||||
|     map.put("appid", config.getAppId()); |     map.put("appid", config.getAppId()); | ||||||
|     map.put("service_id", config.getServiceId()); |     map.put("service_id", config.getServiceId()); | ||||||
|     String result = payService.postV3(url, GSON.toJson(map)); |     String result = payService.postV3(url, WxGsonBuilder.create().toJson(map)); | ||||||
|     return GSON.fromJson(result, WxPayScoreResult.class); |     return WxPayScoreResult.fromJson(result); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -146,13 +144,13 @@ public class PayScoreServiceImpl implements PayScoreService { | |||||||
|     request.setAppid(config.getAppId()); |     request.setAppid(config.getAppId()); | ||||||
|     request.setServiceId(config.getServiceId()); |     request.setServiceId(config.getServiceId()); | ||||||
|     request.setOutOrderNo(null); |     request.setOutOrderNo(null); | ||||||
|     String result = payService.postV3(url, GSON.toJson(request)); |     String result = payService.postV3(url, request.toJson()); | ||||||
|     return GSON.fromJson(result, WxPayScoreResult.class); |     return WxPayScoreResult.fromJson(result); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public PayScoreNotifyData parseNotifyData(String data) { |   public PayScoreNotifyData parseNotifyData(String data) { | ||||||
|     return GSON.fromJson(data, PayScoreNotifyData.class); |     return WxGsonBuilder.create().fromJson(data, PayScoreNotifyData.class); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -163,8 +161,7 @@ public class PayScoreServiceImpl implements PayScoreService { | |||||||
|     String nonce = resource.getNonce(); |     String nonce = resource.getNonce(); | ||||||
|     String apiV3Key = this.payService.getConfig().getApiV3Key(); |     String apiV3Key = this.payService.getConfig().getApiV3Key(); | ||||||
|     try { |     try { | ||||||
|       String s = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key); |       return WxPayScoreResult.fromJson(AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key)); | ||||||
|       return GSON.fromJson(s, WxPayScoreResult.class); |  | ||||||
|     } catch (GeneralSecurityException | IOException e) { |     } catch (GeneralSecurityException | IOException e) { | ||||||
|       throw new WxPayException("解析报文异常!", e); |       throw new WxPayException("解析报文异常!", e); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -25,6 +25,7 @@ import java.security.cert.CertificateExpiredException; | |||||||
| import java.security.cert.CertificateNotYetValidException; | import java.security.cert.CertificateNotYetValidException; | ||||||
| import java.security.cert.X509Certificate; | import java.security.cert.X509Certificate; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | import java.util.Collections; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.concurrent.locks.ReentrantLock; | import java.util.concurrent.locks.ReentrantLock; | ||||||
|  |  | ||||||
| @ -148,30 +149,32 @@ public class AutoUpdateCertificatesVerifier implements Verifier { | |||||||
|    * 反序列化证书并解密 |    * 反序列化证书并解密 | ||||||
|    */ |    */ | ||||||
|   private List<X509Certificate> deserializeToCerts(byte[] apiV3Key, String body) throws GeneralSecurityException, IOException { |   private List<X509Certificate> deserializeToCerts(byte[] apiV3Key, String body) throws GeneralSecurityException, IOException { | ||||||
|     AesUtils decryptor = new AesUtils(apiV3Key); |     AesUtils aesUtils = new AesUtils(apiV3Key); | ||||||
|     ObjectMapper mapper = new ObjectMapper(); |     ObjectMapper mapper = new ObjectMapper(); | ||||||
|     JsonNode dataNode = mapper.readTree(body).get("data"); |     JsonNode dataNode = mapper.readTree(body).get("data"); | ||||||
|     List<X509Certificate> newCertList = new ArrayList<>(); |     if (dataNode == null) { | ||||||
|     if (dataNode != null) { |       return Collections.emptyList(); | ||||||
|       for (int i = 0, count = dataNode.size(); i < count; i++) { |     } | ||||||
|         JsonNode encryptCertificateNode = dataNode.get(i).get("encrypt_certificate"); |  | ||||||
|         //解密 |  | ||||||
|         String cert = decryptor.decryptToString( |  | ||||||
|           encryptCertificateNode.get("associated_data").toString().replaceAll("\"", "") |  | ||||||
|             .getBytes(StandardCharsets.UTF_8), |  | ||||||
|           encryptCertificateNode.get("nonce").toString().replaceAll("\"", "") |  | ||||||
|             .getBytes(StandardCharsets.UTF_8), |  | ||||||
|           encryptCertificateNode.get("ciphertext").toString().replaceAll("\"", "")); |  | ||||||
|  |  | ||||||
|         X509Certificate x509Cert = PemUtils |     List<X509Certificate> newCertList = new ArrayList<>(); | ||||||
|           .loadCertificate(new ByteArrayInputStream(cert.getBytes(StandardCharsets.UTF_8))); |     for (int i = 0, count = dataNode.size(); i < count; i++) { | ||||||
|         try { |       JsonNode encryptCertificateNode = dataNode.get(i).get("encrypt_certificate"); | ||||||
|           x509Cert.checkValidity(); |       //解密 | ||||||
|         } catch (CertificateExpiredException | CertificateNotYetValidException e) { |       String cert = aesUtils.decryptToString( | ||||||
|           continue; |         encryptCertificateNode.get("associated_data").toString().replaceAll("\"", "") | ||||||
|         } |           .getBytes(StandardCharsets.UTF_8), | ||||||
|         newCertList.add(x509Cert); |         encryptCertificateNode.get("nonce").toString().replaceAll("\"", "") | ||||||
|  |           .getBytes(StandardCharsets.UTF_8), | ||||||
|  |         encryptCertificateNode.get("ciphertext").toString().replaceAll("\"", "")); | ||||||
|  |  | ||||||
|  |       X509Certificate x509Cert = PemUtils | ||||||
|  |         .loadCertificate(new ByteArrayInputStream(cert.getBytes(StandardCharsets.UTF_8))); | ||||||
|  |       try { | ||||||
|  |         x509Cert.checkValidity(); | ||||||
|  |       } catch (CertificateExpiredException | CertificateNotYetValidException e) { | ||||||
|  |         continue; | ||||||
|       } |       } | ||||||
|  |       newCertList.add(x509Cert); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return newCertList; |     return newCertList; | ||||||
|  | |||||||
| @ -0,0 +1,54 @@ | |||||||
|  | package com.github.binarywang.wxpay.bean.payscore; | ||||||
|  |  | ||||||
|  | import org.testng.annotations.Test; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||||
|  |  * @date 2020-07-11 | ||||||
|  |  */ | ||||||
|  | public class WxPayScoreRequestTest { | ||||||
|  |   @Test | ||||||
|  |   public void testToJson() { | ||||||
|  |     WxPayScoreRequest request = WxPayScoreRequest.builder() | ||||||
|  |       .outOrderNo("QLS202005201058000201") | ||||||
|  |       .appid("123") | ||||||
|  |       .serviceId("345") | ||||||
|  |       .serviceIntroduction("租借服务") | ||||||
|  |       .timeRange(new TimeRange("OnAccept", "20200520225840")) | ||||||
|  |       .build(); | ||||||
|  |     System.out.println(request.toJson()); | ||||||
|  |     /*    { | ||||||
|  |       "out_order_no":"QLS202005201058000201", | ||||||
|  |       "appid":"123", | ||||||
|  |       "service_id":"345", | ||||||
|  |       "service_introduction":"租借服务", | ||||||
|  |       "time_range":{ | ||||||
|  |       "start_time":"OnAccept", | ||||||
|  |         "end_time":"20200520225840" | ||||||
|  |     }, | ||||||
|  |       "location":{ | ||||||
|  |       "start_location":"山", | ||||||
|  |         "end_location":"山" | ||||||
|  |     }, | ||||||
|  |       "risk_fund":{ | ||||||
|  |       "name":"DEPOSIT", | ||||||
|  |         "amount":200, | ||||||
|  |         "description":"丢失偿还费用2元/台" | ||||||
|  |     }, | ||||||
|  |       "attach":"", | ||||||
|  |       "notify_url":"/pay/notify/payScore", | ||||||
|  |       "openid":"", | ||||||
|  |       "need_user_confirm":true, | ||||||
|  |       "profit_sharing":false, | ||||||
|  |       "post_payments":[ | ||||||
|  |       { | ||||||
|  |         "name":"租借服务", | ||||||
|  |         "amount":100, | ||||||
|  |         "description":"服务费:1元/台", | ||||||
|  |         "count":1 | ||||||
|  |       } | ||||||
|  |     ], | ||||||
|  |       "total_amount":0 | ||||||
|  |     }*/ | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -64,7 +64,7 @@ public class PayScoreServiceImplTest { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test |   @Test | ||||||
|   public void testQueryServiceOrder() throws URISyntaxException, WxPayException { |   public void testQueryServiceOrder() throws WxPayException { | ||||||
|     //两个参数选填一个 |     //两个参数选填一个 | ||||||
|     this.payService.getPayScoreService().queryServiceOrder("11", ""); |     this.payService.getPayScoreService().queryServiceOrder("11", ""); | ||||||
|   } |   } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang