Skip to content

Commit 0fb6991

Browse files
authored
🎨 #3928 【企业微信】修复第三方应用获取部门成员接口使用错误token的问题
1 parent e5b0923 commit 0fb6991

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpUserService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,29 @@ public interface WxCpTpUserService {
5353
* @param departId 必填。部门id
5454
* @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员
5555
* @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
56+
* @param corpId 企业id
5657
* @return the list
5758
* @throws WxErrorException the wx error exception
5859
*/
60+
List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status, String corpId)
61+
throws WxErrorException;
62+
63+
/**
64+
* <pre>
65+
* 获取部门成员.
66+
*
67+
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98
68+
* </pre>
69+
*
70+
* @param departId 必填。部门id
71+
* @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员
72+
* @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
73+
* @return the list
74+
* @throws WxErrorException the wx error exception
75+
* @deprecated 第三方应用调用此接口需要使用 corpId 对应的 access_token,请使用
76+
* {@link #listSimpleByDepartment(Long, Boolean, Integer, String)}
77+
*/
78+
@Deprecated
5979
List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status) throws WxErrorException;
6080

6181
/**

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpUserServiceImpl.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,42 @@ public List<WxCpUser> listByDepartment(Long departId, Boolean fetchChild, Intege
9797
}
9898

9999
@Override
100+
public List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status, String corpId)
101+
throws WxErrorException {
102+
StringBuilder params = new StringBuilder();
103+
if (fetchChild != null) {
104+
params.append("fetch_child=").append(fetchChild ? "1" : "0");
105+
}
106+
if (status != null) {
107+
if (params.length() > 0) {
108+
params.append('&');
109+
}
110+
params.append("status=").append(status);
111+
} else {
112+
if (params.length() > 0) {
113+
params.append('&');
114+
}
115+
params.append("status=0");
116+
}
117+
if (params.length() > 0) {
118+
params.append('&');
119+
}
120+
params.append("access_token=")
121+
.append(mainService.getWxCpTpConfigStorage().getAccessToken(corpId));
122+
123+
String url = mainService.getWxCpTpConfigStorage().getApiUrl(USER_SIMPLE_LIST + departId);
124+
String responseContent = this.mainService.get(url, params.toString(), true);
125+
JsonObject tmpJsonElement = GsonParser.parse(responseContent);
126+
return WxCpGsonBuilder.create()
127+
.fromJson(
128+
tmpJsonElement.getAsJsonObject().get("userlist"),
129+
new TypeToken<List<WxCpUser>>() {
130+
}.getType()
131+
);
132+
}
133+
134+
@Override
135+
@Deprecated
100136
public List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status)
101137
throws WxErrorException {
102138
String params = "";
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package me.chanjar.weixin.cp.tp.service.impl;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.WxCpUser;
5+
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
6+
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
7+
import me.chanjar.weixin.cp.tp.service.WxCpTpUserService;
8+
import org.mockito.Mock;
9+
import org.mockito.MockitoAnnotations;
10+
import org.testng.annotations.AfterClass;
11+
import org.testng.annotations.BeforeClass;
12+
import org.testng.annotations.Test;
13+
14+
import java.util.List;
15+
16+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.User.USER_SIMPLE_LIST;
17+
import static org.mockito.ArgumentMatchers.contains;
18+
import static org.mockito.ArgumentMatchers.eq;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.when;
21+
import static org.testng.Assert.assertNotNull;
22+
23+
/**
24+
* 企业微信-第三方开发-用户管理相关测试.
25+
*
26+
* @author GitHub Copilot
27+
*/
28+
public class WxCpTpUserServiceImplTest {
29+
30+
@Mock
31+
private WxCpTpServiceApacheHttpClientImpl wxCpTpService;
32+
33+
@Mock
34+
private WxCpTpConfigStorage configStorage;
35+
36+
private WxCpTpUserService wxCpTpUserService;
37+
38+
private AutoCloseable mockitoAnnotations;
39+
40+
/**
41+
* Sets up.
42+
*/
43+
@BeforeClass
44+
public void setUp() {
45+
mockitoAnnotations = MockitoAnnotations.openMocks(this);
46+
when(wxCpTpService.getWxCpTpConfigStorage()).thenReturn(configStorage);
47+
WxCpTpDefaultConfigImpl defaultConfig = new WxCpTpDefaultConfigImpl();
48+
when(configStorage.getApiUrl(contains(USER_SIMPLE_LIST)))
49+
.thenAnswer(invocation -> defaultConfig.getApiUrl(invocation.getArgument(0)));
50+
wxCpTpUserService = new WxCpTpUserServiceImpl(wxCpTpService);
51+
}
52+
53+
/**
54+
* Tear down.
55+
*
56+
* @throws Exception the exception
57+
*/
58+
@AfterClass
59+
public void tearDown() throws Exception {
60+
mockitoAnnotations.close();
61+
}
62+
63+
/**
64+
* 测试使用 corpId 的 listSimpleByDepartment 方法,验证请求使用了 access_token 而非 suite_access_token.
65+
*
66+
* @throws WxErrorException the wx error exception
67+
*/
68+
@Test
69+
public void testListSimpleByDepartmentWithCorpId() throws WxErrorException {
70+
Long departId = 1L;
71+
String corpId = "test_corp_id";
72+
String accessToken = "test_access_token";
73+
String result = "{\"errcode\":0,\"errmsg\":\"ok\",\"userlist\":[{\"userid\":\"zhangsan\",\"name\":\"张三\"}]}";
74+
75+
when(configStorage.getAccessToken(corpId)).thenReturn(accessToken);
76+
String url = new WxCpTpDefaultConfigImpl().getApiUrl(USER_SIMPLE_LIST + departId);
77+
when(wxCpTpService.get(eq(url), contains("access_token=" + accessToken), eq(true))).thenReturn(result);
78+
79+
List<WxCpUser> users = wxCpTpUserService.listSimpleByDepartment(departId, true, 0, corpId);
80+
assertNotNull(users);
81+
82+
// 验证调用时传入了 withoutSuiteAccessToken=true,确保不会附加 suite_access_token
83+
verify(wxCpTpService).get(eq(url), contains("access_token=" + accessToken), eq(true));
84+
}
85+
}

0 commit comments

Comments
 (0)