Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public interface WxMaUserService {
*/
WxMaPhoneNumberInfo getPhoneNumber(String code) throws WxErrorException;

/**
* 获取手机号信息,并校验手机号获取凭证与用户的绑定关系。
*
* @param code 每个code只能使用一次,code的有效期为5min。code获取方式参考<a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html">手机号快速验证组件</a>
* @param openid 用户openid,传入后微信服务端将校验其与code的绑定关系
* @return 用户手机号信息
* @throws WxErrorException .
* @apiNote 该接口用于将code换取用户手机号。
*/
WxMaPhoneNumberInfo getPhoneNumber(String code, String openid) throws WxErrorException;
Comment thread
binarywang marked this conversation as resolved.
Outdated
Comment thread
binarywang marked this conversation as resolved.
Outdated
Comment thread
binarywang marked this conversation as resolved.
Outdated

/**
* 获取手机号信息,基础库:2.21.2及以上或2023年8月28日起
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ public WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedDat

@Override
public WxMaPhoneNumberInfo getPhoneNumber(String code) throws WxErrorException {
return this.getPhoneNumber(code, null);
}

@Override
public WxMaPhoneNumberInfo getPhoneNumber(String code, String openid) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("code", code);
if (openid != null) {
Comment thread
binarywang marked this conversation as resolved.
Outdated
param.addProperty("openid", openid);
Comment thread
binarywang marked this conversation as resolved.
Outdated
}
String responseContent = this.service.post(GET_PHONE_NUMBER_URL, param.toString());
JsonObject response = GsonParser.parse(responseContent);
if (response.has(PHONE_INFO)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.testng.annotations.Test;

import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.GET_PHONE_NUMBER_URL;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* {@link WxMaUserServiceImpl} 获取手机号接口的单元测试。
*/
public class WxMaUserServiceImplPhoneNumberTest {

@Test
public void shouldSendOpenidWhenGettingPhoneNumber() throws WxErrorException {
WxMaService wxMaService = mock(WxMaService.class);
when(wxMaService.post(anyString(), anyString())).thenReturn("{\"phone_info\":{}}");

new WxMaUserServiceImpl(wxMaService).getPhoneNumber("phone-code", "user-openid");

verify(wxMaService).post(GET_PHONE_NUMBER_URL, "{\"code\":\"phone-code\",\"openid\":\"user-openid\"}");
Comment thread
binarywang marked this conversation as resolved.
Outdated
Comment thread
binarywang marked this conversation as resolved.
Outdated
}
}