update 支付宝登录

This commit is contained in:
Guoqs
2024-06-26 10:37:59 +08:00
parent fd7b2d5260
commit 805eb5b9ee
8 changed files with 164 additions and 33 deletions

View File

@@ -4,6 +4,12 @@ import cn.hutool.core.util.PageUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.easysdk.factory.Factory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@@ -14,6 +20,7 @@ import com.huifu.adapay.model.Payment;
import com.jsowell.adapay.common.CreateAdaPaymentParam;
import com.jsowell.adapay.config.AbstractAdapayConfig;
import com.jsowell.adapay.factory.AdapayConfigFactory;
import com.jsowell.alipay.factory.AlipayClientFactory;
import com.jsowell.alipay.service.AliAppletRemoteService;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
@@ -112,6 +119,13 @@ public class MemberService {
@Value("${weixin.login.appid}")
private String APP_ID;
private final AlipayClient alipayClient;
@Autowired
public MemberService(AlipayClientFactory alipayClientFactory) {
this.alipayClient = alipayClientFactory.getAlipayClient();
}
/**
* 校验短信验证码
* @param dto
@@ -147,6 +161,7 @@ public class MemberService {
String phoneNumber = dto.getMobileNumber();
String firstLevelMerchantId = dto.getFirstLevelMerchantId();
String openId = dto.getOpenId();
String buyerId = dto.getBuyerId();
log.info("公共登录注册方法, phoneNumber:{}, firstLevelMerchantId:{}, openId:{}", phoneNumber, firstLevelMerchantId, openId);
if (StringUtils.isBlank(phoneNumber)) {
@@ -176,6 +191,9 @@ public class MemberService {
if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && StringUtils.isNotBlank(openId)) {
memberBasicInfo.setOpenId(openId);
}
if (AdapayPayChannelEnum.ALIPAY_LITE.getValue().equals(dto.getRequestSource()) && StringUtils.isNotBlank(buyerId)) {
memberBasicInfo.setBuyerId(buyerId);
}
MemberTransactionDTO memberTransactionDTO = new MemberTransactionDTO();
memberTransactionDTO.setMemberBasicInfo(memberBasicInfo);
@@ -190,9 +208,17 @@ public class MemberService {
}
transactionService.createMember(memberTransactionDTO);
} else {
boolean updateFlag = false;
if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && !StringUtils.equals(memberBasicInfo.getOpenId(), openId)) {
// openId变化就更新
memberBasicInfo.setOpenId(openId);
updateFlag = true;
}
if (AdapayPayChannelEnum.ALIPAY_LITE.getValue().equals(dto.getRequestSource()) && !StringUtils.equals(memberBasicInfo.getBuyerId(), buyerId)) {
memberBasicInfo.setBuyerId(buyerId);
updateFlag = true;
}
if (updateFlag) {
memberBasicInfoService.updateMemberBasicInfo(memberBasicInfo);
}
}
@@ -242,12 +268,31 @@ public class MemberService {
return memberRegisterAndLogin(loginDTO); // 微信小程序一键登录
}
public String alipayLogin(AlipayLoginDTO dto) throws Exception {
// 通过密文解密 获取手机号码
String decrypt = Factory.Util.AES().decrypt(dto.getMobileNumberCiphertext());
/**
* 支付宝 通过密文解密 获取手机号码
* @param mobileNumberCiphertext
* @return
* @throws Exception
*/
private String decryptMobileNumberCiphertext(String mobileNumberCiphertext) throws Exception {
String decrypt = Factory.Util.AES().decrypt(mobileNumberCiphertext);
String mobileNumber = JSON.parseObject(decrypt).getString("mobile");
return mobileNumber;
}
public String alipayLogin(AlipayLoginDTO dto) throws Exception {
String accessToken = getAccessToken(dto.getAuthCode());
// 获取buyer_id
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken);
if(response.isSuccess()){
log.info("调用成功:" + JSON.toJSONString(response));
} else {
log.info("调用失败:" + JSON.toJSONString(response));
}
String userId = response.getUserId();
String mobile = response.getMobile();
// 根据appid查询merchantId
String firstLevelMerchantId = pileMerchantInfoService.getFirstLevelMerchantIdByAliAppId(dto.getAppId());
@@ -256,11 +301,37 @@ public class MemberService {
MemberRegisterAndLoginDTO loginDTO = MemberRegisterAndLoginDTO.builder()
.requestSource(dto.getRequestSource())
.firstLevelMerchantId(firstLevelMerchantId)
.mobileNumber(mobileNumber)
.mobileNumber(mobile)
.buyerId(userId)
.build();
return memberRegisterAndLogin(loginDTO); // 支付宝小程序一键登录
}
/**
* 获取 auth_code用户授权码后应尽快调用 alipay.system.oauth.token换取授权访问令牌接口换取 access_token访问令牌
* @param authCode
* @return
*/
private String getAccessToken(String authCode) {
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
// 设置授权码
request.setCode(authCode);
// 设置授权方式
request.setGrantType("authorization_code");
AlipaySystemOauthTokenResponse response = null;
try {
response = alipayClient.execute(request);
} catch (AlipayApiException e) {
throw new RuntimeException(e);
}
String accessToken = "";
if (response.isSuccess()) {
System.out.println("调用成功");
accessToken = response.getAccessToken();
}
return accessToken;
}
/**
* 获取openId
* @param code
@@ -717,8 +788,6 @@ public class MemberService {
/**
* 充值订单金额
* @param dto
* @throws ParseException
*/
// public void rechargeOrderAmount(RechargeOrderAmountDTO dto) throws ParseException {
// // 根据memberId查询出当前用户正在充电的 vin启动订单 或 卡启动订单

View File

@@ -212,3 +212,5 @@ alipay:
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2

View File

@@ -206,4 +206,6 @@ alipay:
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数必须外网可以正常访问
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2

View File

@@ -206,4 +206,6 @@ alipay:
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数必须外网可以正常访问
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2

View File

@@ -209,4 +209,6 @@ alipay:
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数必须外网可以正常访问
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2