Skip to content

Commit

Permalink
feat:【企业微信】增加办公-发送邮件模块相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-Ho committed Jan 31, 2024
1 parent 2ec6a0f commit 2aca9c7
Show file tree
Hide file tree
Showing 6 changed files with 874 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.chanjar.weixin.cp.api;

import lombok.NonNull;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.bean.oa.mail.WxCpMailCommonSendRequest;
import me.chanjar.weixin.cp.bean.oa.mail.WxCpMailMeetingSendRequest;
import me.chanjar.weixin.cp.bean.oa.mail.WxCpMailScheduleSendRequest;

/**
* 企业微信y邮件相关接口.
* <a href="https://developer.work.weixin.qq.com/document/path/95486">邮件</a>
*
* @author Hugo
*/
public interface WxCpOaMailService {

/**
* 发送普通邮件
* 应用可以通过该接口发送普通邮件,支持附件能力。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: <a href="https://qyapi.weixin.qq.com/cgi-bin/exmail/app/compose_send?access_token=ACCESS_TOKEN">...</a>
*
* @param request 发送普通邮件请求参数
* @return wx cp base resp
* @throws WxErrorException the wx error exception
*/
WxCpBaseResp mailCommonSend(@NonNull WxCpMailCommonSendRequest request) throws WxErrorException;

/**
* 发送日程邮件
* 应用可以通过该接口发送日程邮件。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: <a href="https://qyapi.weixin.qq.com/cgi-bin/exmail/app/compose_send?access_token=ACCESS_TOKEN">...</a>
*
* @param request 发送日程邮件请求参数
* @return wx cp base resp
* @throws WxErrorException the wx error exception
*/
WxCpBaseResp mailScheduleSend(@NonNull WxCpMailScheduleSendRequest request) throws WxErrorException;

/**
* 发送会议邮件
* 应用可以通过该接口发送会议邮件。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: <a href="https://qyapi.weixin.qq.com/cgi-bin/exmail/app/compose_send?access_token=ACCESS_TOKEN">...</a>
*
* @param request 发送会议邮件请求参数
* @return wx cp base resp
* @throws WxErrorException the wx error exception
*/
WxCpBaseResp mailMeetingSend(@NonNull WxCpMailMeetingSendRequest request) throws WxErrorException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package me.chanjar.weixin.cp.api.impl;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpOaMailService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.bean.oa.mail.WxCpMailCommonSendRequest;
import me.chanjar.weixin.cp.bean.oa.mail.WxCpMailMeetingSendRequest;
import me.chanjar.weixin.cp.bean.oa.mail.WxCpMailScheduleSendRequest;

import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Oa.EXMAIL_APP_COMPOSE_SEND;

/**
* 企业微信邮件接口实现类.
*
* @author Hugo
*/
@Slf4j
@RequiredArgsConstructor
public class WxCpOMailServiceImpl implements WxCpOaMailService {
private final WxCpService cpService;

/**
* 发送普通邮件
* 应用可以通过该接口发送普通邮件,支持附件能力。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: <a href="https://qyapi.weixin.qq.com/cgi-bin/exmail/app/compose_send?access_token=ACCESS_TOKEN">...</a>
*
* @param request 发送普通邮件请求参数
* @return wx cp base resp
* @throws WxErrorException the wx error exception
*/
@Override
public WxCpBaseResp mailCommonSend(@NonNull WxCpMailCommonSendRequest request) throws WxErrorException {
return this.mailSend(request.toJson());
}

/**
* 发送日程邮件
* 应用可以通过该接口发送日程邮件。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: <a href="https://qyapi.weixin.qq.com/cgi-bin/exmail/app/compose_send?access_token=ACCESS_TOKEN">...</a>
*
* @param request 发送日程邮件请求参数
* @return wx cp base resp
* @throws WxErrorException the wx error exception
*/
@Override
public WxCpBaseResp mailScheduleSend(@NonNull WxCpMailScheduleSendRequest request) throws WxErrorException {
return this.mailSend(request.toJson());
}

/**
* 发送会议邮件
* 应用可以通过该接口发送会议邮件。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: <a href="https://qyapi.weixin.qq.com/cgi-bin/exmail/app/compose_send?access_token=ACCESS_TOKEN">...</a>
*
* @param request 发送会议邮件请求参数
* @return wx cp base resp
* @throws WxErrorException the wx error exception
*/
@Override
public WxCpBaseResp mailMeetingSend(@NonNull WxCpMailMeetingSendRequest request) throws WxErrorException {

return this.mailSend(request.toJson());
}

private WxCpBaseResp mailSend(String request) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(EXMAIL_APP_COMPOSE_SEND);
String responseContent = this.cpService.post(apiUrl, request);
return WxCpBaseResp.fromJson(responseContent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package me.chanjar.weixin.cp.bean.oa.mail;

import com.google.gson.annotations.SerializedName;
import lombok.*;
import lombok.experimental.Accessors;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;
import java.util.List;

/**
* 发送普通邮件请求.
*
* @author Hugo
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class WxCpMailCommonSendRequest implements Serializable {
private static final long serialVersionUID = -4961239393895454138L;

/**
* 收件人,to.emails 和 to.userids 至少传一个
*/
@SerializedName("to")
private TO to;

/**
* 抄送
*/
@SerializedName("cc")
private CC cc;

/**
* 文档类型, 3:文档 4:表格
*/
@SerializedName("bcc")
private BCC bcc;

/**
* 标题
*/
@SerializedName("subject")
private String subject;

/**
* 内容
*/
@SerializedName("content")
private String content;

/**
* 附件相关
*/
@SerializedName("attachment_list")
private List<Attachment> attachmentList;

/**
* 内容
*/
@SerializedName("content_type")
private String contentType;

/**
* 内容
*/
@SerializedName("enable_id_trans")
private Integer enableIdTrans;

@Getter
@Setter
public static class TO implements Serializable {
private static final long serialVersionUID = -4860239393895754598L;

/**
* 收件人,邮箱地址
*/
@SerializedName("emails")
private List<String> emails;

/**
* 收件人,企业内成员的userid
*/
@SerializedName("userids")
private List<String> userIds;

/**
* From json space info.
*
* @param json the json
* @return the space info
*/
public static WxCpMailCommonSendRequest.TO fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpMailCommonSendRequest.TO.class);
}

/**
* To json string.
*
* @return the string
*/
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}

@Getter
@Setter
public static class CC implements Serializable {
private static final long serialVersionUID = -4863239393895754598L;

/**
* 抄送人,邮箱地址
*/
@SerializedName("emails")
private List<String> emails;

/**
* 抄送人,企业内成员的userid
*/
@SerializedName("userids")
private List<String> userIds;

/**
* From json space info.
*
* @param json the json
* @return the space info
*/
public static WxCpMailCommonSendRequest.CC fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpMailCommonSendRequest.CC.class);
}

