Skip to content

Commit 2a169ba

Browse files
committed
🎨 #2537 【企业微信】部门管理增加获取子部门ID列表和获取单个部门详情的接口
1 parent 94bd262 commit 2a169ba

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

Diff for: weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ public interface WxCpDepartmentService {
2828
*/
2929
Long create(WxCpDepart depart) throws WxErrorException;
3030

31+
/**
32+
* <pre>
33+
* 部门管理接口 - 获取单个部门详情.
34+
* 详情请见: https://developer.work.weixin.qq.com/document/path/95351
35+
* </pre>
36+
*
37+
* @param id 部门id
38+
* @return 部门信息
39+
* @throws WxErrorException 异常
40+
*/
41+
WxCpDepart get(Long id) throws WxErrorException;
42+
3143
/**
3244
* <pre>
3345
* 部门管理接口 - 获取部门列表.
@@ -40,6 +52,18 @@ public interface WxCpDepartmentService {
4052
*/
4153
List<WxCpDepart> list(Long id) throws WxErrorException;
4254

55+
/**
56+
* <pre>
57+
* 部门管理接口 - 获取子部门ID列表.
58+
* 详情请见: https://developer.work.weixin.qq.com/document/path/95350
59+
* </pre>
60+
*
61+
* @param id 部门id。获取指定部门及其下的子部门(以及子部门的子部门等等,递归)。 如果不填,默认获取全量组织架构
62+
* @return 子部门ID列表
63+
* @throws WxErrorException 异常
64+
*/
65+
List<WxCpDepart> simpleList(Long id) throws WxErrorException;
66+
4367
/**
4468
* <pre>
4569
* 部门管理接口 - 更新部门.

Diff for: weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java

+28
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public Long create(WxCpDepart depart) throws WxErrorException {
3535
return GsonHelper.getAsLong(tmpJsonObject.get("id"));
3636
}
3737

38+
@Override
39+
public WxCpDepart get(Long id) throws WxErrorException {
40+
String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(DEPARTMENT_GET), id);
41+
String responseContent = this.mainService.get(url, null);
42+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
43+
return WxCpGsonBuilder.create()
44+
.fromJson(tmpJsonObject.get("department"),
45+
new TypeToken<WxCpDepart>() {
46+
}.getType()
47+
);
48+
}
49+
3850
@Override
3951
public void update(WxCpDepart group) throws WxErrorException {
4052
String url = this.mainService.getWxCpConfigStorage().getApiUrl(DEPARTMENT_UPDATE);
@@ -62,4 +74,20 @@ public List<WxCpDepart> list(Long id) throws WxErrorException {
6274
}.getType()
6375
);
6476
}
77+
78+
@Override
79+
public List<WxCpDepart> simpleList(Long id) throws WxErrorException {
80+
String url = this.mainService.getWxCpConfigStorage().getApiUrl(DEPARTMENT_SIMPLE_LIST);
81+
if (id != null) {
82+
url += "?id=" + id;
83+
}
84+
85+
String responseContent = this.mainService.get(url, null);
86+
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
87+
return WxCpGsonBuilder.create()
88+
.fromJson(tmpJsonObject.get("department_id"),
89+
new TypeToken<List<WxCpDepart>>() {
90+
}.getType()
91+
);
92+
}
6593
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ interface Chat {
7171
interface Department {
7272
String DEPARTMENT_CREATE = "/cgi-bin/department/create";
7373
String DEPARTMENT_UPDATE = "/cgi-bin/department/update";
74+
String DEPARTMENT_GET = "/cgi-bin/department/get?id=%d";
7475
String DEPARTMENT_DELETE = "/cgi-bin/department/delete?id=%d";
7576
String DEPARTMENT_LIST = "/cgi-bin/department/list";
77+
String DEPARTMENT_SIMPLE_LIST = "/cgi-bin/department/simplelist";
7678
}
7779

7880
interface Media {

Diff for: weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3-
import java.util.List;
4-
5-
import org.testng.annotations.*;
6-
73
import com.google.inject.Inject;
4+
import me.chanjar.weixin.common.error.WxErrorException;
85
import me.chanjar.weixin.cp.api.ApiTestModule;
96
import me.chanjar.weixin.cp.api.WxCpService;
107
import me.chanjar.weixin.cp.bean.WxCpDepart;
8+
import org.testng.annotations.DataProvider;
9+
import org.testng.annotations.Guice;
10+
import org.testng.annotations.Test;
11+
12+
import java.util.List;
1113

1214
import static org.assertj.core.api.Assertions.assertThat;
1315

@@ -36,11 +38,11 @@ public void testCreate() throws Exception {
3638
}
3739

3840
@DataProvider
39-
public Object[][] departIds(){
41+
public Object[][] departIds() {
4042
return new Object[][]{
4143
{null},
42-
{1},
43-
{5}
44+
{12L},
45+
{5L}
4446
};
4547
}
4648

@@ -70,4 +72,22 @@ public void testDelete() throws Exception {
7072
this.wxCpService.getDepartmentService().delete(this.depart.getId());
7173
}
7274

75+
@Test(dataProvider = "departIds")
76+
public void testSimpleList(Long id) throws WxErrorException {
77+
System.out.println("=================获取子部门ID列表");
78+
List<WxCpDepart> departList = this.wxCpService.getDepartmentService().simpleList(id);
79+
assertThat(departList).isNotEmpty();
80+
departList.forEach(System.out::println);
81+
}
82+
83+
@Test(dataProvider = "departIds")
84+
public void testGet(Long id) throws WxErrorException {
85+
if (id == null) {
86+
return;
87+
}
88+
89+
WxCpDepart depart = this.wxCpService.getDepartmentService().get(id);
90+
assertThat(depart).isNotNull();
91+
System.out.println(depart);
92+
}
7393
}

0 commit comments

Comments
 (0)