🆕 #1675 企业微信增加创建日历的接口,以及相关回调事件消息通知的支持

This commit is contained in:
Binary Wang 2020-09-20 15:37:40 +08:00
parent 6713787bb9
commit 1598c61e56
11 changed files with 316 additions and 9 deletions

View File

@ -0,0 +1,29 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar;
/**
* 企业微信日历接口.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-09-20
*/
public interface WxCpOaCalendarService {
/**
* 创建日历.
* <pre>
* 该接口用于通过应用在企业内创建一个日历
* : 企业微信需要更新到3.0.2及以上版本
* 请求方式 POSTHTTPS
* 请求地址 https://qyapi.weixin.qq.com/cgi-bin/oa/calendar/add?access_token=ACCESS_TOKEN
*
* 文档地址https://work.weixin.qq.com/api/doc/90000/90135/92618
* </pre>
*
* @param calendar 日历对象
* @return 日历ID
* @throws WxErrorException .
*/
String add(WxCpOaCalendar calendar) throws WxErrorException;
}

View File

@ -388,7 +388,14 @@ public interface WxCpService {
*
* @return the oa service
*/
WxCpOaService getOAService();
WxCpOaService getOaService();
/**
* 获取日历相关接口的服务类对象
*
* @return the menu service
*/
WxCpOaCalendarService getOaCalendarService();
/**
* 获取群机器人消息推送服务
@ -445,4 +452,5 @@ public interface WxCpService {
* @param tagService the tag service
*/
void setTagService(WxCpTagService tagService);
}

View File

@ -52,6 +52,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
private WxCpExternalContactService externalContactService = new WxCpExternalContactServiceImpl(this);
private WxCpGroupRobotService groupRobotService = new WxCpGroupRobotServiceImpl(this);
private WxCpMessageService messageService = new WxCpMessageServiceImpl(this);
private WxCpOaCalendarService oaCalendarService = new WxCpOaCalendarServiceImpl(this);
/**
* 全局的是否正在刷新access token的锁.
@ -305,7 +306,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
return null;
} catch (IOException e) {
log.error("\n【请求地址】: {}\n【请求参数】{}\n【异常信息】{}", uri, data, e.getMessage());
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).errorCode(-1).build(),e);
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).errorCode(-1).build(), e);
}
}
@ -421,10 +422,15 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
}
@Override
public WxCpOaService getOAService() {
public WxCpOaService getOaService() {
return oaService;
}
@Override
public WxCpOaCalendarService getOaCalendarService() {
return this.oaCalendarService;
}
@Override
public WxCpGroupRobotService getGroupRobotService() {
return groupRobotService;

View File

@ -0,0 +1,25 @@
package me.chanjar.weixin.cp.api.impl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpOaCalendarService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Oa.CALENDAR_ADD;
/**
* .
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-09-20
*/
@RequiredArgsConstructor
public class WxCpOaCalendarServiceImpl implements WxCpOaCalendarService {
private final WxCpService wxCpService;
@Override
public String add(WxCpOaCalendar calendar) throws WxErrorException {
return this.wxCpService.post(this.wxCpService.getWxCpConfigStorage().getApiUrl(CALENDAR_ADD),calendar.toJson());
}
}

View File

@ -294,6 +294,20 @@ public class WxCpXmlMessage implements Serializable {
@XStreamConverter(value = XStreamCDataConverter.class)
private String address;
/**
* 日程ID.
*/
@XStreamAlias("ScheduleId")
@XStreamConverter(value = XStreamCDataConverter.class)
private String scheduleId;
/**
* 日历ID.
*/
@XStreamAlias("CalId")
@XStreamConverter(value = XStreamCDataConverter.class)
private String calId;
/**
* 扩展属性.
*/

View File

@ -0,0 +1,105 @@
package me.chanjar.weixin.cp.bean.oa.calendar;
import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
import java.util.List;
/**
* 日历.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-09-20
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class WxCpOaCalendar implements Serializable {
private static final long serialVersionUID = -817988838579546989L;
/**
* 变量名organizer
* 是否必须
* 描述指定的组织者userid注意该字段指定后不可更新
*/
@SerializedName("organizer")
private String organizer;
/**
* 变量名readonly
* 是否必须
* 描述日历组织者对日历是否只读权限即不可编辑日历不可在日历上添加日程仅可作为组织者删除日历0-1-默认为1即只读
*/
@SerializedName("readonly")
private Integer readonly;
/**
* 变量名set_as_default
* 是否必须
* 描述是否将该日历设置为组织者的默认日历0-1-默认为0即不设为默认日历
*/
@SerializedName("set_as_default")
private Integer setAsDefault;
/**
* 变量名summary
* 是否必须
* 描述日历标题1 ~ 128 字符
*/
@SerializedName("summary")
private String summary;
/**
* 变量名color
* 是否必须
* 描述日历在终端上显示的颜色RGB颜色编码16进制表示例如#0000FF 表示纯蓝色
*/
@SerializedName("color")
private String color;
/**
* 变量名description
* 是否必须
* 描述日历描述0 ~ 512 字符
*/
@SerializedName("description")
private String description;
/**
* 变量名shares
* 是否必须
* 描述日历共享成员列表最多2000人
*/
@SerializedName("shares")
private List<ShareInfo> shares;
@Data
@AllArgsConstructor
public static class ShareInfo implements Serializable {
private static final long serialVersionUID = -4882781114860754679L;
/**
* 日历共享成员的id
*/
private String userid;
/**
* 共享成员对日历是否只读权限即不可编辑日历不可在日历上添加日程仅可以退出日历
* 0-1-默认为1即只读
*/
private Integer readonly;
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(ImmutableMap.of("calendar",this));
}
}

