update 支付宝登录

This commit is contained in:
Guoqs
2024-06-26 16:53:35 +08:00
parent 5da14138db
commit f6ae8f3b23
2 changed files with 41 additions and 24 deletions

View File

@@ -272,30 +272,39 @@ public class MemberService {
/**
* 支付宝 通过密文解密 获取手机号码
* @param mobileNumberCiphertext
* @return
* @throws Exception
*/
private String decryptMobileNumberCiphertext(String mobileNumberCiphertext) throws Exception {
String decrypt = Factory.Util.AES().decrypt(mobileNumberCiphertext);
// private String decryptMobileNumberCiphertext(String mobileNumberCiphertext) throws Exception {
// String decrypt = Factory.Util.AES().decrypt(mobileNumberCiphertext);
// String mobileNumber = JSON.parseObject(decrypt).getString("mobile");
// return mobileNumber;
// }
/**
* 支付宝 通过密文解密 获取手机号码
*/
private void decryptMobileNumberCiphertext(AlipayLoginDTO dto) throws Exception {
String decrypt = Factory.Util.AES().decrypt(dto.getMobileNumberCiphertext());
String mobileNumber = JSON.parseObject(decrypt).getString("mobile");
return mobileNumber;
dto.setMobileNumber(mobileNumber);
}
public String alipayLogin(AlipayLoginDTO dto) throws Exception {
String accessToken = getAccessToken(dto.getAuthCode());
// 获取AccessToken
getAccessToken(dto);
// 解密手机号
decryptMobileNumberCiphertext(dto);
// 获取buyer_id
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
// AlipayUserUserinfoShareRequest request = new AlipayUserUserinfoShareRequest();
AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken);
if(response.isSuccess()){
log.info("调用成功:" + JSON.toJSONString(response));
} else {
log.info("调用失败:" + JSON.toJSONString(response));
}
String userId = response.getOpenId();
String mobile = response.getMobile();
// AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
// AlipayUserInfoShareResponse response = alipayClient.execute(request, dto.getAccessToken());
// if(response.isSuccess()){
// log.info("调用成功:" + JSON.toJSONString(response));
// } else {
// log.info("调用失败:" + JSON.toJSONString(response));
// }
// String userId = response.getOpenId();
// String mobile = response.getMobile();
// 根据appid查询merchantId
String firstLevelMerchantId = pileMerchantInfoService.getFirstLevelMerchantIdByAliAppId(dto.getAppId());
@@ -304,21 +313,22 @@ public class MemberService {
MemberRegisterAndLoginDTO loginDTO = MemberRegisterAndLoginDTO.builder()
.requestSource(dto.getRequestSource())
.firstLevelMerchantId(firstLevelMerchantId)
.mobileNumber(mobile)
.buyerId(userId)
.mobileNumber(dto.getMobileNumber())
.buyerId(dto.getOpenId())
.build();
return memberRegisterAndLogin(loginDTO); // 支付宝小程序一键登录
}
/**
* 获取 auth_code用户授权码后应尽快调用 alipay.system.oauth.token换取授权访问令牌接口换取 access_token访问令牌
* @param authCode
* @return
*/
private String getAccessToken(String authCode) throws AlipayApiException {
private void getAccessToken(AlipayLoginDTO dto) throws AlipayApiException {
if (StringUtils.isBlank(dto.getAuthCode())) {
log.info("换取授权访问令牌接口, authCode为空");
}
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
// 设置授权码
request.setCode(authCode);
request.setCode(dto.getAuthCode());
// 设置授权方式
request.setGrantType("authorization_code");
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
@@ -326,7 +336,10 @@ public class MemberService {
throw new BusinessException(ReturnCodeEnum.CODE_ALIPAY_ACCESS_TOKEN_ERROR);
}
log.info("支付宝getAccessToken:{}", JSON.toJSONString(response));
return response.getAccessToken();
String accessToken = response.getAccessToken();
dto.setAccessToken(accessToken);
String openId = response.getOpenId();
dto.setOpenId(openId);
}
/**