diff --git a/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneInfoResult.java b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneInfoResult.java index e813cef4..775dfe46 100644 --- a/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneInfoResult.java +++ b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneInfoResult.java @@ -1,15 +1,14 @@ package com.foxinmy.weixin4j.wxa.api; import com.alibaba.fastjson.TypeReference; - -import java.io.Serializable; +import com.foxinmy.weixin4j.wxa.model.phone.PhoneInfo; /** * get phone number result * * @see */ -public class PhoneInfoResult implements Serializable { +public class PhoneInfoResult extends WxaApiResult { public static final TypeReference TYPE_REFERENCE = new TypeReference() { @@ -25,82 +24,4 @@ public class PhoneInfoResult implements Serializable { this.phoneInfo = phoneInfo; } - public static class PhoneInfo implements Serializable { - - private String phoneNumber; - - private String purePhoneNumber; - - private String countryCode; - - private Watermark watermark; - - /** - * @return 用户绑定的手机号(国外手机号会有区号) - */ - public String getPhoneNumber() { - return phoneNumber; - } - - public void setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - } - - /** - * @return 没有区号的手机号 - */ - public String getPurePhoneNumber() { - return purePhoneNumber; - } - - public void setPurePhoneNumber(String purePhoneNumber) { - this.purePhoneNumber = purePhoneNumber; - } - - /** - * @return 区号 - */ - public String getCountryCode() { - return countryCode; - } - - public void setCountryCode(String countryCode) { - this.countryCode = countryCode; - } - - /** - * @return 数据水印 - */ - public Watermark getWatermark() { - return watermark; - } - - public void setWatermark(Watermark watermark) { - this.watermark = watermark; - } - } - - - public static class Watermark implements Serializable { - - private String appId; - - private Long timestamp; - - public String getAppId() { - return appId; - } - - public void setAppId(String appId) { - this.appId = appId; - } - - public Long getTimestamp() { - return timestamp; - } - - public void setTimestamp(Long timestamp) { - this.timestamp = timestamp; - } - } } diff --git a/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneNumberApi.java b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneNumberApi.java index 46c87081..f09781a4 100644 --- a/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneNumberApi.java +++ b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/api/PhoneNumberApi.java @@ -1,17 +1,16 @@ package com.foxinmy.weixin4j.wxa.api; +import com.alibaba.fastjson.JSONObject; import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.http.weixin.WeixinResponse; import com.foxinmy.weixin4j.token.TokenManager; +import com.foxinmy.weixin4j.wxa.WXBizDataCrypt; +import com.foxinmy.weixin4j.wxa.model.phone.PhoneInfo; import java.util.Properties; public class PhoneNumberApi extends TokenManagerApi { - public PhoneNumberApi(TokenManager tokenManager) { - super(tokenManager); - } - public PhoneNumberApi(TokenManager tokenManager, Properties properties) { super(tokenManager, properties); } @@ -21,15 +20,30 @@ public class PhoneNumberApi extends TokenManagerApi { * * @see */ - public PhoneInfoResult.PhoneInfo getPhoneNumber(String code) throws WeixinException { + public PhoneInfo getPhoneNumber(String code) throws WeixinException { String wxaGetUserPhone = getRequestUri( "wxa_get_user_phone", code, WxaApiResult.TYPE_REFERENCE ); WeixinResponse post = this.weixinExecutor.post(wxaGetUserPhone); - return post.getAsObject(PhoneInfoResult.TYPE_REFERENCE) - .getPhoneInfo(); + PhoneInfoResult phoneInfoResult = post.getAsObject(PhoneInfoResult.TYPE_REFERENCE); + phoneInfoResult.checkErrCode(); + return phoneInfoResult.getPhoneInfo(); + } + + /** + * 从 encryptedData 中解析 PhoneInfo + */ + public PhoneInfo parsePhoneNumber( + String appId, + String sessionKey, + String encryptedData, + String iv + ) { + WXBizDataCrypt wxBizDataCrypt = new WXBizDataCrypt(appId, sessionKey); + JSONObject jsonObject = wxBizDataCrypt.decryptData(encryptedData, iv); + return JSONObject.toJavaObject(jsonObject, PhoneInfo.class); } } diff --git a/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/model/phone/PhoneInfo.java b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/model/phone/PhoneInfo.java new file mode 100644 index 00000000..695b643a --- /dev/null +++ b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/model/phone/PhoneInfo.java @@ -0,0 +1,58 @@ +package com.foxinmy.weixin4j.wxa.model.phone; + +import java.io.Serializable; + +public class PhoneInfo implements Serializable { + + private String phoneNumber; + + private String purePhoneNumber; + + private String countryCode; + + private Watermark watermark; + + /** + * @return 用户绑定的手机号(国外手机号会有区号) + */ + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + /** + * @return 没有区号的手机号 + */ + public String getPurePhoneNumber() { + return purePhoneNumber; + } + + public void setPurePhoneNumber(String purePhoneNumber) { + this.purePhoneNumber = purePhoneNumber; + } + + /** + * @return 区号 + */ + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + + /** + * @return 数据水印 + */ + public Watermark getWatermark() { + return watermark; + } + + public void setWatermark(Watermark watermark) { + this.watermark = watermark; + } +} diff --git a/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/model/phone/Watermark.java b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/model/phone/Watermark.java new file mode 100644 index 00000000..c4685a1d --- /dev/null +++ b/weixin4j-wxa/src/main/java/com/foxinmy/weixin4j/wxa/model/phone/Watermark.java @@ -0,0 +1,27 @@ +package com.foxinmy.weixin4j.wxa.model.phone; + +import java.io.Serializable; + +public class Watermark implements Serializable { + + private String appId; + + private Long timestamp; + + public String getAppId() { + return appId; + } + + public void setAppId(String appId) { + this.appId = appId; + } + + public Long getTimestamp() { + return timestamp; + } + + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } + +}