Skip to content

Commit 6504f5d

Browse files
authored
🆕 #3823 【企业微信】添加获取应用管理员列表的接口
1 parent 912ba35 commit 6504f5d

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.chanjar.weixin.common.error.WxErrorException;
44
import me.chanjar.weixin.cp.bean.WxCpAgent;
5+
import me.chanjar.weixin.cp.bean.WxCpTpAdmin;
56

67
import java.util.List;
78

@@ -52,4 +53,18 @@ public interface WxCpAgentService {
5253
*/
5354
List<WxCpAgent> list() throws WxErrorException;
5455

56+
/**
57+
* <pre>
58+
* 获取应用管理员列表
59+
* 第三方服务商可以用此接口获取授权企业中某个第三方应用或者代开发应用的管理员列表(不包括外部管理员),
60+
* 以便服务商在用户进入应用主页之后根据是否管理员身份做权限的区分。
61+
* 详情请见: <a href="https://developer.work.weixin.qq.com/document/path/90506">文档</a>
62+
* </pre>
63+
*
64+
* @param agentId 应用id
65+
* @return admin list
66+
* @throws WxErrorException the wx error exception
67+
*/
68+
WxCpTpAdmin getAdminList(Integer agentId) throws WxErrorException;
69+
5570
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import me.chanjar.weixin.cp.api.WxCpAgentService;
1212
import me.chanjar.weixin.cp.api.WxCpService;
1313
import me.chanjar.weixin.cp.bean.WxCpAgent;
14+
import me.chanjar.weixin.cp.bean.WxCpTpAdmin;
1415
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
1516

1617
import java.util.List;
@@ -65,4 +66,21 @@ public List<WxCpAgent> list() throws WxErrorException {
6566
}.getType());
6667
}
6768

69+
@Override
70+
public WxCpTpAdmin getAdminList(Integer agentId) throws WxErrorException {
71+
if (agentId == null) {
72+
throw new IllegalArgumentException("缺少agentid参数");
73+
}
74+
75+
JsonObject jsonObject = new JsonObject();
76+
jsonObject.addProperty("agentid", agentId);
77+
String url = this.mainService.getWxCpConfigStorage().getApiUrl(AGENT_GET_ADMIN_LIST);
78+
String responseContent = this.mainService.post(url, jsonObject.toString());
79+
JsonObject respObj = GsonParser.parse(responseContent);
80+
if (respObj.get(WxConsts.ERR_CODE).getAsInt() != 0) {
81+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.CP));
82+
}
83+
return WxCpTpAdmin.fromJson(responseContent);
84+
}
85+
6886
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ interface Agent {
112112
* The constant AGENT_LIST.
113113
*/
114114
String AGENT_LIST = "/cgi-bin/agent/list";
115+
/**
116+
* The constant AGENT_GET_ADMIN_LIST.
117+
*/
118+
String AGENT_GET_ADMIN_LIST = "/cgi-bin/agent/get_admin_list";
115119
}
116120

117121
/**

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
35
import com.google.inject.Inject;
46
import me.chanjar.weixin.common.error.WxErrorException;
57
import me.chanjar.weixin.cp.api.ApiTestModule;
68
import me.chanjar.weixin.cp.api.WxCpAgentService;
79
import me.chanjar.weixin.cp.api.WxCpService;
810
import me.chanjar.weixin.cp.bean.WxCpAgent;
11+
import me.chanjar.weixin.cp.bean.WxCpTpAdmin;
912
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
1013
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
1114
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
@@ -82,6 +85,20 @@ public void testList() throws WxErrorException {
8285
assertThat(list.get(0).getSquareLogoUrl()).isNotEmpty();
8386
}
8487

88+
/**
89+
* Test get admin list.
90+
*
91+
* @throws WxErrorException the wx error exception
92+
*/
93+
@Test
94+
public void testGetAdminList() throws WxErrorException {
95+
final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId();
96+
WxCpTpAdmin adminList = this.wxCpService.getAgentService().getAdminList(agentId);
97+
98+
assertThat(adminList).isNotNull();
99+
assertThat(adminList.getErrcode()).isEqualTo(0L);
100+
}
101+
85102
/**
86103
* The type Mock test.
87104
*/
@@ -118,6 +135,51 @@ public void testGet() throws Exception {
118135

119136
}
120137

138+
/**
139+
* Test get admin list.
140+
*
141+
* @throws Exception the exception
142+
*/
143+
@Test
144+
public void testGetAdminList() throws Exception {
145+
// 构建响应JSON
146+
JsonObject admin1 = new JsonObject();
147+
admin1.addProperty("userid", "zhangsan");
148+
admin1.addProperty("open_userid", "woAJ2GCAAAXtWyujaWJHDDGi0mACH71w");
149+
admin1.addProperty("auth_type", 1);
150+
151+
JsonObject admin2 = new JsonObject();
152+
admin2.addProperty("userid", "lisi");
153+
admin2.addProperty("open_userid", "woAJ2GCAAAXtWyujaWJHDDGi0mACH72w");
154+
admin2.addProperty("auth_type", 2);
155+
156+
JsonArray adminArray = new JsonArray();
157+
adminArray.add(admin1);
158+
adminArray.add(admin2);
159+
160+
JsonObject returnJsonObj = new JsonObject();
161+
returnJsonObj.addProperty("errcode", 0);
162+
returnJsonObj.addProperty("errmsg", "ok");
163+
returnJsonObj.add("admin", adminArray);
164+
String returnJson = returnJsonObj.toString();
165+
166+
JsonObject requestJson = new JsonObject();
167+
requestJson.addProperty("agentid", 9);
168+
final WxCpConfigStorage configStorage = new WxCpDefaultConfigImpl();
169+
when(wxService.getWxCpConfigStorage()).thenReturn(configStorage);
170+
when(wxService.post(configStorage.getApiUrl(WxCpApiPathConsts.Agent.AGENT_GET_ADMIN_LIST), requestJson.toString())).thenReturn(returnJson);
171+
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService));
172+
173+
WxCpAgentService wxAgentService = this.wxService.getAgentService();
174+
WxCpTpAdmin adminList = wxAgentService.getAdminList(9);
175+
176+
assertEquals(0, adminList.getErrcode().intValue());
177+
assertEquals(2, adminList.getAdmin().size());
178+
assertEquals("zhangsan", adminList.getAdmin().get(0).getUserId());
179+
assertEquals("woAJ2GCAAAXtWyujaWJHDDGi0mACH71w", adminList.getAdmin().get(0).getOpenUserId());
180+
assertEquals(1, adminList.getAdmin().get(0).getAuthType().intValue());
181+
}
182+
121183
}
122184

123185
}

0 commit comments

Comments
 (0)