forked from github/dataease
Merge pull request #1240 from dataease/pr@dev@feat_整合邮件和截图
feat: 整合邮件和截图
This commit is contained in:
commit
f50274dc1c
@ -5,20 +5,23 @@ import io.dataease.auth.entity.SysUserEntity;
|
||||
import io.dataease.auth.entity.TokenInfo;
|
||||
import io.dataease.auth.service.AuthUserService;
|
||||
import io.dataease.auth.util.JWTUtils;
|
||||
import io.dataease.commons.utils.CodingUtil;
|
||||
import io.dataease.commons.utils.CommonBeanFactory;
|
||||
import io.dataease.commons.utils.LogUtil;
|
||||
import io.dataease.job.sechedule.strategy.TaskHandler;
|
||||
|
||||
import io.dataease.plugins.common.entity.GlobalTaskEntity;
|
||||
import io.dataease.plugins.common.entity.GlobalTaskInstance;
|
||||
import io.dataease.plugins.config.SpringContextUtil;
|
||||
import io.dataease.plugins.xpack.email.dto.response.XpackEmailTemplateDTO;
|
||||
import io.dataease.plugins.xpack.email.service.EmailXpackService;
|
||||
import io.dataease.service.FileService;
|
||||
import io.dataease.service.system.EmailService;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.quartz.*;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@ -28,10 +31,10 @@ public class EmailTaskHandler extends TaskHandler implements Job {
|
||||
|
||||
|
||||
private static final Integer RUNING = 0;
|
||||
private static final Integer SUCCESS = 1;
|
||||
private static final Integer ERROR = -1;
|
||||
|
||||
|
||||
private AuthUserService authUserService;
|
||||
|
||||
|
||||
@Override
|
||||
protected JobDataMap jobDataMap(GlobalTaskEntity taskEntity) {
|
||||
@ -58,13 +61,9 @@ public class EmailTaskHandler extends TaskHandler implements Job {
|
||||
Long instanceId = saveInstance(taskInstance);
|
||||
taskInstance.setInstanceId(instanceId);
|
||||
|
||||
|
||||
XpackEmailTemplateDTO emailTemplate = (XpackEmailTemplateDTO) jobDataMap.get("emailTemplate");
|
||||
|
||||
|
||||
SysUserEntity creator = (SysUserEntity) jobDataMap.get("creator");
|
||||
|
||||
proxy().sendReport(taskEntity, taskInstance, emailTemplate, creator);
|
||||
proxy().sendReport(taskInstance, emailTemplate, creator);
|
||||
|
||||
}
|
||||
|
||||
@ -86,15 +85,41 @@ public class EmailTaskHandler extends TaskHandler implements Job {
|
||||
return taskInstance;
|
||||
}
|
||||
|
||||
private void changeStatus(GlobalTaskInstance taskInstance, Integer status) {
|
||||
taskInstance.setStatus(status);
|
||||
if(status == SUCCESS) {
|
||||
taskInstance.setFinishTime(System.currentTimeMillis());
|
||||
}
|
||||
EmailXpackService emailXpackService = SpringContextUtil.getBean(EmailXpackService.class);
|
||||
emailXpackService.saveInstance(taskInstance);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Async
|
||||
public void sendReport(GlobalTaskEntity taskEntity, GlobalTaskInstance taskInstance, XpackEmailTemplateDTO emailTemplateDTO, SysUserEntity user) {
|
||||
public void sendReport(GlobalTaskInstance taskInstance, XpackEmailTemplateDTO emailTemplateDTO, SysUserEntity user) {
|
||||
EmailXpackService emailXpackService = SpringContextUtil.getBean(EmailXpackService.class);
|
||||
String fileId = emailXpackService.print(panelUrl(emailTemplateDTO.getPanelId()), tokenByUser(user));
|
||||
// 下面继续执行发送邮件的略记
|
||||
String recipients = emailTemplateDTO.getRecipients();
|
||||
byte[] content = emailTemplateDTO.getContent();
|
||||
EmailService emailService = SpringContextUtil.getBean(EmailService.class);
|
||||
String contentStr = "";
|
||||
byte[] bytes = null;
|
||||
if (ObjectUtils.isNotEmpty(content)) {
|
||||
try {
|
||||
contentStr = new String(content, "UTF-8");
|
||||
FileService fileService = SpringContextUtil.getBean(FileService.class);
|
||||
bytes = fileService.loadFileAsBytes(fileId);
|
||||
emailService.sendWithImage(recipients, emailTemplateDTO.getTitle(), contentStr, bytes);
|
||||
changeStatus(taskInstance, SUCCESS);
|
||||
} catch (Exception e) {
|
||||
changeStatus(taskInstance, ERROR);
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -102,19 +127,22 @@ public class EmailTaskHandler extends TaskHandler implements Job {
|
||||
private String tokenByUser(SysUserEntity user) {
|
||||
TokenInfo tokenInfo = TokenInfo.builder().userId(user.getUserId()).username(user.getUsername()).build();
|
||||
String token = JWTUtils.sign(tokenInfo, user.getPassword());
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
private String panelUrl(String panelId) {
|
||||
InetAddress ip = null;
|
||||
String hostAddress = null;
|
||||
try {
|
||||
ip = InetAddress.getLocalHost();
|
||||
hostAddress = ip.getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Environment environment = SpringContextUtil.getBean(Environment.class);
|
||||
Integer port = environment.getProperty("server.port", Integer.class);
|
||||
String url = "http://" + ip + ":"+port + "/#/preview/" + panelId;
|
||||
String url = "http://" + hostAddress + ":"+port + "/#/preview/" + panelId;
|
||||
|
||||
return url;
|
||||
}
|
||||
|
@ -16,12 +16,19 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.activation.DataHandler;
|
||||
import javax.annotation.Resource;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import javax.mail.util.ByteArrayDataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@Service
|
||||
@ -72,6 +79,40 @@ public class EmailService {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendWithImage(String to, String title, String content, byte[] bytes) {
|
||||
if (StringUtils.isBlank(to)) return ;
|
||||
MailInfo mailInfo = proxy().mailInfo();
|
||||
JavaMailSenderImpl driver = driver(mailInfo);
|
||||
|
||||
MimeMessage mimeMessage = driver.createMimeMessage();
|
||||
MimeMessageHelper helper = null;
|
||||
|
||||
MimeBodyPart image = new MimeBodyPart();
|
||||
DataHandler png = new DataHandler(new ByteArrayDataSource(bytes, "image/png"));
|
||||
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
MimeBodyPart text = new MimeBodyPart();
|
||||
try {
|
||||
text.setContent(content + "<br/><img src='cid:"+uuid+"' />", "text/html; charset=gb2312");
|
||||
// text.setText(, "text/html; charset=gb2312");
|
||||
image.setDataHandler(png);
|
||||
image.setContentID(uuid);
|
||||
MimeMultipart multipart = new MimeMultipart();
|
||||
multipart.addBodyPart(text);
|
||||
multipart.addBodyPart(image);
|
||||
multipart.setSubType("related");
|
||||
mimeMessage.setFrom(driver.getUsername());
|
||||
mimeMessage.setSubject(title);
|
||||
mimeMessage.setRecipients(Message.RecipientType.TO, to);
|
||||
mimeMessage.setContent(multipart);
|
||||
driver.send(mimeMessage);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
// DEException.throwException(Translator.get("connection_failed"));
|
||||
}
|
||||
}
|
||||
|
||||
public JavaMailSenderImpl driver(MailInfo mailInfo) {
|
||||
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
|
||||
javaMailSender.setDefaultEncoding("UTF-8");
|
||||
|
Loading…
Reference in New Issue
Block a user