@@ -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 . getM obileNumberCiphertext( ) ) ;
/**
* 支付宝 通过密文解密 获取手机号码
* @param m obileNumberCiphertext
* @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启动订单 或 卡启动订单