1
0
mirror of synced 2026-03-24 05:28:47 +08:00

🎨 #3928 【企业微信】修复第三方应用获取部门成员接口使用错误token的问题

This commit is contained in:
Copilot
2026-03-12 20:27:40 +08:00
committed by GitHub
parent e5b0923b63
commit 0fb69917b4
3 changed files with 141 additions and 0 deletions

View File

@@ -53,9 +53,29 @@ public interface WxCpTpUserService {
* @param departId 必填。部门id
* @param fetchChild 非必填。1/0是否递归获取子部门下面的成员
* @param status 非必填。0获取全部员工1获取已关注成员列表2获取禁用成员列表4获取未关注成员列表。status可叠加
* @param corpId 企业id
* @return the list
* @throws WxErrorException the wx error exception
*/
List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status, String corpId)
throws WxErrorException;
/**
* <pre>
* 获取部门成员.
*
* 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
* </pre>
*
* @param departId 必填。部门id
* @param fetchChild 非必填。1/0是否递归获取子部门下面的成员
* @param status 非必填。0获取全部员工1获取已关注成员列表2获取禁用成员列表4获取未关注成员列表。status可叠加
* @return the list
* @throws WxErrorException the wx error exception
* @deprecated 第三方应用调用此接口需要使用 corpId 对应的 access_token请使用
* {@link #listSimpleByDepartment(Long, Boolean, Integer, String)}
*/
@Deprecated
List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status) throws WxErrorException;
/**

View File

@@ -97,6 +97,42 @@ public class WxCpTpUserServiceImpl implements WxCpTpUserService {
}
@Override
public List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status, String corpId)
throws WxErrorException {
StringBuilder params = new StringBuilder();
if (fetchChild != null) {
params.append("fetch_child=").append(fetchChild ? "1" : "0");
}
if (status != null) {
if (params.length() > 0) {
params.append('&');
}
params.append("status=").append(status);
} else {
if (params.length() > 0) {
params.append('&');
}
params.append("status=0");
}
if (params.length() > 0) {
params.append('&');
}
params.append("access_token=")
.append(mainService.getWxCpTpConfigStorage().getAccessToken(corpId));
String url = mainService.getWxCpTpConfigStorage().getApiUrl(USER_SIMPLE_LIST + departId);
String responseContent = this.mainService.get(url, params.toString(), true);
JsonObject tmpJsonElement = GsonParser.parse(responseContent);
return WxCpGsonBuilder.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() {
}.getType()
);
}
@Override
@Deprecated
public List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status)
throws WxErrorException {
String params = "";

View File

@@ -0,0 +1,85 @@
package me.chanjar.weixin.cp.tp.service.impl;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
import me.chanjar.weixin.cp.tp.service.WxCpTpUserService;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.List;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.User.USER_SIMPLE_LIST;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertNotNull;
/**
* 企业微信-第三方开发-用户管理相关测试.
*
* @author GitHub Copilot
*/
public class WxCpTpUserServiceImplTest {
@Mock
private WxCpTpServiceApacheHttpClientImpl wxCpTpService;
@Mock
private WxCpTpConfigStorage configStorage;
private WxCpTpUserService wxCpTpUserService;
private AutoCloseable mockitoAnnotations;
/**
* Sets up.
*/
@BeforeClass
public void setUp() {
mockitoAnnotations = MockitoAnnotations.openMocks(this);
when(wxCpTpService.getWxCpTpConfigStorage()).thenReturn(configStorage);
WxCpTpDefaultConfigImpl defaultConfig = new WxCpTpDefaultConfigImpl();
when(configStorage.getApiUrl(contains(USER_SIMPLE_LIST)))
.thenAnswer(invocation -> defaultConfig.getApiUrl(invocation.getArgument(0)));
wxCpTpUserService = new WxCpTpUserServiceImpl(wxCpTpService);
}
/**
* Tear down.
*
* @throws Exception the exception
*/
@AfterClass
public void tearDown() throws Exception {
mockitoAnnotations.close();
}
/**
* 测试使用 corpId 的 listSimpleByDepartment 方法,验证请求使用了 access_token 而非 suite_access_token.
*
* @throws WxErrorException the wx error exception
*/
@Test
public void testListSimpleByDepartmentWithCorpId() throws WxErrorException {
Long departId = 1L;
String corpId = "test_corp_id";
String accessToken = "test_access_token";
String result = "{\"errcode\":0,\"errmsg\":\"ok\",\"userlist\":[{\"userid\":\"zhangsan\",\"name\":\"张三\"}]}";
when(configStorage.getAccessToken(corpId)).thenReturn(accessToken);
String url = new WxCpTpDefaultConfigImpl().getApiUrl(USER_SIMPLE_LIST + departId);
when(wxCpTpService.get(eq(url), contains("access_token=" + accessToken), eq(true))).thenReturn(result);
List<WxCpUser> users = wxCpTpUserService.listSimpleByDepartment(departId, true, 0, corpId);
assertNotNull(users);
// 验证调用时传入了 withoutSuiteAccessToken=true确保不会附加 suite_access_token
verify(wxCpTpService).get(eq(url), contains("access_token=" + accessToken), eq(true));
}
}