/**
* To json string.
*
* @return the string
*/
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}

@Getter
@Setter
public static class BCC implements Serializable {
private static final long serialVersionUID = -4860239393885754598L;

/**
* 密送人,邮箱地址
*/
@SerializedName("emails")
private List<String> emails;

/**
* 密送人,企业内成员的userid
*/
@SerializedName("userids")
private List<String> userIds;

/**
* From json space info.
*
* @param json the json
* @return the space info
*/
public static WxCpMailCommonSendRequest.BCC fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpMailCommonSendRequest.BCC.class);
}

/**
* To json string.
*
* @return the string
*/
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}

@Getter
@Setter
public static class Attachment implements Serializable {
private static final long serialVersionUID = -4860230393895754598L;

/**
* 文件名
*/
@SerializedName("file_name")
private String fileName;

/**
* 文件内容(base64编码),所有附件加正文的大小不允许超过50M, 且附件个数不能超过200个
*/
@SerializedName("content")
private String content;

/**
* From json space info.
*
* @param json the json
* @return the space info
*/
public static WxCpMailCommonSendRequest.Attachment fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpMailCommonSendRequest.Attachment.class);
}

/**
* To json string.
*
* @return the string
*/
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}

/**
* From json wx cp space create request.
*
* @param json the json
* @return the wx cp space create request
*/
public static WxCpMailCommonSendRequest fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpMailCommonSendRequest.class);
}

/**
* To json string.
*
* @return the string
*/
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}
Loading

0 comments on commit 2aca9c7

Please sign in to comment.