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

View File

@@ -23,4 +23,8 @@ public class AlipayLoginDTO extends BaseDTO{
private String authCode; private String authCode;
private String accessToken; private String accessToken;
private String openId;
private String mobileNumber;
} }