Skip to content

Commit

Permalink
🆕 #2455 【小程序】增加新的获取手机号的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0730 authored Dec 28, 2021
1 parent 7101c22 commit 94ad816
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public interface WxMaUserService {
*/
WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr);

/**
* 获取手机号信息,基础库:2.21.2及以上
*
* @param code 动态令牌
* @return .
* @throws WxErrorException .
*/
WxMaPhoneNumberInfo getNewPhoneNoInfo(String code) throws WxErrorException;

/**
* 验证用户信息完整性.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.SignUtils;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Map;

import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.GET_PHONE_NUMBER_URL;
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.SET_USER_STORAGE;

/**
Expand Down Expand Up @@ -58,6 +61,20 @@ public WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedDat
return WxMaPhoneNumberInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
}

@Override
public WxMaPhoneNumberInfo getNewPhoneNoInfo(String code) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("code", code);
String responseContent = this.service.post(GET_PHONE_NUMBER_URL, param.toString());
JsonObject response = GsonParser.parse(responseContent);
boolean hasPhoneInfo = response.has("phone_info");
if (hasPhoneInfo) {
return WxMaGsonBuilder.create().fromJson(response.getAsJsonObject("phone_info"), WxMaPhoneNumberInfo.class);
} else {
return null;
}
}

@Override
public boolean checkUserInfo(String sessionKey, String rawData, String signature) {
final String generatedSignature = DigestUtils.sha1Hex(rawData + sessionKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public interface Subscribe {

public interface User {
String SET_USER_STORAGE = "https://api.weixin.qq.com/wxa/set_user_storage?appid=%s&signature=%s&openid=%s&sig_method=%s";
String GET_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
}

public interface Ocr {
Expand Down Expand Up @@ -357,20 +358,20 @@ interface Audit {
interface Delivery {
String GET_COMPANY_LIST = "https://api.weixin.qq.com/shop/delivery/get_company_list";
String DELIVERY_SEND = "https://api.weixin.qq.com/shop/delivery/send";
String DELIVERY_RECEIVE = "https://api.weixin.qq.com/shop/delivery/recieve";
String DELIVERY_RECEIVE = "https://api.weixin.qq.com/shop/delivery/recieve";
}

interface Aftersale {
String AFTERSALE_ADD = "https://api.weixin.qq.com/shop/aftersale/add";
String AFTERSALE_GET = "https://api.weixin.qq.com/shop/aftersale/get";
String AFTERSALE_UPDATE = "https://api.weixin.qq.com/shop/aftersale/update";
String AFTERSALE_UPDATE = "https://api.weixin.qq.com/shop/aftersale/update";
}
}

/**
* 电子发票报销方
*/
public interface Invoice{
public interface Invoice {

/**
* 报销方查询报销发票信息
Expand All @@ -393,7 +394,7 @@ public interface Invoice{
String UPDATE_STATUS_BATCH = "https://api.weixin.qq.com/card/invoice/reimburse/updatestatusbatch";
}

public interface Internet{
public interface Internet {
String GET_USER_ENCRYPT_KEY = "https://api.weixin.qq.com/wxa/business/getuserencryptkey";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ public void testSetUserStorage() throws WxErrorException {
this.wxService.getUserService().setUserStorage(ImmutableMap.of("1","2"),
"r7BXXKkLb8qrSNn05n0qiA",((TestConfig)this.wxService.getWxMaConfig()).getOpenid());
}

@Test
public void testGetNewPhoneNoInfo() throws Exception{
assertNotNull(wxService.getUserService().getNewPhoneNoInfo("test"));
}
}

0 comments on commit 94ad816

Please sign in to comment.