Browse Source

:new: #1675 企业微信增加更新、查询和删除日历的接口

Binary Wang 4 years ago
parent
commit
64402ab1de

+ 15 - 0
weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/ToJson.java

@@ -0,0 +1,15 @@
+package me.chanjar.weixin.common.bean;
+
+/**
+ * 包含toJson()方法的接口.
+ *
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ * @date 2020-10-05
+ */
+public interface ToJson {
+  /**
+   * 转换为json字符串
+   * @return json字符串
+   */
+  String toJson();
+}

+ 4 - 0
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java

@@ -193,6 +193,10 @@ public class GsonHelper {
         jsonObject.addProperty(key.toString(), (Number) value);
       } else if (value instanceof JsonElement) {
         jsonObject.add(key.toString(), (JsonElement) value);
+      } else if (value instanceof List) {
+        JsonArray array = new JsonArray();
+        ((List<?>) value).forEach(a -> array.add(a.toString()));
+        jsonObject.add(key.toString(), array);
       } else {
         jsonObject.addProperty(key.toString(), value.toString());
       }

+ 54 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaCalendarService.java

@@ -3,6 +3,8 @@ package me.chanjar.weixin.cp.api;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar;
 
+import java.util.List;
+
 /**
  * 企业微信日历接口.
  *
@@ -26,4 +28,56 @@ public interface WxCpOaCalendarService {
    * @throws WxErrorException .
    */
   String add(WxCpOaCalendar calendar) throws WxErrorException;
+
+  /**
+   * 更新日历.
+   * <pre>
+   * 该接口用于修改指定日历的信息。
+   * 注意,更新操作是覆盖式,而不是增量式
+   * 企业微信需要更新到3.0.2及以上版本
+   * 请求方式: POST(HTTPS)
+   * 请求地址: https://qyapi.weixin.qq.com/cgi-bin/oa/calendar/update?access_token=ACCESS_TOKEN
+   *
+   * 文档地址:https://work.weixin.qq.com/api/doc/90000/90135/92619
+   * </pre>
+   *
+   * @param calendar 日历对象
+   * @throws WxErrorException .
+   */
+  void update(WxCpOaCalendar calendar) throws WxErrorException;
+
+  /**
+   * 获取日历.
+   * <pre>
+   * 该接口用于获取应用在企业内创建的日历信息。
+   *
+   * 注: 企业微信需要更新到3.0.2及以上版本
+   *
+   * 请求方式: POST(HTTPS)
+   * 请求地址: https://qyapi.weixin.qq.com/cgi-bin/oa/calendar/get?access_token=ACCESS_TOKEN
+   *
+   * 文档地址:https://work.weixin.qq.com/api/doc/90000/90135/92621
+   * </pre>
+   *
+   * @param calIds 日历id列表
+   * @return 日历对象列表
+   * @throws WxErrorException .
+   */
+  List<WxCpOaCalendar> get(List<String> calIds) throws WxErrorException;
+
+  /**
+   * 删除日历.
+   * <pre>
+   * 该接口用于删除指定日历。
+   * 注: 企业微信需要更新到3.0.2及以上版本
+   * 请求方式: POST(HTTPS)
+   * 请求地址: https://qyapi.weixin.qq.com/cgi-bin/oa/calendar/del?access_token=ACCESS_TOKEN
+   *
+   * 文档地址:https://work.weixin.qq.com/api/doc/90000/90135/92620
+   * </pre>
+   *
+   * @param calId 日历id
+   * @throws WxErrorException .
+   */
+  void delete(String calId) throws WxErrorException;
 }

+ 27 - 4
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java

@@ -1,5 +1,7 @@
 package me.chanjar.weixin.cp.api;
 
+import com.google.gson.JsonObject;
+import me.chanjar.weixin.common.bean.ToJson;
 import me.chanjar.weixin.common.bean.WxJsapiSignature;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.common.session.WxSession;
