diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpUnionidToExternalUseridResult.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpUnionidToExternalUseridResult.java new file mode 100644 index 0000000000..f14fa45f6a --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpUnionidToExternalUseridResult.java @@ -0,0 +1,27 @@ +package me.chanjar.weixin.cp.bean; + +import com.google.gson.annotations.SerializedName; +import lombok.*; +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; + + +@Getter +@Setter +public class WxCpTpUnionidToExternalUseridResult extends WxCpBaseResp { + + + private static final long serialVersionUID = -6153589164415497369L; + + @SerializedName("external_userid") + private String externalUserid; + + @SerializedName("pending_id") + private String pendingId; + + + public static WxCpTpUnionidToExternalUseridResult fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpTpUnionidToExternalUseridResult.class); + } + + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpIdConverService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpIdConverService.java new file mode 100644 index 0000000000..c60e20a2c9 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpIdConverService.java @@ -0,0 +1,25 @@ +package me.chanjar.weixin.cp.tp.service; + +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.cp.bean.WxCpTpUnionidToExternalUseridResult; + +/** + *
+ *  企业微信三方应用ID转换接口
+ *
+ * 
+ */ +public interface WxCpTpIdConverService { + + + /** + * unionid与external_userid的关联 https://developer.work.weixin.qq.com/document/path/95900 + * @param unionid 微信客户的unionid + * @param openid 微信客户的openid + * @param subjectType 程序或公众号的主体类型: 0表示主体名称是企业的,1表示主体名称是服务商的 + * @throws WxErrorException + */ + WxCpTpUnionidToExternalUseridResult unionidToExternalUserid(String cropId,String unionid, String openid, Integer subjectType) throws WxErrorException; + + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java index ecb7084c73..c20878a24d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java @@ -605,4 +605,10 @@ public interface WxCpTpService { * @param wxCpTpEditionService the wx cp tp edition service */ void setWxCpTpOrderService(WxCpTpEditionService wxCpTpEditionService); + + + WxCpTpIdConverService getWxCpTpIdConverService(); + + void setWxCpTpIdConverService(WxCpTpIdConverService wxCpTpIdConverService); + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java index c089cb3e22..f6b11794d5 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java @@ -57,6 +57,7 @@ public abstract class BaseWxCpTpServiceImpl implements WxCpTpService, Requ private WxCpTpOrderService wxCpTpOrderService = new WxCpTpOrderServiceImpl(this); private WxCpTpEditionService wxCpTpEditionService = new WxCpTpEditionServiceImpl(this); private WxCpTpLicenseService wxCpTpLicenseService = new WxCpTpLicenseServiceImpl(this); + private WxCpTpIdConverService wxCpTpIdConverService = new WxCpTpIdConverServiceImpl(this); /** * 全局的是否正在刷新access token的锁. @@ -725,4 +726,16 @@ private WxJsapiSignature doCreateWxJsapiSignature(String url, String authCorpId, return jsapiSignature; } + @Override + public WxCpTpIdConverService getWxCpTpIdConverService() { + return wxCpTpIdConverService; + } + + @Override + public void setWxCpTpIdConverService(WxCpTpIdConverService wxCpTpIdConverService) { + this.wxCpTpIdConverService = wxCpTpIdConverService; + } + + + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpIdConverServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpIdConverServiceImpl.java new file mode 100644 index 0000000000..412d91e646 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpIdConverServiceImpl.java @@ -0,0 +1,34 @@ +package me.chanjar.weixin.cp.tp.service.impl; + +import com.google.gson.JsonObject; +import lombok.RequiredArgsConstructor; +import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.cp.bean.WxCpTpUnionidToExternalUseridResult; +import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; +import me.chanjar.weixin.cp.tp.service.WxCpTpIdConverService; +import me.chanjar.weixin.cp.tp.service.WxCpTpService; + + +@RequiredArgsConstructor +public class WxCpTpIdConverServiceImpl implements WxCpTpIdConverService { + + private final WxCpTpService mainService; + + @Override + public WxCpTpUnionidToExternalUseridResult unionidToExternalUserid(String cropId, String unionid, String openid, Integer subjectType) throws WxErrorException { + JsonObject json = new JsonObject(); + json.addProperty("unionid", unionid); + json.addProperty("openid", openid); + if (subjectType != null) { + json.addProperty("subject_type", subjectType); + } + WxCpTpConfigStorage wxCpTpConfigStorage = mainService.getWxCpTpConfigStorage(); + String accessToken = wxCpTpConfigStorage.getAccessToken(cropId); + String url = wxCpTpConfigStorage.getApiUrl("/cgi-bin/idconvert/unionid_to_external_userid"); + url += "?access_token=" + accessToken; + String responseContent = this.mainService.post(url, json.toString()); + return WxCpTpUnionidToExternalUseridResult.fromJson(responseContent); + } + + +}