Skip to content

Commit 701070b

Browse files
committed
feat: 小程序获取手机号支持openid校验
1 parent 8c5d917 commit 701070b

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaUserService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ public interface WxMaUserService {
7373
*/
7474
WxMaPhoneNumberInfo getPhoneNumber(String code) throws WxErrorException;
7575

76+
/**
77+
* 获取手机号信息,并校验手机号获取凭证与用户的绑定关系。
78+
*
79+
* @param code 每个code只能使用一次,code的有效期为5min。code获取方式参考<a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html">手机号快速验证组件</a>
80+
* @param openid 用户openid,传入后微信服务端将校验其与code的绑定关系
81+
* @return 用户手机号信息
82+
* @throws WxErrorException .
83+
* @apiNote 该接口用于将code换取用户手机号。
84+
*/
85+
WxMaPhoneNumberInfo getPhoneNumber(String code, String openid) throws WxErrorException;
86+
7687
/**
7788
* 获取手机号信息,基础库:2.21.2及以上或2023年8月28日起
7889
*

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,16 @@ public WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedDat
6767

6868
@Override
6969
public WxMaPhoneNumberInfo getPhoneNumber(String code) throws WxErrorException {
70+
return this.getPhoneNumber(code, null);
71+
}
72+
73+
@Override
74+
public WxMaPhoneNumberInfo getPhoneNumber(String code, String openid) throws WxErrorException {
7075
JsonObject param = new JsonObject();
7176
param.addProperty("code", code);
77+
if (openid != null) {
78+
param.addProperty("openid", openid);
79+
}
7280
String responseContent = this.service.post(GET_PHONE_NUMBER_URL, param.toString());
7381
JsonObject response = GsonParser.parse(responseContent);
7482
if (response.has(PHONE_INFO)) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
import org.testng.annotations.Test;
6+
7+
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.GET_PHONE_NUMBER_URL;
8+
import static org.mockito.ArgumentMatchers.anyString;
9+
import static org.mockito.ArgumentMatchers.eq;
10+
import static org.mockito.Mockito.mock;
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.when;
13+
14+
/**
15+
* {@link WxMaUserServiceImpl} 获取手机号接口的单元测试。
16+
*/
17+
public class WxMaUserServiceImplPhoneNumberTest {
18+
19+
@Test
20+
public void shouldSendOpenidWhenGettingPhoneNumber() throws WxErrorException {
21+
WxMaService wxMaService = mock(WxMaService.class);
22+
when(wxMaService.post(anyString(), anyString())).thenReturn("{\"phone_info\":{}}");
23+
24+
new WxMaUserServiceImpl(wxMaService).getPhoneNumber("phone-code", "user-openid");
25+
26+
verify(wxMaService).post(GET_PHONE_NUMBER_URL, "{\"code\":\"phone-code\",\"openid\":\"user-openid\"}");
27+
}
28+
}

0 commit comments

Comments
 (0)