提交 f52e62b1 authored 作者: hzh's avatar hzh

邮件发送功能实现

上级 711bf8c2
......@@ -2,12 +2,16 @@ package org.dromara.common.core.utils.file;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.dromara.common.core.utils.StringUtils;
import java.io.File;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
......@@ -88,4 +92,46 @@ public class FileUtils extends FileUtil {
FileUtil.writeUtf8String(data, file);
return file;
}
/**
* 将 URL 转换为 File 对象(自动处理本地/网络 URL)
*
* @param urlStr URL 字符串(支持 file:/// 或 http(s):// 协议)
* @return 本地 File 对象(网络 URL 会先下载到临时目录)
* @throws Exception 转换失败时抛出异常
*/
public static File convert(String urlStr, String fileName) throws Exception {
URL url = URLUtil.url(urlStr);
// 1. 处理本地文件 URL(file 协议)
if ("file".equals(url.getProtocol())) {
return new File(url.getPath());
}
// 2. 处理网络 URL(http/https 等),需先下载到本地
else {
return downloadToLocal(urlStr, fileName);
}
}
/**
* 下载网络 URL 到本地临时目录
*/
private static File downloadToLocal(String urlStr, String fileName) {
try {
// 从 URL 中提取文件名
if (StringUtils.isEmpty(fileName)) {
fileName = urlStr.substring(urlStr.lastIndexOf("/") + 1);
}
// 生成临时文件路径(使用系统临时目录)
String tempDir = System.getProperty("java.io.tmpdir");
String localPath = tempDir + File.separator + fileName;
// 使用 Hutool 下载文件(支持超时和重试)
HttpUtil.downloadFile(urlStr, localPath);
return new File(localPath);
} catch (Exception e) {
throw new RuntimeException("URL 下载失败:" + urlStr, e);
}
}
}
......@@ -69,7 +69,6 @@ public class SysDeptOssBo extends BaseEntity {
/**
* 是否公开
*/
@NotNull(message = "是否公开不能为空", groups = { AddGroup.class, EditGroup.class })
private Boolean open;
/**
......
......@@ -9,8 +9,11 @@ import org.dromara.common.core.enums.BusinessStatusEnum;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.FileUtils;
import org.dromara.common.mail.utils.MailUtils;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.common.mybatis.service.AbstractBaseService;
import org.dromara.resource.api.RemoteFileService;
import org.dromara.workflow.api.domain.RemoteWorkflowService;
import org.dromara.workflow.api.domain.event.ProcessEvent;
import org.dromara.workflow.api.domain.event.ProcessTaskEvent;
......@@ -20,6 +23,7 @@ import org.dromara.workflow.domain.vo.FileApproveVo;
import org.dromara.workflow.mapper.FileApproveMapper;
import org.dromara.workflow.service.IFileApproveService;
import org.dromara.workflow.utils.QueryUtils;
import org.dromara.workflow.utils.WorkflowUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -43,6 +47,8 @@ public class FileApproveServiceImpl extends AbstractBaseService<FileApproveVo, F
private final FileApproveMapper baseMapper;
@DubboReference
private final RemoteWorkflowService workflowService;
@DubboReference
private final RemoteFileService remoteFileService;
@Override
public BaseMapperPlus<FileApprove, FileApproveVo> mapper() {
......@@ -107,6 +113,19 @@ public class FileApproveServiceImpl extends AbstractBaseService<FileApproveVo, F
fa.setStatus(BusinessStatusEnum.WAITING.getStatus());
}
baseMapper.updateById(fa);
//审批完成
if (StringUtils.equals(processEvent.getStatus(), BusinessStatusEnum.FINISH.getStatus())) {
try {
//发送邮件
Map<String, Object> entity = (Map<String, Object>) WorkflowUtils.getHistoricVariableByBusinessKey(processEvent.getBusinessKey(), "entity");
String url = entity.get("fileUrl").toString();
String fileName = entity.get("fileName").toString();
MailUtils.send(fa.getEmail(), "部门文件下载", "", false, FileUtils.convert(url, fileName));
} catch (Exception e) {
e.printStackTrace();
log.info("邮件发送失败,邮箱{}", fa.getEmail());
}
}
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论