工具类修改
This commit is contained in:
@@ -10,16 +10,12 @@ import com.alipay.api.DefaultAlipayClient;
|
|||||||
import com.alipay.api.internal.util.AlipayEncrypt;
|
import com.alipay.api.internal.util.AlipayEncrypt;
|
||||||
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.exception.ApiNotPrintException;
|
|
||||||
import com.czg.resp.CzgResult;
|
import com.czg.resp.CzgResult;
|
||||||
import com.czg.system.dto.SysParamsDTO;
|
import com.czg.system.dto.SysParamsDTO;
|
||||||
import com.czg.system.service.SysParamsService;
|
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.apache.dubbo.config.annotation.DubboReference;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,61 +31,6 @@ public class AlipayUtil {
|
|||||||
@DubboReference
|
@DubboReference
|
||||||
private SysParamsService sysParamsService;
|
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
|
@SneakyThrows
|
||||||
public AlipayClient createClient(boolean isAccount) {
|
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 alipayConfig = new AlipayConfig();
|
||||||
//设置网关地址
|
//设置网关地址
|
||||||
alipayConfig.setServerUrl(serverUrl);
|
alipayConfig.setServerUrl(serverUrl);
|
||||||
@@ -141,27 +96,30 @@ public class AlipayUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getMobile(String encryptedData) {
|
public String getMobile(String encryptedData) {
|
||||||
if(StrUtil.isEmpty(encryptedData)){
|
if (StrUtil.isEmpty(encryptedData)) {
|
||||||
throw new RuntimeException("加密数据不能为空");
|
throw new RuntimeException("加密数据不能为空");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
CzgResult<SysParamsDTO> aliEncryptKey = sysParamsService.getParamsByCode("ali_encrypt_key");
|
||||||
|
String encryptKey = aliEncryptKey.getData().getParamValue();
|
||||||
|
|
||||||
log.info("解密前的数据,返回结果:{}", encryptedData);
|
log.info("解密前的数据,返回结果:{}", encryptedData);
|
||||||
String resp = AlipayEncrypt.decryptContent(encryptedData, "AES", encryptKey, "UTF-8");
|
String resp = AlipayEncrypt.decryptContent(encryptedData, "AES", encryptKey, "UTF-8");
|
||||||
log.info("解密后的数据,返回结果:{}", resp);
|
log.info("解密后的数据,返回结果:{}", resp);
|
||||||
boolean isJson = JSONUtil.isTypeJSON(resp);
|
boolean isJson = JSONUtil.isTypeJSON(resp);
|
||||||
if(!isJson){
|
if (!isJson) {
|
||||||
throw new AlipayApiException("解密后的数据不是json格式");
|
throw new AlipayApiException("解密后的数据不是json格式");
|
||||||
}
|
}
|
||||||
JSONObject jsonObject = JSONUtil.parseObj(resp);
|
JSONObject jsonObject = JSONUtil.parseObj(resp);
|
||||||
String code = jsonObject.getStr("code");
|
String code = jsonObject.getStr("code");
|
||||||
String msg = jsonObject.getStr("msg");
|
String msg = jsonObject.getStr("msg");
|
||||||
String mobile = jsonObject.getStr("mobile");
|
String mobile = jsonObject.getStr("mobile");
|
||||||
if("10000".equals(code)){
|
if ("10000".equals(code)) {
|
||||||
return mobile;
|
return mobile;
|
||||||
}else{
|
} else {
|
||||||
throw new AlipayApiException(code,msg);
|
throw new AlipayApiException(code, msg);
|
||||||
}
|
}
|
||||||
}catch (AlipayApiException e){
|
} catch (AlipayApiException e) {
|
||||||
log.error("获取支付宝用户的手机号码失败,错误码:{},错误信息:{}", e.getErrCode(), e.getErrMsg());
|
log.error("获取支付宝用户的手机号码失败,错误码:{},错误信息:{}", e.getErrCode(), e.getErrMsg());
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import com.czg.system.dto.SysParamsDTO;
|
|||||||
import com.czg.system.enums.SysParamCodeEnum;
|
import com.czg.system.enums.SysParamCodeEnum;
|
||||||
import com.czg.system.service.SysParamsService;
|
import com.czg.system.service.SysParamsService;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -38,16 +37,6 @@ public class WechatAuthUtil {
|
|||||||
@Resource
|
@Resource
|
||||||
private AliOssUtil aliOssUtil;
|
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 TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
|
||||||
private static final String QR_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode";
|
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) {
|
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?";
|
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?";
|
||||||
Map<String, Object> requestUrlParam = new HashMap<>();
|
Map<String, Object> requestUrlParam = new HashMap<>();
|
||||||
// https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
|
// https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
|
||||||
@@ -102,6 +82,10 @@ public class WechatAuthUtil {
|
|||||||
|
|
||||||
//获取小程序token
|
//获取小程序token
|
||||||
private String getAccessToken() {
|
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 url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", TOKEN_URL, appId, secrete);
|
||||||
String response = HttpUtil.get(url);
|
String response = HttpUtil.get(url);
|
||||||
com.alibaba.fastjson.JSONObject jsonResponse = com.alibaba.fastjson.JSONObject.parseObject(response);
|
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 {
|
public String getFetchQrCode(Map<String, Object> params) throws Exception {
|
||||||
String url = aliOssUtil.upload(fetchQrCode(params), aliOssUtil.getPath("shopVip", "png"));
|
String url = aliOssUtil.upload(fetchQrCode(params), aliOssUtil.getPath("shopVip", "png"));
|
||||||
@@ -127,7 +109,7 @@ public class WechatAuthUtil {
|
|||||||
private InputStream fetchQrCode(Map<String, Object> params) {
|
private InputStream fetchQrCode(Map<String, Object> params) {
|
||||||
JsonObject jsonObject = new JsonObject();
|
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 时,生成透明底色的小程序码
|
//是否需要透明底色,为 true 时,生成透明底色的小程序码
|
||||||
jsonObject.addProperty("is_hyaline", true);
|
jsonObject.addProperty("is_hyaline", true);
|
||||||
//正式版为 release,体验版为 trial,开发版为 develop
|
//正式版为 release,体验版为 trial,开发版为 develop
|
||||||
@@ -144,6 +126,10 @@ public class WechatAuthUtil {
|
|||||||
|
|
||||||
|
|
||||||
public JSONObject getSession(String code) {
|
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";
|
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
||||||
Map<String, Object> requestUrlParam = new HashMap<>();
|
Map<String, Object> requestUrlParam = new HashMap<>();
|
||||||
// https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
|
// https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
|
||||||
|
|||||||
Reference in New Issue
Block a user