🎨 #3345 【小程序】获取手机号 getPhoneNoInfo方法兼容旧版本

This commit is contained in:
水依寒 2024-08-20 18:32:44 +08:00 committed by GitHub
parent 748c19f108
commit e6ec2a5173
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 4 deletions

View File

@ -45,7 +45,19 @@ public interface WxMaUserService {
void setUserStorage(Map<String, String> kvMap, String sessionKey, String openid) throws WxErrorException;
/**
* 获取手机号信息,2023年8月28日起
* 解密用户手机号信息.
*
* @param sessionKey 会话密钥
* @param encryptedData 消息密文
* @param ivStr 加密算法的初始向量
* @return .
* @deprecated 当前基础库2.21.2以下使用旧版本以上请使用替代方法 {@link #getPhoneNoInfo(String)}
*/
@Deprecated
WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr);
/**
* 获取手机号信息,基础库:2.21.2及以上或2023年8月28日起
*
* @param code 每个code只能使用一次code的有效期为5mincode获取方式参考<a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html">手机号快速验证组件</a>
* @return 用户手机号信息
@ -55,7 +67,7 @@ public interface WxMaUserService {
WxMaPhoneNumberInfo getPhoneNumber(String code) throws WxErrorException;
/**
* 获取手机号信息,2023年8月28日起
* 获取手机号信息,基础库:2.21.2及以上或2023年8月28日起
*
* @param code 每个code只能使用一次code的有效期为5mincode获取方式参考<a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html">手机号快速验证组件</a>
* @return 用户手机号信息

View File

@ -57,6 +57,11 @@ public class WxMaUserServiceImpl implements WxMaUserService {
this.service.post(url, params);
}
@Override
public WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr) {
return WxMaPhoneNumberInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
}
@Override
public WxMaPhoneNumberInfo getPhoneNumber(String code) throws WxErrorException {
JsonObject param = new JsonObject();
@ -67,7 +72,6 @@ public class WxMaUserServiceImpl implements WxMaUserService {
return WxMaGsonBuilder.create().fromJson(response.getAsJsonObject(PHONE_INFO),
WxMaPhoneNumberInfo.class);
}
return null;
}

View File

@ -49,7 +49,16 @@ public class WxMaUserServiceImplTest {
@Test
public void testGetPhoneNoInfo() throws WxErrorException {
public void testGetPhoneNoInfo() {
WxMaPhoneNumberInfo phoneNoInfo = this.wxService.getUserService().getPhoneNoInfo("tiihtNczf5v6AKRyjwEUhQ==",
"CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew==",
"r7BXXKkLb8qrSNn05n0qiA==");
assertNotNull(phoneNoInfo);
System.out.println(phoneNoInfo.toString());
}
@Test
public void testGetPhoneInfo() throws WxErrorException {
WxMaPhoneNumberInfo phoneNoInfo = this.wxService.getUserService().getPhoneNumber("tiihtNczf5v6AKRyjwEUhQ==");
assertNotNull(phoneNoInfo);
System.out.println(phoneNoInfo.toString());