From 8bd9f2e8dd75ecd9ef68d43398f6415ce6f7f532 Mon Sep 17 00:00:00 2001 From: zhangmei <645025773@qq.com> Date: 星期三, 19 三月 2025 15:22:16 +0800 Subject: [PATCH] 修改发票邮件 --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java | 30 +++++++ ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentMailUtil.java | 76 +++++++++--------- ruoyi-system/src/main/java/com/ruoyi/system/service/TInvoiceService.java | 3 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TInvoiceController.java | 54 +++++++++++++ ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TInvoiceServiceImpl.java | 63 --------------- 5 files changed, 126 insertions(+), 100 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TInvoiceController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TInvoiceController.java index 157d180..02fb59c 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TInvoiceController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TInvoiceController.java @@ -12,10 +12,16 @@ import com.ruoyi.system.query.TInvoiceQuery; import com.ruoyi.system.service.TBillService; import com.ruoyi.system.service.TInvoiceService; +import com.ruoyi.web.controller.tool.TencentCosUtil; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; /** @@ -33,6 +39,8 @@ private TInvoiceService invoiceService; @Autowired TBillService tBillService; + @Autowired + TencentCosUtil tencentCosUtil; @PreAuthorize("@ss.hasPermi('invoice:list')") @ApiOperation(value = "获取开票列表") @PostMapping("/list") @@ -58,7 +66,51 @@ @PostMapping("/uploadVoucher") @PreAuthorize("@ss.hasPermi('invoice:list:payment')") public R<Boolean> uploadVoucher(@RequestBody TInvoiceQuery query) { - return R.ok(invoiceService.uploadVoucher(query)); + String invoiceVoucher = query.getInvoiceVoucher(); + String invoiceVoucherName = query.getInvoiceVoucherName(); + if (invoiceVoucher == null || invoiceVoucherName == null) { + return R.fail("请上传发票文件"); + } + String[] voucherUrls = invoiceVoucher.split(","); + String[] voucherNames = invoiceVoucherName.split(","); + + // 确保两个数组长度一致 + int length = Math.min(voucherUrls.length, voucherNames.length); + if (length == 0) { + return R.fail("请上传发票文件"); + } + // 构建附件列表 + List<Map<String, String>> attachments = new ArrayList<>(length); + for (int i = 0; i < length; i++) { + String voucherUrl = voucherUrls[i]; + String fileName = voucherNames[i]; + Map<String, String> attachment = new HashMap<>(2); // 初始容量为2,避免扩容 + String tempDir = System.getProperty("java.io.tmpdir"); + Path filePath = Paths.get(tempDir, fileName); + // 保存到临时目录 + tencentCosUtil.download(voucherUrl,filePath.toString(),fileName); + + attachment.put("filePath", filePath.toString()); + attachment.put("fileName", fileName); + attachments.add(attachment); + } + return R.ok(invoiceService.uploadVoucher(query,attachments)); } } + + + + + + + + + + + + + + + + diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java index 3133f3a..9a9ac21 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java @@ -27,6 +27,11 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.Base64; import java.util.Date; import java.util.UUID; @@ -256,4 +261,29 @@ } + /** + * 将文件下载到指定目录 + * @param fileUrl + * @param saveDir + * @param fileName + * @throws IOException + */ + public void download(String fileUrl, String saveDir, String fileName){ + fileUrl = fileUrl.replace(cosConfig.getRootSrc(), ""); + // 下载文件并获取输入流 + COSObject object = cosClient.getObject(cosConfig.getBucketName(),fileUrl); + try ( + InputStream in = object.getObjectContent(); + ){ + Path targetPath = Paths.get(saveDir, fileName); + // 确保目录存在 + Files.createDirectories(targetPath.getParent()); + // 将文件保存到目标路径 + Files.copy(in, targetPath, StandardCopyOption.REPLACE_EXISTING); + }catch (IOException e){ + log.error("读取cos图片发生异常", e); + } + } + + } \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentMailUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentMailUtil.java index 1420362..64d39bb 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentMailUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/TencentMailUtil.java @@ -8,17 +8,19 @@ import javax.activation.DataHandler; import javax.activation.FileDataSource; -import java.net.URLEncoder; -import javax.activation.URLDataSource; +import javax.annotation.Resource; import javax.mail.*; import javax.mail.internet.*; -import javax.net.ssl.*; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.security.cert.X509Certificate; -import java.util.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; import java.util.concurrent.CompletableFuture; @Component @@ -132,14 +134,43 @@ // 设置邮件标题 message.setSubject("发票"); // 创建邮件内容 - Multipart multipart = createMultipart(list); + Multipart multipart = new MimeMultipart(); + // 添加文本消息部分 + BodyPart messageBodyPart = new MimeBodyPart(); + messageBodyPart.setHeader("Content-Type", "text/plain;charset=utf-8"); + messageBodyPart.setContent("您在小程序提交的开票申请已开票成功,请查看附件内容","text/html;charset=UTF-8"); + multipart.addBodyPart(messageBodyPart); + List<Path> tempFilePath = new ArrayList<>(); + // 添加附件部分 + for (Map<String, String> map : list) { + messageBodyPart = new MimeBodyPart(); + String filePath = map.get("filePath"); + String fileName = map.get("fileName"); + tempFilePath.add(Paths.get(filePath)); + FileDataSource source = new FileDataSource(filePath); + messageBodyPart.setDataHandler(new DataHandler(source)); + String filenameEncode = MimeUtility.encodeText(fileName, "UTF-8", "base64"); + // String encodedFileName = Base64.getEncoder().encodeToString(fileName.getBytes(StandardCharsets.UTF_8)); + // String filenameEncode = MimeUtility.encodeText(encodedFileName); + messageBodyPart.setFileName(filenameEncode); + messageBodyPart.setHeader("Content-Transfer-Encoding", "base64"); + messageBodyPart.setHeader("Content-Disposition", "attachment"); + messageBodyPart.setHeader("Content-Type", "application/octet-stream;name=\"" + filenameEncode + "\""); + multipart.addBodyPart(messageBodyPart); + } // 设置邮件内容 message.setContent(multipart); // 发送邮件 Transport.send(message); + // 删除临时目录里面的文件 + for (Path path : tempFilePath) { + Files.deleteIfExists(path); + } } catch (MessagingException | UnsupportedEncodingException | MalformedURLException e) { log.error("发送邮件发生异常", e); throw new ServiceException("发送邮件失败, 请检查"); + } catch (IOException e) { + throw new RuntimeException("文件下载发生异常"); } } @@ -168,35 +199,6 @@ } }; } - - private Multipart createMultipart(List<Map<String, String>> list) throws MessagingException, UnsupportedEncodingException, MalformedURLException { - Multipart multipart = new MimeMultipart(); - // 添加文本消息部分 - BodyPart messageBodyPart = new MimeBodyPart(); - messageBodyPart.setHeader("Content-Type", "text/plain;charset=utf-8"); - messageBodyPart.setContent("您在小程序提交的开票申请已开票成功,请查看附件内容","text/html;charset=UTF-8"); - multipart.addBodyPart(messageBodyPart); - // 添加附件部分 - for (Map<String, String> map : list) { - messageBodyPart = new MimeBodyPart(); - String url = map.get("url"); - String fileName = map.get("fileName"); - URLDataSource source = new URLDataSource(new URL(url)); - messageBodyPart.setDataHandler(new DataHandler(source)); - String filenameEncode = MimeUtility.encodeText(fileName, "UTF-8", "base64"); - // String encodedFileName = Base64.getEncoder().encodeToString(fileName.getBytes(StandardCharsets.UTF_8)); - // String filenameEncode = MimeUtility.encodeText(encodedFileName); - messageBodyPart.setFileName(filenameEncode); - messageBodyPart.setHeader("Content-Transfer-Encoding", "base64"); - messageBodyPart.setHeader("Content-Disposition", "attachment"); - messageBodyPart.setHeader("Content-Type", "application/octet-stream;name=\"" + filenameEncode + "\""); - multipart.addBodyPart(messageBodyPart); - } - - return multipart; - } - - // public static void main(String[] args) throws UnsupportedEncodingException { // TencentMailUtil tencentMailUtil = new TencentMailUtil(); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/TInvoiceService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/TInvoiceService.java index 62a2e34..4b27c65 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/TInvoiceService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/TInvoiceService.java @@ -7,6 +7,7 @@ import com.ruoyi.system.query.TInvoiceQuery; import java.util.List; +import java.util.Map; /** * <p> @@ -19,5 +20,5 @@ public interface TInvoiceService extends IService<TInvoice> { PageInfo<TInvoice> pageList(TInvoiceQuery query); List<TInvoice> makeQuery(TInvoiceQuery query); - Boolean uploadVoucher(TInvoiceQuery query); + Boolean uploadVoucher(TInvoiceQuery query, List<Map<String, String>> map); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TInvoiceServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TInvoiceServiceImpl.java index c0be87b..20e23e5 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TInvoiceServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TInvoiceServiceImpl.java @@ -58,7 +58,7 @@ } @Override - public Boolean uploadVoucher(TInvoiceQuery query) { + public Boolean uploadVoucher(TInvoiceQuery query, List<Map<String, String>> map) { // 检查是否存在对应的发票记录 TInvoice preexist = getById(query.getId()); if (preexist == null) { @@ -74,17 +74,11 @@ tInvoice.setInvoiceTime(query.getInvoiceTime()); tInvoice.setStatus(2); - // 处理附件信息 - List<Map<String, String>> attachments = buildAttachments(query.getInvoiceVoucher(), query.getInvoiceVoucherName()); - if (attachments.isEmpty()) { - log.warn("未找到有效的附件信息"); - return updateById(tInvoice); - } // 异步发送邮件 CompletableFuture.runAsync(() -> { try { - mailUtil.sendInvoice(preexist.getEmail(), attachments); + mailUtil.sendInvoice(preexist.getEmail(), map); } catch (ServiceException e) { log.error("邮件发送失败", e); } @@ -93,57 +87,4 @@ // 更新数据库 return updateById(tInvoice); } - - private List<Map<String, String>> buildAttachments(String invoiceVoucher, String invoiceVoucherName) { - if (invoiceVoucher == null || invoiceVoucherName == null) { - return Collections.emptyList(); - } - - String[] voucherUrls = invoiceVoucher.split(","); - String[] voucherNames = invoiceVoucherName.split(","); - - // 确保两个数组长度一致 - int length = Math.min(voucherUrls.length, voucherNames.length); - if (length == 0) { - return Collections.emptyList(); - } - - // 构建附件列表 - List<Map<String, String>> attachments = new ArrayList<>(length); - for (int i = 0; i < length; i++) { - Map<String, String> attachment = new HashMap<>(2); // 初始容量为2,避免扩容 - attachment.put("url", voucherUrls[i]); - attachment.put("fileName", voucherNames[i]); - attachments.add(attachment); - } - - return attachments; - } - - // @Override - // public Boolean uploadVoucher(TInvoiceQuery query) { - // TInvoice preexist = getById(query.getId()); - // if (preexist == null){ - // return false; - // } - // TInvoice tInvoice = new TInvoice(); - // tInvoice.setId(query.getId()); - // tInvoice.setInvoiceVoucher(query.getInvoiceVoucher()); - // tInvoice.setInvoiceVoucherName(query.getInvoiceVoucherName()); - // tInvoice.setInvoiceTime(query.getInvoiceTime()); - // tInvoice.setStatus(2); - // List<Map<String, String>> mapArrayList = new ArrayList<>(); - // String invoiceVoucher = query.getInvoiceVoucher(); - // String invoiceVoucherName = query.getInvoiceVoucherName(); - // - // List<String> list = Arrays.stream(invoiceVoucher.split(",")).collect(Collectors.toList()); - // for (int i = 0; i < list.size()-1; i++) { - // Map<String, String> map = new HashMap<>(); - // map.put("url", list.get(i)); - // map.put("fileName",invoiceVoucherName.split(",")[i]); - // mapArrayList.add(map); - // } - // mailUtil.sendInvoice(preexist.getEmail(),mapArrayList); - // return updateById(tInvoice); - // } } -- Gitblit v1.7.1