🆕 #2455 【小程序】增加新的获取手机号的接口

This commit is contained in:
hb0730 2021-12-28 18:01:22 +08:00 committed by GitHub
parent 7101c22899
commit 94ad8169fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 4 deletions

View File

@ -54,6 +54,15 @@ public interface WxMaUserService {
*/ */
WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr); WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr);
/**
* 获取手机号信息,基础库:2.21.2及以上
*
* @param code 动态令牌
* @return .
* @throws WxErrorException .
*/
WxMaPhoneNumberInfo getNewPhoneNoInfo(String code) throws WxErrorException;
/** /**
* 验证用户信息完整性. * 验证用户信息完整性.
* *

View File

@ -6,16 +6,19 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.binarywang.wx.miniapp.config.WxMaConfig; import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.SignUtils; import me.chanjar.weixin.common.util.SignUtils;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import java.util.Map; 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; import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.SET_USER_STORAGE;
/** /**
@ -58,6 +61,20 @@ public class WxMaUserServiceImpl implements WxMaUserService {
return WxMaPhoneNumberInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr)); 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 @Override
public boolean checkUserInfo(String sessionKey, String rawData, String signature) { public boolean checkUserInfo(String sessionKey, String rawData, String signature) {
final String generatedSignature = DigestUtils.sha1Hex(rawData + sessionKey); final String generatedSignature = DigestUtils.sha1Hex(rawData + sessionKey);

View File

@ -288,6 +288,7 @@ public class WxMaApiUrlConstants {
public interface User { 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 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 { public interface Ocr {
@ -357,20 +358,20 @@ public class WxMaApiUrlConstants {
interface Delivery { interface Delivery {
String GET_COMPANY_LIST = "https://api.weixin.qq.com/shop/delivery/get_company_list"; 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_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 { interface Aftersale {
String AFTERSALE_ADD = "https://api.weixin.qq.com/shop/aftersale/add"; String AFTERSALE_ADD = "https://api.weixin.qq.com/shop/aftersale/add";
String AFTERSALE_GET = "https://api.weixin.qq.com/shop/aftersale/get"; 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 {
/** /**
* 报销方查询报销发票信息 * 报销方查询报销发票信息
@ -393,7 +394,7 @@ public class WxMaApiUrlConstants {
String UPDATE_STATUS_BATCH = "https://api.weixin.qq.com/card/invoice/reimburse/updatestatusbatch"; 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"; String GET_USER_ENCRYPT_KEY = "https://api.weixin.qq.com/wxa/business/getuserencryptkey";
} }

View File

@ -71,4 +71,9 @@ public class WxMaUserServiceImplTest {
this.wxService.getUserService().setUserStorage(ImmutableMap.of("1","2"), this.wxService.getUserService().setUserStorage(ImmutableMap.of("1","2"),
"r7BXXKkLb8qrSNn05n0qiA",((TestConfig)this.wxService.getWxMaConfig()).getOpenid()); "r7BXXKkLb8qrSNn05n0qiA",((TestConfig)this.wxService.getWxMaConfig()).getOpenid());
} }
@Test
public void testGetNewPhoneNoInfo() throws Exception{
assertNotNull(wxService.getUserService().getNewPhoneNoInfo("test"));
}
} }