new package wxa.model.phone

This commit is contained in:
niko 2021-12-20 16:33:24 +08:00
parent 8d96e3c46f
commit 10a346ca67
4 changed files with 108 additions and 88 deletions

View File

@ -1,15 +1,14 @@
package com.foxinmy.weixin4j.wxa.api; package com.foxinmy.weixin4j.wxa.api;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.wxa.model.phone.PhoneInfo;
import java.io.Serializable;
/** /**
* get phone number result * get phone number result
* *
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html"><a/> * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html"><a/>
*/ */
public class PhoneInfoResult implements Serializable { public class PhoneInfoResult extends WxaApiResult {
public static final TypeReference<PhoneInfoResult> TYPE_REFERENCE public static final TypeReference<PhoneInfoResult> TYPE_REFERENCE
= new TypeReference<PhoneInfoResult>() { = new TypeReference<PhoneInfoResult>() {
@ -25,82 +24,4 @@ public class PhoneInfoResult implements Serializable {
this.phoneInfo = phoneInfo; 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;
}
}
} }

View File

@ -1,17 +1,16 @@
package com.foxinmy.weixin4j.wxa.api; package com.foxinmy.weixin4j.wxa.api;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse; import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.token.TokenManager; import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.wxa.WXBizDataCrypt;
import com.foxinmy.weixin4j.wxa.model.phone.PhoneInfo;
import java.util.Properties; import java.util.Properties;
public class PhoneNumberApi extends TokenManagerApi { public class PhoneNumberApi extends TokenManagerApi {
public PhoneNumberApi(TokenManager tokenManager) {
super(tokenManager);
}
public PhoneNumberApi(TokenManager tokenManager, Properties properties) { public PhoneNumberApi(TokenManager tokenManager, Properties properties) {
super(tokenManager, properties); super(tokenManager, properties);
} }
@ -21,15 +20,30 @@ public class PhoneNumberApi extends TokenManagerApi {
* *
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html"><a/> * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html"><a/>
*/ */
public PhoneInfoResult.PhoneInfo getPhoneNumber(String code) throws WeixinException { public PhoneInfo getPhoneNumber(String code) throws WeixinException {
String wxaGetUserPhone = getRequestUri( String wxaGetUserPhone = getRequestUri(
"wxa_get_user_phone", "wxa_get_user_phone",
code, code,
WxaApiResult.TYPE_REFERENCE WxaApiResult.TYPE_REFERENCE
); );
WeixinResponse post = this.weixinExecutor.post(wxaGetUserPhone); WeixinResponse post = this.weixinExecutor.post(wxaGetUserPhone);
return post.getAsObject(PhoneInfoResult.TYPE_REFERENCE) PhoneInfoResult phoneInfoResult = post.getAsObject(PhoneInfoResult.TYPE_REFERENCE);
.getPhoneInfo(); 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);
} }
} }

View File

@ -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;
}
}

View File

@ -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;
}
}