From e6ec2a51731c9b280beeb8cf2320a454681400f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B0=B4=E4=BE=9D=E5=AF=92?= Date: Tue, 20 Aug 2024 18:32:44 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#3345=20=20=E3=80=90=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E3=80=91=E8=8E=B7=E5=8F=96=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=20getPhoneNoInfo=E6=96=B9=E6=B3=95=E5=85=BC=E5=AE=B9=E6=97=A7?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/WxMaUserService.java | 16 ++++++++++++++-- .../wx/miniapp/api/impl/WxMaUserServiceImpl.java | 6 +++++- .../api/impl/WxMaUserServiceImplTest.java | 11 ++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaUserService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaUserService.java index 3b2c3f74d..8c6a8ef87 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaUserService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaUserService.java @@ -45,7 +45,19 @@ public interface WxMaUserService { void setUserStorage(Map 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的有效期为5min。code获取方式参考手机号快速验证组件 * @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的有效期为5min。code获取方式参考手机号快速验证组件 * @return 用户手机号信息 diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImpl.java index 8a921d05a..c9f5c2e33 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImpl.java @@ -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; } diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java index d3cd1b7d2..7c6d61082 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java @@ -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());