小程序获取手机号接口

This commit is contained in:
张松 2025-03-10 13:40:42 +08:00
parent 0e5b2ecfb6
commit 79e0163e68
1 changed files with 12 additions and 6 deletions

View File

@ -2,10 +2,8 @@ package com.czg.service.account.service.impl;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.czg.account.dto.auth.GetPhoneDTO;
import com.czg.account.dto.auth.LoginTokenDTO;
import com.czg.account.dto.auth.UserAuthorizationLoginDTO;
@ -22,7 +20,6 @@ import com.czg.sa.StpKit;
import com.czg.service.account.util.AlipayUtil;
import com.czg.service.account.util.WechatAuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -56,16 +53,20 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService {
public String getPhone(GetPhoneDTO phoneDTO) {
String mobile;
String openId;
UserInfo userInfo;
UserInfo userInfo = null;
if (UserAuthSourceEnum.ALIPAY.getValue().equals(phoneDTO.getSource())) {
openId = alipayUtil.getOpenId(phoneDTO.getCode(), true);
mobile = alipayUtil.getMobile(phoneDTO.getEncryptedData());
userInfo = userInfoService.queryChain().eq(UserInfo::getWechatOpenId, openId).one();
if (StrUtil.isNotBlank(openId)) {
userInfo = userInfoService.queryChain().eq(UserInfo::getWechatOpenId, openId).one();
}
} else {
JSONObject session = wechatAuthUtil.getSession(phoneDTO.getCode());
String sessionKey = session.getString("session_key");
openId = session.getString("openid");
userInfo = userInfoService.queryChain().eq(UserInfo::getAlipayOpenId, openId).one();
if (StrUtil.isNotBlank(openId)) {
userInfo = userInfoService.queryChain().eq(UserInfo::getAlipayOpenId, openId).one();
}
String data = WxMaCryptUtils.decrypt(sessionKey, phoneDTO.getEncryptedData(), phoneDTO.getIv());
JSONObject jsonObject = JSONObject.parseObject(data);
if (jsonObject.containsKey("phoneNumber")) {
@ -74,6 +75,11 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService {
throw new ApiNotPrintException("手机号获取失败");
}
}
if (userInfo != null) {
userInfo.setPhone(mobile);
userInfoService.updateById(userInfo);
}
return mobile;
}