mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 00:46:54 +08:00 
			
		
		
		
	🆕 #2455 【小程序】增加新的获取手机号的接口
This commit is contained in:
		| @@ -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; | ||||
|  | ||||
|   /** | ||||
|    * 验证用户信息完整性. | ||||
|    * | ||||
|   | ||||
| @@ -6,16 +6,19 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | ||||
| 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; | ||||
|  | ||||
| /** | ||||
| @@ -58,6 +61,20 @@ public class WxMaUserServiceImpl implements WxMaUserService { | ||||
|     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); | ||||
|   | ||||
| @@ -288,6 +288,7 @@ public class WxMaApiUrlConstants { | ||||
|  | ||||
|   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 { | ||||
| @@ -357,20 +358,20 @@ public class WxMaApiUrlConstants { | ||||
|     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 { | ||||
|  | ||||
|     /** | ||||
|      * 报销方查询报销发票信息 | ||||
| @@ -393,7 +394,7 @@ public class WxMaApiUrlConstants { | ||||
|     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"; | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -71,4 +71,9 @@ public class WxMaUserServiceImplTest { | ||||
|     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")); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 hb0730
					hb0730