From 2dcb3a273ed6b019692162072551fcdf51c4c6cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Thu, 20 Mar 2025 09:17:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../czg/service/account/util/AlipayUtil.java | 88 +++++-------------- .../service/account/util/WechatAuthUtil.java | 40 +++------ 2 files changed, 36 insertions(+), 92 deletions(-) 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 b07bf7a8c..cbe452409 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 @@ -10,16 +10,12 @@ import com.alipay.api.DefaultAlipayClient; import com.alipay.api.internal.util.AlipayEncrypt; import com.alipay.api.request.AlipaySystemOauthTokenRequest; import com.alipay.api.response.AlipaySystemOauthTokenResponse; -import com.czg.exception.ApiNotPrintException; 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; /** @@ -35,61 +31,6 @@ public class AlipayUtil { @DubboReference private SysParamsService sysParamsService; - /** - * 网关地址 线上:https://openapi.alipay.com/gateway.do 沙箱:https://openapi.alipaydev.com/gateway.do - */ -// @Value("${alipay.serverUrl}") - private String serverUrl; - /** - * 应用ID - */ -// @Value("${alipay.appId}") - private String appId; - - /** - * 应用私钥 - */ -// @Value("${alipay.privateKey}") - private String privateKey; - /** - * 支付宝公钥 - */ -// @Value("${alipay.alipayPublicKey}") - private String alipayPublicKey; - /** - * 支付宝公钥 - */ -// @Value("${alipay.encryptKey}") - private String encryptKey; - -// @Value("${alipay.account.appId}") - private String accountAppId; -// @Value("${alipay.account.privateKey}") - private String accountPrivateKey; -// @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); -// } - /** * 创建支付宝客户端 * @@ -97,6 +38,20 @@ public class AlipayUtil { */ @SneakyThrows public AlipayClient createClient(boolean isAccount) { + 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"); + String serverUrl = aliGateway.getData().getParamValue(); + String appId = aliMiniAppId.getData().getParamValue(); + String privateKey = aliMiniPrivateKey.getData().getParamValue(); + String alipayPublicKey = aliMiniPublicKey.getData().getParamValue(); + String accountAppId = aliAccountAppId.getData().getParamValue(); + String accountPrivateKey = aliAccountPrivateKey.getData().getParamValue(); + String accountPublicKey = aliAccountPublicKey.getData().getParamValue(); AlipayConfig alipayConfig = new AlipayConfig(); //设置网关地址 alipayConfig.setServerUrl(serverUrl); @@ -141,27 +96,30 @@ public class AlipayUtil { } public String getMobile(String encryptedData) { - if(StrUtil.isEmpty(encryptedData)){ + if (StrUtil.isEmpty(encryptedData)) { throw new RuntimeException("加密数据不能为空"); } try { + CzgResult aliEncryptKey = sysParamsService.getParamsByCode("ali_encrypt_key"); + String encryptKey = aliEncryptKey.getData().getParamValue(); + log.info("解密前的数据,返回结果:{}", encryptedData); String resp = AlipayEncrypt.decryptContent(encryptedData, "AES", encryptKey, "UTF-8"); log.info("解密后的数据,返回结果:{}", resp); boolean isJson = JSONUtil.isTypeJSON(resp); - if(!isJson){ + if (!isJson) { throw new AlipayApiException("解密后的数据不是json格式"); } JSONObject jsonObject = JSONUtil.parseObj(resp); String code = jsonObject.getStr("code"); String msg = jsonObject.getStr("msg"); String mobile = jsonObject.getStr("mobile"); - if("10000".equals(code)){ + if ("10000".equals(code)) { return mobile; - }else{ - throw new AlipayApiException(code,msg); + } else { + throw new AlipayApiException(code, msg); } - }catch (AlipayApiException e){ + } catch (AlipayApiException e) { log.error("获取支付宝用户的手机号码失败,错误码:{},错误信息:{}", e.getErrCode(), e.getErrMsg()); 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 6af586598..ede411b10 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 @@ -13,7 +13,6 @@ import com.czg.system.dto.SysParamsDTO; import com.czg.system.enums.SysParamCodeEnum; import com.czg.system.service.SysParamsService; import com.google.gson.JsonObject; -import jakarta.annotation.PostConstruct; import jakarta.annotation.Resource; import jakarta.validation.constraints.NotBlank; import lombok.extern.slf4j.Slf4j; @@ -38,16 +37,6 @@ public class WechatAuthUtil { @Resource private AliOssUtil aliOssUtil; - // @Value("${wx.appId}") - private String appId; - // @Value("${wx.secrete}") - private String secrete; - - // @Value("${wx.account.appId}") - private String accountAppId; - // @Value("${wx.account.secrete}") - private String accountSecrete; - private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"; private static final String QR_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode"; @@ -68,20 +57,11 @@ 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) { + CzgResult wxAccountAppId = sysParamsService.getParamsByCode("wx_account_app_id"); + CzgResult wxAccountSecrete = sysParamsService.getParamsByCode("wx_account_secrete"); + String accountAppId = wxAccountAppId.getData().getParamValue(); + String accountSecrete = wxAccountSecrete.getData().getParamValue(); String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?"; Map requestUrlParam = new HashMap<>(); // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN @@ -102,6 +82,10 @@ public class WechatAuthUtil { //获取小程序token private String getAccessToken() { + CzgResult wxMiniAppId = sysParamsService.getParamsByCode("wx_mini_app_id"); + CzgResult wxMiniSecrete = sysParamsService.getParamsByCode("wx_mini_secrete"); + String appId = wxMiniAppId.getData().getParamValue(); + String secrete = wxMiniSecrete.getData().getParamValue(); String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", TOKEN_URL, appId, secrete); String response = HttpUtil.get(url); com.alibaba.fastjson.JSONObject jsonResponse = com.alibaba.fastjson.JSONObject.parseObject(response); @@ -114,8 +98,6 @@ public class WechatAuthUtil { /** * 生成 小程序码 跳转对应页面 * - * @param params - * @return */ public String getFetchQrCode(Map params) throws Exception { String url = aliOssUtil.upload(fetchQrCode(params), aliOssUtil.getPath("shopVip", "png")); @@ -127,7 +109,7 @@ public class WechatAuthUtil { private InputStream fetchQrCode(Map params) { JsonObject jsonObject = new JsonObject(); //路径 - jsonObject.addProperty("path", sysParamsService.getSysParamValue(SysParamCodeEnum.WX_MINI_VIP_URL.getCode())+"?shopId="+params.get("shopId")); + jsonObject.addProperty("path", sysParamsService.getSysParamValue(SysParamCodeEnum.WX_MINI_VIP_URL.getCode()) + "?shopId=" + params.get("shopId")); //是否需要透明底色,为 true 时,生成透明底色的小程序码 jsonObject.addProperty("is_hyaline", true); //正式版为 release,体验版为 trial,开发版为 develop @@ -144,6 +126,10 @@ public class WechatAuthUtil { public JSONObject getSession(String code) { + CzgResult wxMiniSecrete = sysParamsService.getParamsByCode("wx_mini_secrete"); + CzgResult wxMiniAppId = sysParamsService.getParamsByCode("wx_mini_app_id"); + String appId = wxMiniAppId.getData().getParamValue(); + String secrete = wxMiniSecrete.getData().getParamValue(); String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"; Map requestUrlParam = new HashMap<>(); // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN