改用单例的方式 初始化支付宝小程序

pull/26/head
Dingxindong 2 years ago
parent c6f02392cb
commit 350e969afb

@ -8,6 +8,7 @@ import com.alipay.api.domain.AlipayOpenAppMiniTemplatemessageSendModel;
import com.alipay.api.request.AlipayOpenAppMiniTemplatemessageSendRequest;
import com.java3y.austin.common.dto.account.AlipayMiniProgramAccount;
import com.java3y.austin.handler.alipay.AlipayMiniProgramAccountService;
import com.java3y.austin.handler.config.AlipayClientSingleton;
import com.java3y.austin.handler.domain.alipay.AlipayMiniProgramParam;
import com.java3y.austin.support.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
@ -39,7 +40,7 @@ public class AlipayMiniProgramAccountServiceImpl implements AlipayMiniProgramAcc
public void send(AlipayMiniProgramParam miniProgramParam) throws AlipayApiException {
AlipayMiniProgramAccount miniProgramAccount = accountUtils.getAccountById(miniProgramParam.getSendAccount(), AlipayMiniProgramAccount.class);
AlipayClient client = initService(miniProgramAccount);
AlipayClient client = AlipayClientSingleton.getSingleton(miniProgramAccount);
List<AlipayOpenAppMiniTemplatemessageSendRequest> request = assembleReq(miniProgramParam, miniProgramAccount);
for (AlipayOpenAppMiniTemplatemessageSendRequest req : request) {
client.execute(req);
@ -66,20 +67,20 @@ public class AlipayMiniProgramAccountServiceImpl implements AlipayMiniProgramAcc
return requestList;
}
/**
*
*/
private AlipayClient initService(AlipayMiniProgramAccount alipayMiniProgramAccount) throws AlipayApiException {
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
alipayConfig.setAppId(alipayMiniProgramAccount.getAppId());
alipayConfig.setPrivateKey(alipayMiniProgramAccount.getPrivateKey());
alipayConfig.setFormat("json");
alipayConfig.setAlipayPublicKey(alipayMiniProgramAccount.getAlipayPublicKey());
alipayConfig.setCharset("utf-8");
alipayConfig.setSignType("RSA2");
return new DefaultAlipayClient(alipayConfig);
}
// /**
// * 初始化支付宝小程序
// */
// private AlipayClient initService(AlipayMiniProgramAccount alipayMiniProgramAccount) throws AlipayApiException {
// AlipayConfig alipayConfig = new AlipayConfig();
// alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
// alipayConfig.setAppId(alipayMiniProgramAccount.getAppId());
// alipayConfig.setPrivateKey(alipayMiniProgramAccount.getPrivateKey());
// alipayConfig.setFormat("json");
// alipayConfig.setAlipayPublicKey(alipayMiniProgramAccount.getAlipayPublicKey());
// alipayConfig.setCharset("utf-8");
// alipayConfig.setSignType("RSA2");
// return new DefaultAlipayClient(alipayConfig);
// }
}

@ -0,0 +1,39 @@
package com.java3y.austin.handler.config;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.java3y.austin.common.dto.account.AlipayMiniProgramAccount;
/**
*
*
* @author
* @date 2022-12-07
*/
public class AlipayClientSingleton {
private volatile static DefaultAlipayClient alipayClientSingleton;
private AlipayClientSingleton() {
}
public static DefaultAlipayClient getSingleton(AlipayMiniProgramAccount alipayMiniProgramAccount) throws AlipayApiException {
if (alipayClientSingleton == null) {
synchronized (DefaultAlipayClient.class) {
if (alipayClientSingleton == null) {
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
alipayConfig.setAppId(alipayMiniProgramAccount.getAppId());
alipayConfig.setPrivateKey(alipayMiniProgramAccount.getPrivateKey());
alipayConfig.setFormat("json");
alipayConfig.setAlipayPublicKey(alipayMiniProgramAccount.getAlipayPublicKey());
alipayConfig.setCharset("utf-8");
alipayConfig.setSignType("RSA2");
alipayClientSingleton = new DefaultAlipayClient(alipayConfig);
}
}
}
return alipayClientSingleton;
}
}
Loading…
Cancel
Save