From 2a169bacadbb0a6971126a7940b46cd5c5e47b92 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Thu, 31 Mar 2022 21:59:46 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#2537=20=E3=80=90=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E3=80=91=E9=83=A8=E9=97=A8=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=AD=90=E9=83=A8=E9=97=A8?= =?UTF-8?q?ID=E5=88=97=E8=A1=A8=E5=92=8C=E8=8E=B7=E5=8F=96=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E9=83=A8=E9=97=A8=E8=AF=A6=E6=83=85=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpDepartmentService.java | 24 +++++++++++++ .../api/impl/WxCpDepartmentServiceImpl.java | 28 +++++++++++++++ .../weixin/cp/constant/WxCpApiPathConsts.java | 2 ++ .../impl/WxCpDepartmentServiceImplTest.java | 34 +++++++++++++++---- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java index c86816b7f..b8e43cbdc 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java @@ -28,6 +28,18 @@ public interface WxCpDepartmentService { */ Long create(WxCpDepart depart) throws WxErrorException; + /** + *
+   * 部门管理接口 - 获取单个部门详情.
+   * 详情请见: https://developer.work.weixin.qq.com/document/path/95351
+   * 
+ * + * @param id 部门id + * @return 部门信息 + * @throws WxErrorException 异常 + */ + WxCpDepart get(Long id) throws WxErrorException; + /** *
    * 部门管理接口 - 获取部门列表.
@@ -40,6 +52,18 @@ public interface WxCpDepartmentService {
    */
   List list(Long id) throws WxErrorException;
 
+  /**
+   * 
+   * 部门管理接口 - 获取子部门ID列表.
+   * 详情请见: https://developer.work.weixin.qq.com/document/path/95350
+   * 
+ * + * @param id 部门id。获取指定部门及其下的子部门(以及子部门的子部门等等,递归)。 如果不填,默认获取全量组织架构 + * @return 子部门ID列表 + * @throws WxErrorException 异常 + */ + List simpleList(Long id) throws WxErrorException; + /** *
    * 部门管理接口 - 更新部门.
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
index 3a5ef8798..b6d9cf29b 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java
@@ -35,6 +35,18 @@ public class WxCpDepartmentServiceImpl implements WxCpDepartmentService {
     return GsonHelper.getAsLong(tmpJsonObject.get("id"));
   }
 
+  @Override
+  public WxCpDepart get(Long id) throws WxErrorException {
+    String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(DEPARTMENT_GET), id);
+    String responseContent = this.mainService.get(url, null);
+    JsonObject tmpJsonObject = GsonParser.parse(responseContent);
+    return WxCpGsonBuilder.create()
+      .fromJson(tmpJsonObject.get("department"),
+        new TypeToken() {
+        }.getType()
+      );
+  }
+
   @Override
   public void update(WxCpDepart group) throws WxErrorException {
     String url = this.mainService.getWxCpConfigStorage().getApiUrl(DEPARTMENT_UPDATE);
@@ -62,4 +74,20 @@ public class WxCpDepartmentServiceImpl implements WxCpDepartmentService {
         }.getType()
       );
   }
+
+  @Override
+  public List simpleList(Long id) throws WxErrorException {
+    String url = this.mainService.getWxCpConfigStorage().getApiUrl(DEPARTMENT_SIMPLE_LIST);
+    if (id != null) {
+      url += "?id=" + id;
+    }
+
+    String responseContent = this.mainService.get(url, null);
+    JsonObject tmpJsonObject = GsonParser.parse(responseContent);
+    return WxCpGsonBuilder.create()
+      .fromJson(tmpJsonObject.get("department_id"),
+        new TypeToken>() {
+        }.getType()
+      );
+  }
 }
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
index 1b803cfdd..2155b4c61 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
@@ -71,8 +71,10 @@ public interface WxCpApiPathConsts {
   interface Department {
     String DEPARTMENT_CREATE = "/cgi-bin/department/create";
     String DEPARTMENT_UPDATE = "/cgi-bin/department/update";
+    String DEPARTMENT_GET = "/cgi-bin/department/get?id=%d";
     String DEPARTMENT_DELETE = "/cgi-bin/department/delete?id=%d";
     String DEPARTMENT_LIST = "/cgi-bin/department/list";
+    String DEPARTMENT_SIMPLE_LIST = "/cgi-bin/department/simplelist";
   }
 
   interface Media {
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java
index 57957d3fb..7417f8055 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java
@@ -1,13 +1,15 @@
 package me.chanjar.weixin.cp.api.impl;
 
-import java.util.List;
-
-import org.testng.annotations.*;
-
 import com.google.inject.Inject;
+import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.cp.api.ApiTestModule;
 import me.chanjar.weixin.cp.api.WxCpService;
 import me.chanjar.weixin.cp.bean.WxCpDepart;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+import java.util.List;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
@@ -36,11 +38,11 @@ public class WxCpDepartmentServiceImplTest {
   }
 
   @DataProvider
-  public Object[][] departIds(){
+  public Object[][] departIds() {
     return new Object[][]{
       {null},
-      {1},
-      {5}
+      {12L},
+      {5L}
     };
   }
 
@@ -70,4 +72,22 @@ public class WxCpDepartmentServiceImplTest {
     this.wxCpService.getDepartmentService().delete(this.depart.getId());
   }
 
+  @Test(dataProvider = "departIds")
+  public void testSimpleList(Long id) throws WxErrorException {
+    System.out.println("=================获取子部门ID列表");
+    List departList = this.wxCpService.getDepartmentService().simpleList(id);
+    assertThat(departList).isNotEmpty();
+    departList.forEach(System.out::println);
+  }
+
+  @Test(dataProvider = "departIds")
+  public void testGet(Long id) throws WxErrorException {
+    if (id == null) {
+      return;
+    }
+
+    WxCpDepart depart = this.wxCpService.getDepartmentService().get(id);
+    assertThat(depart).isNotNull();
+    System.out.println(depart);
+  }
 }