mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	🆕 #1675 企业微信增加更新、查询和删除日历的接口
This commit is contained in:
		| @ -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(); | ||||||
|  | } | ||||||
| @ -193,6 +193,10 @@ public class GsonHelper { | |||||||
|         jsonObject.addProperty(key.toString(), (Number) value); |         jsonObject.addProperty(key.toString(), (Number) value); | ||||||
|       } else if (value instanceof JsonElement) { |       } else if (value instanceof JsonElement) { | ||||||
|         jsonObject.add(key.toString(), (JsonElement) value); |         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 { |       } else { | ||||||
|         jsonObject.addProperty(key.toString(), value.toString()); |         jsonObject.addProperty(key.toString(), value.toString()); | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -3,6 +3,8 @@ package me.chanjar.weixin.cp.api; | |||||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||||
| import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar; | import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar; | ||||||
|  |  | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 企业微信日历接口. |  * 企业微信日历接口. | ||||||
|  * |  * | ||||||
| @ -26,4 +28,56 @@ public interface WxCpOaCalendarService { | |||||||
|    * @throws WxErrorException . |    * @throws WxErrorException . | ||||||
|    */ |    */ | ||||||
|   String add(WxCpOaCalendar calendar) 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; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,7 @@ | |||||||
| package me.chanjar.weixin.cp.api; | 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.bean.WxJsapiSignature; | ||||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||||
| import me.chanjar.weixin.common.session.WxSession; | import me.chanjar.weixin.common.session.WxSession; | ||||||
| @ -179,6 +181,26 @@ public interface WxCpService { | |||||||
|    */ |    */ | ||||||
|   String post(String url, String postData) throws WxErrorException; |   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请求 |    * 当不需要自动带accessToken的时候,可以用这个发起post请求 | ||||||
|    * |    * | ||||||
| @ -403,6 +425,7 @@ public interface WxCpService { | |||||||
|    * @return 群机器人消息推送服务 group robot service |    * @return 群机器人消息推送服务 group robot service | ||||||
|    */ |    */ | ||||||
|   WxCpGroupRobotService getGroupRobotService(); |   WxCpGroupRobotService getGroupRobotService(); | ||||||
|  |  | ||||||
|   /* |   /* | ||||||
|    * 获取工作台服务 |    * 获取工作台服务 | ||||||
|    * |    * | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ import com.google.gson.JsonArray; | |||||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||||
| import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||||
| import me.chanjar.weixin.common.api.WxConsts; | 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.bean.WxJsapiSignature; | ||||||
| import me.chanjar.weixin.common.enums.WxType; | import me.chanjar.weixin.common.enums.WxType; | ||||||
| import me.chanjar.weixin.common.error.WxError; | import me.chanjar.weixin.common.error.WxError; | ||||||
| @ -211,6 +212,16 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH | |||||||
|     return execute(SimplePostRequestExecutor.create(this), url, postData); |     return execute(SimplePostRequestExecutor.create(this), url, postData); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @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 |   @Override | ||||||
|   public String postWithoutToken(String url, String postData) throws WxErrorException { |   public String postWithoutToken(String url, String postData) throws WxErrorException { | ||||||
|     return this.executeNormal(SimplePostRequestExecutor.create(this), url, postData); |     return this.executeNormal(SimplePostRequestExecutor.create(this), url, postData); | ||||||
|  | |||||||
| @ -1,12 +1,18 @@ | |||||||
| package me.chanjar.weixin.cp.api.impl; | package me.chanjar.weixin.cp.api.impl; | ||||||
|  |  | ||||||
|  | import com.google.gson.reflect.TypeToken; | ||||||
| import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||||
| import me.chanjar.weixin.common.error.WxErrorException; | 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.WxCpOaCalendarService; | ||||||
| import me.chanjar.weixin.cp.api.WxCpService; | import me.chanjar.weixin.cp.api.WxCpService; | ||||||
| import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar; | 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 |   @Override | ||||||
|   public String add(WxCpOaCalendar calendar) throws WxErrorException { |   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)); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -7,6 +7,7 @@ import lombok.Builder; | |||||||
| import lombok.Data; | import lombok.Data; | ||||||
| import lombok.NoArgsConstructor; | import lombok.NoArgsConstructor; | ||||||
| import lombok.experimental.Accessors; | import lombok.experimental.Accessors; | ||||||
|  | import me.chanjar.weixin.common.bean.ToJson; | ||||||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | ||||||
|  |  | ||||||
| import java.io.Serializable; | import java.io.Serializable; | ||||||
| @ -23,9 +24,17 @@ import java.util.List; | |||||||
| @NoArgsConstructor | @NoArgsConstructor | ||||||
| @AllArgsConstructor | @AllArgsConstructor | ||||||
| @Accessors(chain = true) | @Accessors(chain = true) | ||||||
| public class WxCpOaCalendar implements Serializable { | public class WxCpOaCalendar implements Serializable, ToJson { | ||||||
|   private static final long serialVersionUID = -817988838579546989L; |   private static final long serialVersionUID = -817988838579546989L; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 变量名:cal_id | ||||||
|  |    * 是否必须:更新时必须提供 | ||||||
|  |    * 描述:日历ID | ||||||
|  |    */ | ||||||
|  |   @SerializedName("cal_id") | ||||||
|  |   private String calId; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 变量名:organizer |    * 变量名:organizer | ||||||
|    * 是否必须:是 |    * 是否必须:是 | ||||||
| @ -99,7 +108,8 @@ public class WxCpOaCalendar implements Serializable { | |||||||
|     private Integer readonly; |     private Integer readonly; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|   public String toJson() { |   public String toJson() { | ||||||
|     return WxCpGsonBuilder.create().toJson(ImmutableMap.of("calendar",this)); |     return WxCpGsonBuilder.create().toJson(ImmutableMap.of("calendar", this)); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -9,6 +9,9 @@ import org.testng.annotations.Guice; | |||||||
| import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||||
|  |  | ||||||
| import java.util.Arrays; | 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 { | public class WxCpOaCalendarServiceImplTest { | ||||||
|   @Inject |   @Inject | ||||||
|   protected WxCpService wxService; |   protected WxCpService wxService; | ||||||
|  |   private final String calId = "wcbBJNCQAARipW967iE8DKPAp5Kb96qQ"; | ||||||
|  |  | ||||||
|   @Test |   @Test | ||||||
|   public void testAdd() throws WxErrorException { |   public void testAdd() throws WxErrorException { | ||||||
|     this.wxService.getOaCalendarService().add(WxCpOaCalendar.builder() |     this.wxService.getOaCalendarService().add(WxCpOaCalendar.builder() | ||||||
|       .organizer("userid1") |       .organizer("binary") | ||||||
|       .readonly(1) |       .readonly(1) | ||||||
|       .setAsDefault(1) |       .setAsDefault(1) | ||||||
|       .summary("test_summary") |       .summary("test_summary") | ||||||
|       .color("#FF3030") |       .color("#FF3030") | ||||||
|       .description("test_describe") |       .description("test_describe") | ||||||
|       .shares(Arrays.asList(new WxCpOaCalendar.ShareInfo("userid2", null), |       .shares(Arrays.asList(new WxCpOaCalendar.ShareInfo("binary", null), | ||||||
|         new WxCpOaCalendar.ShareInfo("userid3", 1))) |         new WxCpOaCalendar.ShareInfo("binary", 1))) | ||||||
|       .build()); |       .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("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); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang