feat(定时报告): 邮件支持pdf附件格式

This commit is contained in:
fit2cloud-chenyw 2023-06-19 18:04:50 +08:00
parent dfa7e6bb55
commit f1c757fe6f
7 changed files with 72 additions and 10 deletions

View File

@ -35,7 +35,10 @@ import io.dataease.service.system.EmailService;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.quartz.*; import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils; import org.springframework.web.util.HtmlUtils;
@ -173,10 +176,6 @@ public class EmailTaskHandler extends TaskHandler implements Job {
String url = panelUrl(panelId); String url = panelUrl(panelId);
token = tokenByUser(user); token = tokenByUser(user);
XpackPixelEntity xpackPixelEntity = buildPixel(emailTemplateDTO); XpackPixelEntity xpackPixelEntity = buildPixel(emailTemplateDTO);
LogUtil.info("url is " + url);
LogUtil.info("token is " + token);
byte[] bytes = emailXpackService.printData(url, token, xpackPixelEntity);
LogUtil.info("picture of " + url + " is finished");
// 下面继续执行发送邮件的 // 下面继续执行发送邮件的
String recipients = emailTemplateDTO.getRecipients(); String recipients = emailTemplateDTO.getRecipients();
String reciUsers = emailTemplateDTO.getReciUsers(); String reciUsers = emailTemplateDTO.getReciUsers();
@ -232,7 +231,15 @@ public class EmailTaskHandler extends TaskHandler implements Job {
case "email": case "email":
if (StringUtils.isNotBlank(recipients)) if (StringUtils.isNotBlank(recipients))
try { try {
emailService.sendWithImageAndFiles(recipients, emailTemplateDTO.getTitle(), contentStr, bytes, files); Integer panelFormat = emailTemplateDTO.getPanelFormat();
if (ObjectUtils.isEmpty(panelFormat) || panelFormat == 0) {
byte[] bytes = emailXpackService.printData(url, token, xpackPixelEntity);
emailService.sendWithImageAndFiles(recipients, emailTemplateDTO.getTitle(), contentStr, bytes, files);
} else {
byte[] bytes = emailXpackService.printPdf(url, token, xpackPixelEntity, false, true);
emailService.sendPdfWithFiles(recipients, emailTemplateDTO.getTitle(), contentStr, bytes, files);
}
} catch (Exception e) { } catch (Exception e) {
errorMsgs.add("email: " + e.getMessage()); errorMsgs.add("email: " + e.getMessage());
} }
@ -253,6 +260,7 @@ public class EmailTaskHandler extends TaskHandler implements Job {
if (CollectionUtils.isNotEmpty(wecomUsers)) { if (CollectionUtils.isNotEmpty(wecomUsers)) {
WecomXpackService wecomXpackService = SpringContextUtil.getBean(WecomXpackService.class); WecomXpackService wecomXpackService = SpringContextUtil.getBean(WecomXpackService.class);
byte[] bytes = emailXpackService.printData(url, token, xpackPixelEntity);
WecomMsgResult wecomMsgResult = wecomXpackService.pushOaMsg(wecomUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files); WecomMsgResult wecomMsgResult = wecomXpackService.pushOaMsg(wecomUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files);
if (wecomMsgResult.getErrcode() != 0) { if (wecomMsgResult.getErrcode() != 0) {
errorMsgs.add("wecom: " + wecomMsgResult.getErrmsg()); errorMsgs.add("wecom: " + wecomMsgResult.getErrmsg());
@ -277,6 +285,7 @@ public class EmailTaskHandler extends TaskHandler implements Job {
if (CollectionUtils.isNotEmpty(dingTalkUsers)) { if (CollectionUtils.isNotEmpty(dingTalkUsers)) {
DingtalkXpackService dingtalkXpackService = SpringContextUtil.getBean(DingtalkXpackService.class); DingtalkXpackService dingtalkXpackService = SpringContextUtil.getBean(DingtalkXpackService.class);
byte[] bytes = emailXpackService.printData(url, token, xpackPixelEntity);
DingtalkMsgResult dingtalkMsgResult = dingtalkXpackService.pushOaMsg(dingTalkUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files); DingtalkMsgResult dingtalkMsgResult = dingtalkXpackService.pushOaMsg(dingTalkUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files);
if (dingtalkMsgResult.getErrcode() != 0) { if (dingtalkMsgResult.getErrcode() != 0) {
errorMsgs.add("dingtalk: " + dingtalkMsgResult.getErrmsg()); errorMsgs.add("dingtalk: " + dingtalkMsgResult.getErrmsg());
@ -301,6 +310,7 @@ public class EmailTaskHandler extends TaskHandler implements Job {
if (CollectionUtils.isNotEmpty(larkUsers)) { if (CollectionUtils.isNotEmpty(larkUsers)) {
LarkXpackService larkXpackService = SpringContextUtil.getBean(LarkXpackService.class); LarkXpackService larkXpackService = SpringContextUtil.getBean(LarkXpackService.class);
byte[] bytes = emailXpackService.printData(url, token, xpackPixelEntity);
LarkMsgResult larkMsgResult = larkXpackService.pushOaMsg(larkUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files); LarkMsgResult larkMsgResult = larkXpackService.pushOaMsg(larkUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files);
if (larkMsgResult.getCode() != 0) { if (larkMsgResult.getCode() != 0) {
errorMsgs.add("lark: " + larkMsgResult.getMsg()); errorMsgs.add("lark: " + larkMsgResult.getMsg());
@ -325,6 +335,7 @@ public class EmailTaskHandler extends TaskHandler implements Job {
if (CollectionUtils.isNotEmpty(larksuiteUsers)) { if (CollectionUtils.isNotEmpty(larksuiteUsers)) {
LarksuiteXpackService larksuiteXpackService = SpringContextUtil.getBean(LarksuiteXpackService.class); LarksuiteXpackService larksuiteXpackService = SpringContextUtil.getBean(LarksuiteXpackService.class);
byte[] bytes = emailXpackService.printData(url, token, xpackPixelEntity);
LarksuiteMsgResult larksuiteMsgResult = larksuiteXpackService.pushOaMsg(larksuiteUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files); LarksuiteMsgResult larksuiteMsgResult = larksuiteXpackService.pushOaMsg(larksuiteUsers, emailTemplateDTO.getTitle(), contentStr, bytes, files);
if (larksuiteMsgResult.getCode() != 0) { if (larksuiteMsgResult.getCode() != 0) {
errorMsgs.add("larksuite: " + larksuiteMsgResult.getMsg()); errorMsgs.add("larksuite: " + larksuiteMsgResult.getMsg());

View File

@ -174,7 +174,7 @@ public class XEmailTaskServer {
String currentToken = ServletUtils.getToken(); String currentToken = ServletUtils.getToken();
Future<?> future = priorityExecutor.submit(() -> { Future<?> future = priorityExecutor.submit(() -> {
try { try {
return emailXpackService.printPdf(url, currentToken, buildPixel(request.getPixel()), request.isShowPageNo()); return emailXpackService.printPdf(url, currentToken, buildPixel(request.getPixel()), request.isShowPageNo(), false);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
DEException.throwException("预览失败,请联系管理员"); DEException.throwException("预览失败,请联系管理员");

View File

@ -83,8 +83,35 @@ public class EmailService {
} }
public void sendPdfWithFiles(String to, String title, String content, byte[] bytes, List<File> files) {
if (ArrayUtil.isEmpty(bytes)) {
send(to, title, content);
return;
}
if (StringUtils.isBlank(to))
return;
MailInfo mailInfo = proxy().mailInfo();
checkMailInfo(mailInfo);
JavaMailSenderImpl driver = driver(mailInfo);
MimeMultipart multipart = new MimeMultipart();
MimeMessage mimeMessage = driver.createMimeMessage();
try {
multipart = addContent(multipart, content);
multipart = addPdf(multipart, bytes);
if (CollectionUtils.isNotEmpty(files)) {
multipart = addFiles(multipart, files);
}
mimeMessage.setSubject(title);
mimeMessage.setFrom(driver.getUsername());
mimeMessage.setRecipients(Message.RecipientType.TO, to);
mimeMessage.setContent(multipart);
driver.send(mimeMessage);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
DEException.throwException(e);
}
}
public void sendWithImageAndFiles(String to, String title, String content, byte[] bytes, List<File> files) { public void sendWithImageAndFiles(String to, String title, String content, byte[] bytes, List<File> files) {
if (StringUtils.isBlank(to)) if (StringUtils.isBlank(to))
@ -116,7 +143,14 @@ public class EmailService {
} }
} }
private MimeMultipart addImage(MimeMultipart multipart, byte[] bytes, String content) throws Exception{ private MimeMultipart addContent(MimeMultipart multipart, String content) throws Exception {
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html; charset=gb2312");
multipart.addBodyPart(text);
return multipart;
}
private MimeMultipart addImage(MimeMultipart multipart, byte[] bytes, String content) throws Exception {
MimeBodyPart image = new MimeBodyPart(); MimeBodyPart image = new MimeBodyPart();
DataHandler png = new DataHandler(new ByteArrayDataSource(bytes, "image/png")); DataHandler png = new DataHandler(new ByteArrayDataSource(bytes, "image/png"));
String uuid = UUID.randomUUID().toString(); String uuid = UUID.randomUUID().toString();
@ -131,7 +165,17 @@ public class EmailService {
return multipart; return multipart;
} }
private MimeMultipart addFiles(MimeMultipart multipart, List<File> files) throws Exception{ private MimeMultipart addPdf(MimeMultipart multipart, byte[] bytes) throws Exception {
MimeBodyPart attach = new MimeBodyPart();
ByteArrayDataSource fileDataSource = new ByteArrayDataSource(bytes, "application/pdf");
attach.setDataHandler(new DataHandler(fileDataSource));
attach.setFileName(MimeUtility.encodeText("panel.pdf", "gb2312", null));
multipart.addBodyPart(attach);
multipart.setSubType("related");
return multipart;
}
private MimeMultipart addFiles(MimeMultipart multipart, List<File> files) throws Exception {
for (int i = 0; i < files.size(); i++) { for (int i = 0; i < files.size(); i++) {
File file = files.get(i); File file = files.get(i);

View File

@ -2,3 +2,7 @@ UPDATE `my_plugin`
SET `version` = '1.18.8' SET `version` = '1.18.8'
where `plugin_id` > 0 where `plugin_id` > 0
and `version` = '1.18.7'; and `version` = '1.18.7';
ALTER TABLE `sys_task_email`
ADD COLUMN `panel_format` INT(1) NOT NULL DEFAULT 0 COMMENT '仪表板格式0:jpeg,1:pdf' AFTER `panel_id`;

View File

@ -2711,6 +2711,7 @@ export default {
preview: 'Preview', preview: 'Preview',
emial_preview: 'Report preview', emial_preview: 'Report preview',
chart_data_range: 'View data range', chart_data_range: 'View data range',
panel_format: 'Panel format',
simple_repeat: 'Simple repeat', simple_repeat: 'Simple repeat',
once_a_day: 'Once a day', once_a_day: 'Once a day',
once_a_week: 'Once a week', once_a_week: 'Once a week',

View File

@ -2705,6 +2705,7 @@ export default {
preview: '預覽', preview: '預覽',
emial_preview: '報告預覽', emial_preview: '報告預覽',
chart_data_range: '視圖數據範圍', chart_data_range: '視圖數據範圍',
panel_format: '儀表板格式',
simple_repeat: '簡單重複', simple_repeat: '簡單重複',
once_a_day: '每天', once_a_day: '每天',
once_a_week: '每周', once_a_week: '每周',

View File

@ -2720,6 +2720,7 @@ export default {
preview: '预览', preview: '预览',
emial_preview: '报告预览', emial_preview: '报告预览',
chart_data_range: '视图数据范围', chart_data_range: '视图数据范围',
panel_format: '仪表板格式',
simple_repeat: '简单重复', simple_repeat: '简单重复',
once_a_day: '每天', once_a_day: '每天',
once_a_week: '每周', once_a_week: '每周',