View File

@ -101,6 +101,11 @@ public final class WxCpApiPathConsts {
public static final String GET_DIAL_RECORD = "/cgi-bin/dial/get_dial_record";
public static final String GET_TEMPLATE_DETAIL = "/cgi-bin/oa/gettemplatedetail";
public static final String APPLY_EVENT = "/cgi-bin/oa/applyevent";
public static final String CALENDAR_ADD = "/cgi-bin/oa/calendar/add";
public static final String CALENDAR_UPDATE = "/cgi-bin/oa/calendar/update";
public static final String CALENDAR_GET = "/cgi-bin/oa/calendar/get";
public static final String CALENDAR_DEL = "/cgi-bin/oa/calendar/del";
}
@UtilityClass

View File

@ -103,6 +103,30 @@ public class WxCpConsts {
*/
public static final String OPEN_APPROVAL_CHANGE = "open_approval_change";
/**
* 修改日历事件
*/
public static final String MODIFY_CALENDAR = "modify_calendar";
/**
* 删除日历事件
*/
public static final String DELETE_CALENDAR = "delete_calendar";
/**
* 添加日程事件
*/
public static final String ADD_SCHEDULE = "add_schedule";
/**
* 修改日程事件
*/
public static final String MODIFY_SCHEDULE = "modify_schedule";
/**
* 删除日程事件
*/
public static final String DELETE_SCHEDULE = "delete_schedule";
}

View File

@ -0,0 +1,39 @@
package me.chanjar.weixin.cp.api.impl;
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.oa.calendar.WxCpOaCalendar;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.util.Arrays;
/**
* 单元测试.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-09-20
*/
@Test
@Guice(modules = ApiTestModule.class)
public class WxCpOaCalendarServiceImplTest {
@Inject
protected WxCpService wxService;
@Test
public void testAdd() throws WxErrorException {
this.wxService.getOaCalendarService().add(WxCpOaCalendar.builder()
.organizer("userid1")
.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)))
.build());
}
}

View File

@ -37,7 +37,7 @@ public class WxCpOaServiceImplTest {
Date startTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-04-11");
Date endTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-05-10");
List<WxCpCheckinData> results = wxService.getOAService()
List<WxCpCheckinData> results = wxService.getOaService()
.getCheckinData(1, startTime, endTime, Lists.newArrayList("binary"));
assertThat(results).isNotNull();
@ -51,7 +51,7 @@ public class WxCpOaServiceImplTest {
public void testGetCheckinOption() throws WxErrorException {
Date now = new Date();
List<WxCpCheckinOption> results = wxService.getOAService().getCheckinOption(now, Lists.newArrayList("binary"));
List<WxCpCheckinOption> results = wxService.getOaService().getCheckinOption(now, Lists.newArrayList("binary"));
assertThat(results).isNotNull();
System.out.println("results ");
System.out.println(gson.toJson(results));
@ -61,7 +61,7 @@ public class WxCpOaServiceImplTest {
public void testGetApprovalInfo() throws WxErrorException, ParseException {
Date startTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-12-01");
Date endTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-12-31");
WxCpApprovalInfo result = wxService.getOAService().getApprovalInfo(startTime, endTime);
WxCpApprovalInfo result = wxService.getOaService().getApprovalInfo(startTime, endTime);
assertThat(result).isNotNull();
@ -72,7 +72,7 @@ public class WxCpOaServiceImplTest {
@Test
public void testGetApprovalDetail() throws WxErrorException {
String spNo = "201912020001";
WxCpApprovalDetailResult result = wxService.getOAService().getApprovalDetail(spNo);
WxCpApprovalDetailResult result = wxService.getOaService().getApprovalDetail(spNo);
assertThat(result).isNotNull();
@ -83,7 +83,7 @@ public class WxCpOaServiceImplTest {
@Test
public void testGetTemplateDetail() throws WxErrorException {
String templateId = "3TkZjxugodbqpEMk9j7X6h6zKqYkc7MxQrrFmT7H";
WxCpTemplateResult result = wxService.getOAService().getTemplateDetail(templateId);
WxCpTemplateResult result = wxService.getOaService().getTemplateDetail(templateId);
assertThat(result).isNotNull();
System.out.println("result ");
System.out.println(gson.toJson(result));
@ -91,7 +91,7 @@ public class WxCpOaServiceImplTest {
@Test
public void testApply() throws WxErrorException {
this.wxService.getOAService().apply(new WxCpOaApplyEventRequest().setCreatorUserId("123"));
this.wxService.getOaService().apply(new WxCpOaApplyEventRequest().setCreatorUserId("123"));
}
@Test

View File

@ -0,0 +1,52 @@
package me.chanjar.weixin.cp.bean.oa.calendar;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.testng.annotations.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 单元测试.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-09-20
*/
public class WxCpOaCalendarTest {
@Test
public void testToJson() {
String json = "{\n" +
" \"calendar\" : {\n" +
" \"organizer\" : \"userid1\",\n" +
" \"readonly\" : 1,\n" +
" \"set_as_default\" : 1,\n" +
" \"summary\" : \"test_summary\",\n" +
" \"color\" : \"#FF3030\",\n" +
" \"description\" : \"test_describe\",\n" +
" \"shares\" : [\n" +
" {\n" +
" \"userid\" : \"userid2\"\n" +
" },\n" +
" {\n" +
" \"userid\" : \"userid3\",\n" +
" \"readonly\" : 1\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n";
assertThat(WxCpOaCalendar.builder()
.organizer("userid1")
.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)))
.build().toJson())
.isEqualTo(GsonParser.parse(json).toString());
}
}