支付宝和微信工具类参数从sysParam取出

This commit is contained in:
张松
2025-03-10 10:21:40 +08:00
parent d808e70eec
commit 8307f94913
2 changed files with 66 additions and 15 deletions

View File

@@ -6,8 +6,14 @@ import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient; import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest; import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse; import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.czg.resp.CzgResult;
import com.czg.system.dto.SysParamsDTO;
import com.czg.system.service.SysParamsService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -21,43 +27,67 @@ import org.springframework.stereotype.Component;
@Slf4j @Slf4j
@Component @Component
public class AlipayUtil { public class AlipayUtil {
@DubboReference
private SysParamsService sysParamsService;
/** /**
* 网关地址 线上https://openapi.alipay.com/gateway.do 沙箱https://openapi.alipaydev.com/gateway.do * 网关地址 线上https://openapi.alipay.com/gateway.do 沙箱https://openapi.alipaydev.com/gateway.do
*/ */
@Value("${alipay.serverUrl}") // @Value("${alipay.serverUrl}")
private String serverUrl; private String serverUrl;
/** /**
* 应用ID * 应用ID
*/ */
@Value("${alipay.appId}") // @Value("${alipay.appId}")
private String appId; private String appId;
/** /**
* 应用私钥 * 应用私钥
*/ */
@Value("${alipay.privateKey}") // @Value("${alipay.privateKey}")
private String privateKey; private String privateKey;
/** /**
* 支付宝公钥 * 支付宝公钥
*/ */
@Value("${alipay.alipayPublicKey}") // @Value("${alipay.alipayPublicKey}")
private String alipayPublicKey; private String alipayPublicKey;
/** /**
* 支付宝公钥 * 支付宝公钥
*/ */
@Value("${alipay.encryptKey}") // @Value("${alipay.encryptKey}")
private String encryptKey; private String encryptKey;
@Value("${alipay.account.appId}") // @Value("${alipay.account.appId}")
private String accountAppId; private String accountAppId;
@Value("${alipay.account.privateKey}") // @Value("${alipay.account.privateKey}")
private String accountPrivateKey; private String accountPrivateKey;
@Value("${alipay.account.publicKey}") // @Value("${alipay.account.publicKey}")
private String accountPublicKey; private String accountPublicKey;
@PostConstruct
private void init() {
CzgResult<SysParamsDTO> aliGateway = sysParamsService.getParamsByCode("ali_gateway");
CzgResult<SysParamsDTO> aliMiniAppId = sysParamsService.getParamsByCode("ali_mini_app_id");
CzgResult<SysParamsDTO> aliMiniPrivateKey = sysParamsService.getParamsByCode("ali_mini_private_key");
CzgResult<SysParamsDTO> aliMiniPublicKey = sysParamsService.getParamsByCode("ali_mini_public_key");
CzgResult<SysParamsDTO> aliAccountAppId = sysParamsService.getParamsByCode("ali_account_app_id");
CzgResult<SysParamsDTO> aliAccountPrivateKey = sysParamsService.getParamsByCode("ali_account_private_key");
CzgResult<SysParamsDTO> aliAccountPublicKey = sysParamsService.getParamsByCode("ali_account_public_key");
CzgResult<SysParamsDTO> aliEncryptKey = sysParamsService.getParamsByCode("ali_encrypt_key");
serverUrl = aliGateway.getData().getParamValue();
appId = aliMiniAppId.getData().getParamValue();
privateKey = aliMiniPrivateKey.getData().getParamValue();
alipayPublicKey = aliMiniPublicKey.getData().getParamValue();
encryptKey = aliEncryptKey.getData().getParamValue();
accountAppId = aliAccountAppId.getData().getParamValue();
accountPrivateKey = aliAccountPrivateKey.getData().getParamValue();
accountPublicKey = aliAccountPublicKey.getData().getParamValue();
log.info("支付宝工具类初始化成功, {}", this);
}
/** /**
* 创建支付宝客户端 * 创建支付宝客户端
*
* @return AlipayClient * @return AlipayClient
*/ */
@SneakyThrows @SneakyThrows
@@ -77,10 +107,11 @@ public class AlipayUtil {
/** /**
* 获取支付宝用户的openId * 获取支付宝用户的openId
* @param code 用户信息授权码 *
* @param code 用户信息授权码
* @return openId * @return openId
*/ */
public String getOpenId(String code, boolean isAccount){ public String getOpenId(String code, boolean isAccount) {
AlipaySystemOauthTokenRequest req = new AlipaySystemOauthTokenRequest(); AlipaySystemOauthTokenRequest req = new AlipaySystemOauthTokenRequest();
//SDK已经封装掉了公共参数这里只需要传入业务参数 //SDK已经封装掉了公共参数这里只需要传入业务参数
req.setCode(code); req.setCode(code);
@@ -90,7 +121,7 @@ public class AlipayUtil {
AlipaySystemOauthTokenResponse response; AlipaySystemOauthTokenResponse response;
try { try {
response = createClient(isAccount).execute(req); response = createClient(isAccount).execute(req);
}catch (Exception e){ } catch (Exception e) {
log.error("获取支付宝用户信息失败", e); log.error("获取支付宝用户信息失败", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -4,7 +4,12 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.czg.resp.CzgResult;
import com.czg.system.dto.SysParamsDTO;
import com.czg.system.service.SysParamsService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -18,15 +23,17 @@ import java.util.Map;
@Slf4j @Slf4j
@Component @Component
public class WechatAuthUtil { public class WechatAuthUtil {
@DubboReference
private SysParamsService sysParamsService;
@Value("${wx.appId}") // @Value("${wx.appId}")
private String appId; private String appId;
@Value("${wx.secrete}") // @Value("${wx.secrete}")
private String secrete; private String secrete;
@Value("${wx.account.appId}") // @Value("${wx.account.appId}")
private String accountAppId; private String accountAppId;
@Value("${wx.account.secrete}") // @Value("${wx.account.secrete}")
private String accountSecrete; private String accountSecrete;
@@ -47,6 +54,19 @@ public class WechatAuthUtil {
} }
@PostConstruct
public void init() {
CzgResult<SysParamsDTO> wxMiniAppId = sysParamsService.getParamsByCode("wx_mini_app_id");
CzgResult<SysParamsDTO> wxMiniSecrete = sysParamsService.getParamsByCode("wx_mini_secrete");
CzgResult<SysParamsDTO> wxAccountAppId = sysParamsService.getParamsByCode("wx_account_app_id");
CzgResult<SysParamsDTO> wxAccountSecrete = sysParamsService.getParamsByCode("wx_account_secrete");
appId = wxMiniAppId.getData().getParamValue();
secrete = wxMiniSecrete.getData().getParamValue();
accountAppId = wxAccountAppId.getData().getParamValue();
accountSecrete = wxAccountSecrete.getData().getParamValue();
log.info("微信工具类初始化成功, appId: {}, secrete: {}, accountAppId: {}, accountSecrete: {}", appId, secrete, accountAppId, accountSecrete);
}
public String getAccountOpenId(String code) { public String getAccountOpenId(String code) {
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?"; String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?";
Map<String, Object> requestUrlParam = new HashMap<>(); Map<String, Object> requestUrlParam = new HashMap<>();