工具类修改

This commit is contained in:
张松
2025-03-20 09:17:31 +08:00
parent 8dfc386c61
commit 2dcb3a273e
2 changed files with 36 additions and 92 deletions

View File

@@ -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<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);
// }
/**
* 创建支付宝客户端
*
@@ -97,6 +38,20 @@ public class AlipayUtil {
*/
@SneakyThrows
public AlipayClient createClient(boolean isAccount) {
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");
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<SysParamsDTO> 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);
}

View File

@@ -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<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) {
CzgResult<SysParamsDTO> wxAccountAppId = sysParamsService.getParamsByCode("wx_account_app_id");
CzgResult<SysParamsDTO> 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<String, Object> 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<SysParamsDTO> wxMiniAppId = sysParamsService.getParamsByCode("wx_mini_app_id");
CzgResult<SysParamsDTO> 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<String, Object> params) throws Exception {
String url = aliOssUtil.upload(fetchQrCode(params), aliOssUtil.getPath("shopVip", "png"));
@@ -127,7 +109,7 @@ public class WechatAuthUtil {
private InputStream fetchQrCode(Map<String, Object> 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<SysParamsDTO> wxMiniSecrete = sysParamsService.getParamsByCode("wx_mini_secrete");
CzgResult<SysParamsDTO> 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<String, Object> requestUrlParam = new HashMap<>();
// https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN