diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/util/AlipayUtil.java b/cash-service/account-service/src/main/java/com/czg/service/account/util/AlipayUtil.java index 7c7906343..380bf07a0 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/util/AlipayUtil.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/util/AlipayUtil.java @@ -6,8 +6,14 @@ import com.alipay.api.AlipayConfig; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipaySystemOauthTokenRequest; 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.extern.slf4j.Slf4j; +import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -21,43 +27,67 @@ import org.springframework.stereotype.Component; @Slf4j @Component public class AlipayUtil { + @DubboReference + private SysParamsService sysParamsService; /** * 网关地址 线上:https://openapi.alipay.com/gateway.do 沙箱:https://openapi.alipaydev.com/gateway.do */ - @Value("${alipay.serverUrl}") +// @Value("${alipay.serverUrl}") private String serverUrl; /** * 应用ID */ - @Value("${alipay.appId}") +// @Value("${alipay.appId}") private String appId; /** * 应用私钥 */ - @Value("${alipay.privateKey}") +// @Value("${alipay.privateKey}") private String privateKey; /** * 支付宝公钥 */ - @Value("${alipay.alipayPublicKey}") +// @Value("${alipay.alipayPublicKey}") private String alipayPublicKey; /** * 支付宝公钥 */ - @Value("${alipay.encryptKey}") +// @Value("${alipay.encryptKey}") private String encryptKey; - @Value("${alipay.account.appId}") +// @Value("${alipay.account.appId}") private String accountAppId; - @Value("${alipay.account.privateKey}") +// @Value("${alipay.account.privateKey}") private String accountPrivateKey; - @Value("${alipay.account.publicKey}") +// @Value("${alipay.account.publicKey}") private String accountPublicKey; + @PostConstruct + private void init() { + CzgResult aliGateway = sysParamsService.getParamsByCode("ali_gateway"); + CzgResult aliMiniAppId = sysParamsService.getParamsByCode("ali_mini_app_id"); + CzgResult aliMiniPrivateKey = sysParamsService.getParamsByCode("ali_mini_private_key"); + CzgResult aliMiniPublicKey = sysParamsService.getParamsByCode("ali_mini_public_key"); + CzgResult aliAccountAppId = sysParamsService.getParamsByCode("ali_account_app_id"); + CzgResult aliAccountPrivateKey = sysParamsService.getParamsByCode("ali_account_private_key"); + CzgResult aliAccountPublicKey = sysParamsService.getParamsByCode("ali_account_public_key"); + CzgResult 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 */ @SneakyThrows @@ -77,10 +107,11 @@ public class AlipayUtil { /** * 获取支付宝用户的openId - * @param code 用户信息授权码 + * + * @param code 用户信息授权码 * @return openId */ - public String getOpenId(String code, boolean isAccount){ + public String getOpenId(String code, boolean isAccount) { AlipaySystemOauthTokenRequest req = new AlipaySystemOauthTokenRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数 req.setCode(code); @@ -90,7 +121,7 @@ public class AlipayUtil { AlipaySystemOauthTokenResponse response; try { response = createClient(isAccount).execute(req); - }catch (Exception e){ + } catch (Exception e) { log.error("获取支付宝用户信息失败", e); throw new RuntimeException(e); } diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java b/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java index 57ef79b67..4d223308d 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/util/WechatAuthUtil.java @@ -4,7 +4,12 @@ import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson2.JSON; 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 org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -18,15 +23,17 @@ import java.util.Map; @Slf4j @Component public class WechatAuthUtil { + @DubboReference + private SysParamsService sysParamsService; - @Value("${wx.appId}") +// @Value("${wx.appId}") private String appId; - @Value("${wx.secrete}") +// @Value("${wx.secrete}") private String secrete; - @Value("${wx.account.appId}") +// @Value("${wx.account.appId}") private String accountAppId; - @Value("${wx.account.secrete}") +// @Value("${wx.account.secrete}") private String accountSecrete; @@ -47,6 +54,19 @@ public class WechatAuthUtil { } + @PostConstruct + public void init() { + CzgResult wxMiniAppId = sysParamsService.getParamsByCode("wx_mini_app_id"); + CzgResult wxMiniSecrete = sysParamsService.getParamsByCode("wx_mini_secrete"); + CzgResult wxAccountAppId = sysParamsService.getParamsByCode("wx_account_app_id"); + CzgResult 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) { String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?"; Map requestUrlParam = new HashMap<>();