Skip to content

Commit 8805e97

Browse files
authored
🆕 #1538 企业微信模块增加获取企业授权信息的接口
1 parent 172638b commit 8805e97

File tree

6 files changed

+300
-5
lines changed

6 files changed

+300
-5
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import me.chanjar.weixin.common.util.http.RequestExecutor;
77
import me.chanjar.weixin.common.util.http.RequestHttp;
88
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
9+
import me.chanjar.weixin.cp.bean.WxCpTpAuthInfo;
910
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
1011
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
1112
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
@@ -119,6 +120,16 @@ public interface WxCpTpService {
119120
*/
120121
String getPreAuthUrl(String redirectUri,String state) throws WxErrorException;
121122

123+
/**
124+
* 获取企业的授权信息
125+
*
126+
* @param authCorpId 授权企业的corpId
127+
* @param permanentCode 授权企业的永久授权码
128+
* @return
129+
* @throws WxErrorException
130+
*/
131+
WxCpTpAuthInfo getAuthInfo(String authCorpId,String permanentCode) throws WxErrorException;
132+
122133
/**
123134
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
124135
*

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
1717
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
1818
import me.chanjar.weixin.cp.api.WxCpTpService;
19-
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
20-
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
21-
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
22-
import me.chanjar.weixin.cp.bean.WxCpTpPreauthCode;
19+
import me.chanjar.weixin.cp.bean.*;
2320
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
2421
import org.apache.commons.lang3.StringUtils;
2522

@@ -148,6 +145,15 @@ public String getPreAuthUrl(String redirectUri,String state) throws WxErrorExcep
148145
return preAuthUrl;
149146
}
150147

148+
@Override
149+
public WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException{
150+
JsonObject jsonObject = new JsonObject();
151+
jsonObject.addProperty("auth_corpid", authCorpId);
152+
jsonObject.addProperty("permanent_code", permanentCode);
153+
String result = post(configStorage.getApiUrl(GET_AUTH_INFO), jsonObject.toString());
154+
return WxCpTpAuthInfo.fromJson(result);
155+
}
156+
151157
@Override
152158
public String get(String url, String queryParam) throws WxErrorException {
153159
return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
7+
8+
import java.util.List;
9+
10+
/**
11+
* 服务商模式获取授权信息
12+
*
13+
* @author Daniel Qian
14+
*/
15+
@Getter
16+
@Setter
17+
public class WxCpTpAuthInfo extends WxCpBaseResp {
18+
19+
private static final long serialVersionUID = -5028321625140879571L;
20+
21+
/**
22+
* 服务商信息
23+
*/
24+
@SerializedName("dealer_corp_info")
25+
private DealerCorpInfo dealerCorpInfo;
26+
27+
/**
28+
* 授权企业信息
29+
*/
30+
@SerializedName("auth_corp_info")
31+
private AuthCorpInfo authCorpInfo;
32+
33+
/**
34+
* 授权信息。如果是通讯录应用,且没开启实体应用,是没有该项的。通讯录应用拥有企业通讯录的全部信息读写权限
35+
*/
36+
@SerializedName("auth_info")
37+
private AuthInfo authInfo;
38+
39+
@Getter
40+
@Setter
41+
public static class DealerCorpInfo {
42+
@SerializedName("corpid")
43+
private String corpId;
44+
45+
@SerializedName("corp_name")
46+
private String corpName;
47+
}
48+
49+
@Getter
50+
@Setter
51+
public static class AuthCorpInfo {
52+
@SerializedName("corpid")
53+
private String corpId;
54+
55+
@SerializedName("corp_name")
56+
private String corpName;
57+
58+
@SerializedName("corp_type")
59+
private String corpType;
60+
61+
@SerializedName("corp_square_logo_url")
62+
private String corpSquareLogoUrl;
63+
64+
@SerializedName("corp_round_logo_url")
65+
private String corpRoundLogoUrl;
66+
67+
@SerializedName("corp_user_max")
68+
private String corpUserMax;
69+
70+
@SerializedName("corp_agent_max")
71+
private String corpAgentMax;
72+
73+
/**
74+
* 所绑定的企业微信主体名称(仅认证过的企业有)
75+
*/
76+
@SerializedName("corp_full_name")
77+
private String corpFullName;
78+
79+
/**
80+
* 认证到期时间
81+
*/
82+
@SerializedName("verified_end_time")
83+
private Long verifiedEndTime;
84+
85+
/**
86+
* 企业类型,1. 企业; 2. 政府以及事业单位; 3. 其他组织, 4.团队号
87+
*/
88+
@SerializedName("subject_type")
89+
private Integer subjectType;
90+
91+
/**
92+
* 授权企业在微工作台(原企业号)的二维码,可用于关注微工作台
93+
*/
94+
@SerializedName("corp_wxqrcode")
95+
private String corpWxqrcode;
96+
97+
@SerializedName("corp_scale")
98+
private String corpScale;
99+
100+
@SerializedName("corp_industry")
101+
private String corpIndustry;
102+
103+
@SerializedName("corp_sub_industry")
104+
private String corpSubIndustry;
105+
106+
@SerializedName("location")
107+
private String location;
108+
109+
}
110+
111+
/**
112+
* 授权信息
113+
*/
114+
@Getter
115+
@Setter
116+
public static class AuthInfo {
117+
118+
/**
119+
* 授权的应用信息,注意是一个数组,但仅旧的多应用套件授权时会返回多个agent,对新的单应用授权,永远只返回一个agent
120+
*/
121+
@SerializedName("agent")
122+
private List<Agent> agent;
123+
124+
}
125+
126+
@Getter
127+
@Setter
128+
public static class Agent {
129+
@SerializedName("agentid")
130+
private Integer agentid;
131+
132+
@SerializedName("name")
133+
private String name;
134+
135+
@SerializedName("round_logo_url")
136+
private String roundLogoUrl;
137+
138+
@SerializedName("square_logo_url")
139+
private String squareLogoUrl;
140+
141+
/**
142+
* 旧的多应用套件中的对应应用id,新开发者请忽略
143+
*/
144+
@SerializedName("appid")
145+
@Deprecated
146+
private String appid;
147+
148+
/**
149+
* 应用权限
150+
*/
151+
@SerializedName("privilege")
152+
private Privilege privilege;
153+
154+
}
155+
156+
/**
157+
* 应用对应的权限
158+
*/
159+
@Getter
160+
@Setter
161+
public static class Privilege {
162+
163+
/**
164+
* 权限等级。
165+
* 1:通讯录基本信息只读
166+
* 2:通讯录全部信息只读
167+
* 3:通讯录全部信息读写
168+
* 4:单个基本信息只读
169+
* 5:通讯录全部信息只写
170+
*/
171+
@SerializedName("level")
172+
private Integer level;
173+
174+
@SerializedName("allow_party")
175+
private List<Integer> allowParty;
176+
177+
@SerializedName("allow_user")
178+
private List<String> allowUser;
179+
180+
@SerializedName("allow_tag")
181+
private List<Integer> allowTag;
182+
183+
@SerializedName("extra_party")
184+
private List<Integer> extraParty;
185+
186+
@SerializedName("extra_user")
187+
private List<String> extraUser;
188+
189+
@SerializedName("extra_tag")
190+
private List<Integer> extraTag;
191+
192+
193+
}
194+
195+
196+
public static WxCpTpAuthInfo fromJson(String json) {
197+
return WxCpGsonBuilder.create().fromJson(json, WxCpTpAuthInfo.class);
198+
}
199+
200+
public String toJson() {
201+
return WxCpGsonBuilder.create().toJson(this);
202+
}
203+
204+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTpPermanentCodeInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.List;
99

1010
/**
11-
* 微信部门.
11+
* 服务商模式获取永久授权码信息
1212
*
1313
* @author Daniel Qian
1414
*/

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static class Tp {
9494
public static final String GET_SUITE_TOKEN = "/cgi-bin/service/get_suite_token";
9595
public static final String GET_PROVIDER_TOKEN = "/cgi-bin/service/get_provider_token";
9696
public static final String GET_PREAUTH_CODE = "/cgi-bin/service/get_pre_auth_code";
97+
public static final String GET_AUTH_INFO = "/cgi-bin/service/get_auth_info";
9798
}
9899

99100
public static class User {

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import me.chanjar.weixin.common.error.WxErrorException;
55
import me.chanjar.weixin.cp.api.WxCpService;
66
import me.chanjar.weixin.cp.api.WxCpTpService;
7+
import me.chanjar.weixin.cp.bean.WxCpTpAuthInfo;
78
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
89
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
910
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
1011
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
1112
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
1213
import org.testng.annotations.Test;
1314

15+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.GET_AUTH_INFO;
1416
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.GET_PERMANENT_CODE;
1517
import static org.assertj.core.api.Assertions.assertThat;
1618
import static org.mockito.Matchers.any;
@@ -196,6 +198,77 @@ public void testGetPermanentCodeInfo() throws WxErrorException{
196198
assertNotNull(tpPermanentCodeInfo.getAuthCorpInfo().getCorpSquareLogoUrl());
197199
}
198200

201+
@Test
202+
public void testGetAuthInfo() throws WxErrorException{
203+
String returnJson = "{\n" +
204+
" \"errcode\":0 ,\n" +
205+
" \"errmsg\":\"ok\" ,\n" +
206+
" \"dealer_corp_info\": \n" +
207+
" {\n" +
208+
" \"corpid\": \"xxxx\",\n" +
209+
" \"corp_name\": \"name\"\n" +
210+
" },\n" +
211+
" \"auth_corp_info\": \n" +
212+
" {\n" +
213+
" \"corpid\": \"xxxx\",\n" +
214+
" \"corp_name\": \"name\",\n" +
215+
" \"corp_type\": \"verified\",\n" +
216+
" \"corp_square_logo_url\": \"yyyyy\",\n" +
217+
" \"corp_user_max\": 50,\n" +
218+
" \"corp_agent_max\": 30,\n" +
219+
" \"corp_full_name\":\"full_name\",\n" +
220+
" \"verified_end_time\":1431775834,\n" +
221+
" \"subject_type\": 1,\n" +
222+
" \"corp_wxqrcode\": \"zzzzz\",\n" +
223+
" \"corp_scale\": \"1-50人\",\n" +
224+
" \"corp_industry\": \"IT服务\",\n" +
225+
" \"corp_sub_industry\": \"计算机软件/硬件/信息服务\",\n" +
226+
" \"location\":\"广东省广州市\"\n" +
227+
" },\n" +
228+
" \"auth_info\":\n" +
229+
" {\n" +
230+
" \"agent\" :\n" +
231+
" [\n" +
232+
" {\n" +
233+
" \"agentid\":1,\n" +
234+
" \"name\":\"NAME\",\n" +
235+
" \"round_logo_url\":\"xxxxxx\",\n" +
236+
" \"square_logo_url\":\"yyyyyy\",\n" +
237+
" \"appid\":1,\n" +
238+
" \"privilege\":\n" +
239+
" {\n" +
240+
" \"level\":1,\n" +
241+
" \"allow_party\":[1,2,3],\n" +
242+
" \"allow_user\":[\"zhansan\",\"lisi\"],\n" +
243+
" \"allow_tag\":[1,2,3],\n" +
244+
" \"extra_party\":[4,5,6],\n" +
245+
" \"extra_user\":[\"wangwu\"],\n" +
246+
" \"extra_tag\":[4,5,6]\n" +
247+
" }\n" +
248+
" },\n" +
249+
" {\n" +
250+
" \"agentid\":2,\n" +
251+
" \"name\":\"NAME2\",\n" +
252+
" \"round_logo_url\":\"xxxxxx\",\n" +
253+
" \"square_logo_url\":\"yyyyyy\",\n" +
254+
" \"appid\":5\n" +
255+
" }\n" +
256+
" ]\n" +
257+
" }\n" +
258+
"}\n";
259+
260+
final WxCpTpConfigStorage configStorage = new WxCpTpDefaultConfigImpl();
261+
tpService.setWxCpTpConfigStorage(configStorage);
262+
JsonObject jsonObject = new JsonObject();
263+
String authCorpId = "xxxxx";
264+
String permanentCode = "xxxxx";
265+
jsonObject.addProperty("auth_corpid", authCorpId);
266+
jsonObject.addProperty("permanent_code", permanentCode);
267+
doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_AUTH_INFO), jsonObject.toString());
268+
WxCpTpAuthInfo authInfo = tpService.getAuthInfo(authCorpId,permanentCode);
269+
assertNotNull(authInfo.getAuthCorpInfo().getCorpId());
270+
}
271+
199272
@Test
200273
public void testGet() {
201274
}

0 commit comments

Comments
 (0)