@@ -180,6 +182,26 @@ public interface WxCpService {
   String post(String url, String postData) throws WxErrorException;
 
   /**
+   * 内部使用.
+   *
+   * @param url        接口地址
+   * @param jsonObject 请求body的json对象
+   * @return the string
+   * @throws WxErrorException the wx error exception
+   */
+  String post(String url, JsonObject jsonObject) throws WxErrorException;
+
+  /**
+   * 内部使用.
+   *
+   * @param url 接口地址
+   * @param obj 请求body的对象,实现了ToJson接口
+   * @return the string
+   * @throws WxErrorException the wx error exception
+   */
+  String post(String url, ToJson obj) throws WxErrorException;
+
+  /**
    * 当不需要自动带accessToken的时候,可以用这个发起post请求
    *
    * @param url      接口地址
@@ -403,11 +425,12 @@ public interface WxCpService {
    * @return 群机器人消息推送服务 group robot service
    */
   WxCpGroupRobotService getGroupRobotService();
+
   /*
-  * 获取工作台服务
-  *
-  * @return the workbench service
-  * */
+   * 获取工作台服务
+   *
+   * @return the workbench service
+   * */
   WxCpAgentWorkBenchService getWorkBenchService();
 
   /**

+ 11 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java

@@ -5,6 +5,7 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.api.WxConsts;
+import me.chanjar.weixin.common.bean.ToJson;
 import me.chanjar.weixin.common.bean.WxJsapiSignature;
 import me.chanjar.weixin.common.enums.WxType;
 import me.chanjar.weixin.common.error.WxError;
@@ -212,6 +213,16 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
   }
 
   @Override
+  public String post(String url, JsonObject jsonObject) throws WxErrorException {
+    return this.post(url, jsonObject.toString());
+  }
+
+  @Override
+  public String post(String url, ToJson obj) throws WxErrorException {
+    return this.post(url, obj.toJson());
+  }
+
+  @Override
   public String postWithoutToken(String url, String postData) throws WxErrorException {
     return this.executeNormal(SimplePostRequestExecutor.create(this), url, postData);
   }

+ 28 - 2
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImpl.java

@@ -1,12 +1,18 @@
 package me.chanjar.weixin.cp.api.impl;
 
+import com.google.gson.reflect.TypeToken;
 import lombok.RequiredArgsConstructor;
 import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.util.json.GsonHelper;
+import me.chanjar.weixin.common.util.json.GsonParser;
 import me.chanjar.weixin.cp.api.WxCpOaCalendarService;
 import me.chanjar.weixin.cp.api.WxCpService;
 import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar;
+import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
 
-import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Oa.CALENDAR_ADD;
+import java.util.List;
+
+import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Oa.*;
 
 /**
  * .
@@ -20,6 +26,26 @@ public class WxCpOaCalendarServiceImpl implements WxCpOaCalendarService {
 
   @Override
   public String add(WxCpOaCalendar calendar) throws WxErrorException {
-    return this.wxCpService.post(this.wxCpService.getWxCpConfigStorage().getApiUrl(CALENDAR_ADD),calendar.toJson());
+    return this.wxCpService.post(this.wxCpService.getWxCpConfigStorage().getApiUrl(CALENDAR_ADD), calendar);
+  }
+
+  @Override
+  public void update(WxCpOaCalendar calendar) throws WxErrorException {
+    this.wxCpService.post(this.wxCpService.getWxCpConfigStorage().getApiUrl(CALENDAR_UPDATE), calendar);
+  }
+
+  @Override
+  public List<WxCpOaCalendar> get(List<String> calIds) throws WxErrorException {
+    String response = this.wxCpService.post(this.wxCpService.getWxCpConfigStorage().getApiUrl(CALENDAR_GET),
+      GsonHelper.buildJsonObject("cal_id_list", calIds));
+    return WxCpGsonBuilder.create().fromJson(GsonParser.parse(response).get("calendar_list").getAsJsonArray().toString(),
+      new TypeToken<List<WxCpOaCalendar>>() {
+      }.getType());
+  }
+
+  @Override
+  public void delete(String calId) throws WxErrorException {
+    this.wxCpService.post(this.wxCpService.getWxCpConfigStorage().getApiUrl(CALENDAR_DEL),
+      GsonHelper.buildJsonObject("cal_id", calId));
   }
 }

+ 12 - 2
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/calendar/WxCpOaCalendar.java

@@ -7,6 +7,7 @@ import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.experimental.Accessors;
+import me.chanjar.weixin.common.bean.ToJson;
 import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
 
 import java.io.Serializable;
@@ -23,10 +24,18 @@ import java.util.List;
 @NoArgsConstructor
 @AllArgsConstructor
 @Accessors(chain = true)
-public class WxCpOaCalendar implements Serializable {
+public class WxCpOaCalendar implements Serializable, ToJson {
   private static final long serialVersionUID = -817988838579546989L;
 
   /**
+   * 变量名:cal_id
+   * 是否必须:更新时必须提供
+   * 描述:日历ID
+   */
+  @SerializedName("cal_id")
+  private String calId;
+
+  /**
    * 变量名:organizer
    * 是否必须:是
    * 描述:指定的组织者userid。注意该字段指定后不可更新
@@ -99,7 +108,8 @@ public class WxCpOaCalendar implements Serializable {
     private Integer readonly;
   }
 
+  @Override
   public String toJson() {
-    return WxCpGsonBuilder.create().toJson(ImmutableMap.of("calendar",this));
+    return WxCpGsonBuilder.create().toJson(ImmutableMap.of("calendar", this));
   }
 }

+ 34 - 3
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImplTest.java

@@ -9,6 +9,9 @@ import org.testng.annotations.Guice;
 import org.testng.annotations.Test;
 
 import java.util.Arrays;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
 
 /**
  * 单元测试.
@@ -22,18 +25,46 @@ import java.util.Arrays;
 public class WxCpOaCalendarServiceImplTest {
   @Inject
   protected WxCpService wxService;
+  private final String calId = "wcbBJNCQAARipW967iE8DKPAp5Kb96qQ";
 
   @Test
   public void testAdd() throws WxErrorException {
     this.wxService.getOaCalendarService().add(WxCpOaCalendar.builder()
-      .organizer("userid1")
+      .organizer("binary")
+      .readonly(1)
+      .setAsDefault(1)
+      .summary("test_summary")
+      .color("#FF3030")
+      .description("test_describe")
+      .shares(Arrays.asList(new WxCpOaCalendar.ShareInfo("binary", null),
+        new WxCpOaCalendar.ShareInfo("binary", 1)))
+      .build());
+  }
+
+  @Test
+  public void testUpdate() throws WxErrorException {
+    this.wxService.getOaCalendarService().update(WxCpOaCalendar.builder()
+      .calId(calId)
+      .organizer("binary")
       .readonly(1)
       .setAsDefault(1)
       .summary("test_summary")
       .color("#FF3030")
       .description("test_describe")
-      .shares(Arrays.asList(new WxCpOaCalendar.ShareInfo("userid2", null),
-        new WxCpOaCalendar.ShareInfo("userid3", 1)))
+      .shares(Arrays.asList(new WxCpOaCalendar.ShareInfo("binary", null),
+        new WxCpOaCalendar.ShareInfo("binary", 1)))
       .build());
   }
+
+  @Test
+  public void testGet() throws WxErrorException {
+    final List<WxCpOaCalendar> calendars = this.wxService.getOaCalendarService()
+      .get(Arrays.asList(calId));
+    assertThat(calendars).isNotEmpty();
+  }
+
+  @Test
+  public void testDelete() throws WxErrorException {
+    this.wxService.getOaCalendarService().delete(calId);
+  }
 }