diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java index 0c9a0a0a41..ad2f403af6 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java @@ -6,6 +6,7 @@ import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; +import me.chanjar.weixin.cp.bean.WxCpTpAuthInfo; import me.chanjar.weixin.cp.bean.WxCpTpCorp; import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; @@ -119,6 +120,16 @@ public interface WxCpTpService { */ String getPreAuthUrl(String redirectUri,String state) throws WxErrorException; + /** + * 获取企业的授权信息 + * + * @param authCorpId 授权企业的corpId + * @param permanentCode 授权企业的永久授权码 + * @return + * @throws WxErrorException + */ + WxCpTpAuthInfo getAuthInfo(String authCorpId,String permanentCode) throws WxErrorException; + /** * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求. * diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java index bbc8e3c18c..dea647f3f9 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java @@ -16,10 +16,7 @@ import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; import me.chanjar.weixin.cp.api.WxCpTpService; -import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; -import me.chanjar.weixin.cp.bean.WxCpTpCorp; -import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo; -import me.chanjar.weixin.cp.bean.WxCpTpPreauthCode; +import me.chanjar.weixin.cp.bean.*; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; import org.apache.commons.lang3.StringUtils; @@ -148,6 +145,15 @@ public String getPreAuthUrl(String redirectUri,String state) throws WxErrorExcep return preAuthUrl; } + @Override + public WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException{ + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("auth_corpid", authCorpId); + jsonObject.addProperty("permanent_code", permanentCode); + String result = post(configStorage.getApiUrl(GET_AUTH_INFO), jsonObject.toString()); + return WxCpTpAuthInfo.fromJson(result); + } + @Override public String get(String url, String queryParam) throws WxErrorException { return execute(SimpleGetRequestExecutor.create(this), url, queryParam); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpAuthInfo.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpAuthInfo.java new file mode 100644 index 0000000000..e57678b1dc --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpAuthInfo.java @@ -0,0 +1,204 @@ +package me.chanjar.weixin.cp.bean; + +import com.google.gson.annotations.SerializedName; +import lombok.Getter; +import lombok.Setter; +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; + +import java.util.List; + +/** + * 服务商模式获取授权信息 + * + * @author Daniel Qian + */ +@Getter +@Setter +public class WxCpTpAuthInfo extends WxCpBaseResp { + + private static final long serialVersionUID = -5028321625140879571L; + + /** + * 服务商信息 + */ + @SerializedName("dealer_corp_info") + private DealerCorpInfo dealerCorpInfo; + + /** + * 授权企业信息 + */ + @SerializedName("auth_corp_info") + private AuthCorpInfo authCorpInfo; + + /** + * 授权信息。如果是通讯录应用,且没开启实体应用,是没有该项的。通讯录应用拥有企业通讯录的全部信息读写权限 + */ + @SerializedName("auth_info") + private AuthInfo authInfo; + + @Getter + @Setter + public static class DealerCorpInfo { + @SerializedName("corpid") + private String corpId; + + @SerializedName("corp_name") + private String corpName; + } + + @Getter + @Setter + public static class AuthCorpInfo { + @SerializedName("corpid") + private String corpId; + + @SerializedName("corp_name") + private String corpName; + + @SerializedName("corp_type") + private String corpType; + + @SerializedName("corp_square_logo_url") + private String corpSquareLogoUrl; + + @SerializedName("corp_round_logo_url") + private String corpRoundLogoUrl; + + @SerializedName("corp_user_max") + private String corpUserMax; + + @SerializedName("corp_agent_max") + private String corpAgentMax; + + /** + * 所绑定的企业微信主体名称(仅认证过的企业有) + */ + @SerializedName("corp_full_name") + private String corpFullName; + + /** + * 认证到期时间 + */ + @SerializedName("verified_end_time") + private Long verifiedEndTime; + + /** + * 企业类型,1. 企业; 2. 政府以及事业单位; 3. 其他组织, 4.团队号 + */ + @SerializedName("subject_type") + private Integer subjectType; + + /** + * 授权企业在微工作台(原企业号)的二维码,可用于关注微工作台 + */ + @SerializedName("corp_wxqrcode") + private String corpWxqrcode; + + @SerializedName("corp_scale") + private String corpScale; + + @SerializedName("corp_industry") + private String corpIndustry; + + @SerializedName("corp_sub_industry") + private String corpSubIndustry; + + @SerializedName("location") + private String location; + + } + + /** + * 授权信息 + */ + @Getter + @Setter + public static class AuthInfo { + + /** + * 授权的应用信息,注意是一个数组,但仅旧的多应用套件授权时会返回多个agent,对新的单应用授权,永远只返回一个agent + */ + @SerializedName("agent") + private List agent; + + } + + @Getter + @Setter + public static class Agent { + @SerializedName("agentid") + private Integer agentid; + + @SerializedName("name") + private String name; + + @SerializedName("round_logo_url") + private String roundLogoUrl; + + @SerializedName("square_logo_url") + private String squareLogoUrl; + + /** + * 旧的多应用套件中的对应应用id,新开发者请忽略 + */ + @SerializedName("appid") + @Deprecated + private String appid; + + /** + * 应用权限 + */ + @SerializedName("privilege") + private Privilege privilege; + + } + + /** + * 应用对应的权限 + */ + @Getter + @Setter + public static class Privilege { + + /** + * 权限等级。 + * 1:通讯录基本信息只读 + * 2:通讯录全部信息只读 + * 3:通讯录全部信息读写 + * 4:单个基本信息只读 + * 5:通讯录全部信息只写 + */ + @SerializedName("level") + private Integer level; + + @SerializedName("allow_party") + private List allowParty; + + @SerializedName("allow_user") + private List allowUser; + + @SerializedName("allow_tag") + private List allowTag; + + @SerializedName("extra_party") + private List extraParty; + + @SerializedName("extra_user") + private List extraUser; + + @SerializedName("extra_tag") + private List extraTag; + + + } + + + public static WxCpTpAuthInfo fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpTpAuthInfo.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpPermanentCodeInfo.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpPermanentCodeInfo.java index c703c28a69..cb5bb85e39 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpPermanentCodeInfo.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpPermanentCodeInfo.java @@ -8,7 +8,7 @@ import java.util.List; /** - * 微信部门. + * 服务商模式获取永久授权码信息 * * @author Daniel Qian */ diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java index da3774ca8d..f10a4dd124 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java @@ -94,6 +94,7 @@ public static class Tp { public static final String GET_SUITE_TOKEN = "/cgi-bin/service/get_suite_token"; public static final String GET_PROVIDER_TOKEN = "/cgi-bin/service/get_provider_token"; public static final String GET_PREAUTH_CODE = "/cgi-bin/service/get_pre_auth_code"; + public static final String GET_AUTH_INFO = "/cgi-bin/service/get_auth_info"; } public static class User { diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java index e8e813b04b..b8e572eb29 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java @@ -4,6 +4,7 @@ import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpTpService; +import me.chanjar.weixin.cp.bean.WxCpTpAuthInfo; import me.chanjar.weixin.cp.bean.WxCpTpCorp; import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; @@ -11,6 +12,7 @@ import me.chanjar.weixin.cp.constant.WxCpApiPathConsts; import org.testng.annotations.Test; +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.GET_AUTH_INFO; import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.GET_PERMANENT_CODE; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; @@ -196,6 +198,77 @@ public void testGetPermanentCodeInfo() throws WxErrorException{ assertNotNull(tpPermanentCodeInfo.getAuthCorpInfo().getCorpSquareLogoUrl()); } + @Test + public void testGetAuthInfo() throws WxErrorException{ + String returnJson = "{\n" + + " \"errcode\":0 ,\n" + + " \"errmsg\":\"ok\" ,\n" + + " \"dealer_corp_info\": \n" + + " {\n" + + " \"corpid\": \"xxxx\",\n" + + " \"corp_name\": \"name\"\n" + + " },\n" + + " \"auth_corp_info\": \n" + + " {\n" + + " \"corpid\": \"xxxx\",\n" + + " \"corp_name\": \"name\",\n" + + " \"corp_type\": \"verified\",\n" + + " \"corp_square_logo_url\": \"yyyyy\",\n" + + " \"corp_user_max\": 50,\n" + + " \"corp_agent_max\": 30,\n" + + " \"corp_full_name\":\"full_name\",\n" + + " \"verified_end_time\":1431775834,\n" + + " \"subject_type\": 1,\n" + + " \"corp_wxqrcode\": \"zzzzz\",\n" + + " \"corp_scale\": \"1-50人\",\n" + + " \"corp_industry\": \"IT服务\",\n" + + " \"corp_sub_industry\": \"计算机软件/硬件/信息服务\",\n" + + " \"location\":\"广东省广州市\"\n" + + " },\n" + + " \"auth_info\":\n" + + " {\n" + + " \"agent\" :\n" + + " [\n" + + " {\n" + + " \"agentid\":1,\n" + + " \"name\":\"NAME\",\n" + + " \"round_logo_url\":\"xxxxxx\",\n" + + " \"square_logo_url\":\"yyyyyy\",\n" + + " \"appid\":1,\n" + + " \"privilege\":\n" + + " {\n" + + " \"level\":1,\n" + + " \"allow_party\":[1,2,3],\n" + + " \"allow_user\":[\"zhansan\",\"lisi\"],\n" + + " \"allow_tag\":[1,2,3],\n" + + " \"extra_party\":[4,5,6],\n" + + " \"extra_user\":[\"wangwu\"],\n" + + " \"extra_tag\":[4,5,6]\n" + + " }\n" + + " },\n" + + " {\n" + + " \"agentid\":2,\n" + + " \"name\":\"NAME2\",\n" + + " \"round_logo_url\":\"xxxxxx\",\n" + + " \"square_logo_url\":\"yyyyyy\",\n" + + " \"appid\":5\n" + + " }\n" + + " ]\n" + + " }\n" + + "}\n"; + + final WxCpTpConfigStorage configStorage = new WxCpTpDefaultConfigImpl(); + tpService.setWxCpTpConfigStorage(configStorage); + JsonObject jsonObject = new JsonObject(); + String authCorpId = "xxxxx"; + String permanentCode = "xxxxx"; + jsonObject.addProperty("auth_corpid", authCorpId); + jsonObject.addProperty("permanent_code", permanentCode); + doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_AUTH_INFO), jsonObject.toString()); + WxCpTpAuthInfo authInfo = tpService.getAuthInfo(authCorpId,permanentCode); + assertNotNull(authInfo.getAuthCorpInfo().getCorpId()); + } + @Test public void testGet() { }