From 2aebbc9601ab439707f69b08e467808df9f7549c Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期五, 07 十一月 2025 11:50:26 +0800
Subject: [PATCH] feat: weixin
---
ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/SysTaskAttachmentController.java | 84 +++++++++++++++++++++++++++++++++++++++++-
1 files changed, 82 insertions(+), 2 deletions(-)
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/SysTaskAttachmentController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/SysTaskAttachmentController.java
index b12fa9e..d9e5490 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/SysTaskAttachmentController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/SysTaskAttachmentController.java
@@ -1,8 +1,13 @@
package com.ruoyi.web.controller.task;
import java.util.List;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
+import com.ruoyi.common.utils.WechatUtils;
+import com.ruoyi.common.config.WechatConfig;
import com.ruoyi.system.domain.SysTask;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,6 +38,9 @@
@Autowired
private ISysTaskService sysTaskService;
+
+ @Autowired
+ private WechatConfig wechatConfig;
/**
* 鏌ヨ浠诲姟闄勪欢鍒楄〃
@@ -50,9 +58,11 @@
@PreAuthorize("@ss.hasPermi('task:general:edit')")
@Log(title = "浠诲姟闄勪欢", businessType = BusinessType.INSERT)
@PostMapping("/upload/{taskId}")
- public AjaxResult upload(@PathVariable("taskId") Long taskId, @RequestParam("file") MultipartFile file) {
+ public AjaxResult upload(@PathVariable("taskId") Long taskId,
+ @RequestParam("file") MultipartFile file,
+ @RequestParam(value = "category", required = false) String category) {
try {
- int result = sysTaskService.uploadAttachment(taskId, file);
+ int result = sysTaskService.uploadAttachment(taskId, file, category);
if (result > 0) {
return success("涓婁紶鎴愬姛");
} else {
@@ -81,4 +91,74 @@
return error("鍒犻櫎澶辫触锛�" + e.getMessage());
}
}
+
+ /**
+ * 浠庡井淇ediaId涓婁紶闄勪欢锛堝井淇″皬绋嬪簭涓撶敤锛�
+ */
+ @PreAuthorize("@ss.hasPermi('task:general:edit')")
+ @Log(title = "浠诲姟闄勪欢", businessType = BusinessType.INSERT)
+ @PostMapping("/uploadFromWechat/{taskId}")
+ public AjaxResult uploadFromWechat(@PathVariable("taskId") Long taskId,
+ @RequestParam("mediaId") String mediaId,
+ @RequestParam(value = "category", required = false) String category) {
+ try {
+ // 鑾峰彇寰俊AccessToken
+ String accessToken = WechatUtils.getAccessToken(
+ wechatConfig.getAppId(),
+ wechatConfig.getAppSecret()
+ );
+ if (accessToken == null || accessToken.isEmpty()) {
+ return error("鑾峰彇寰俊AccessToken澶辫触");
+ }
+
+ // 閫氳繃mediaId涓婁紶闄勪欢
+ int result = sysTaskService.uploadAttachmentFromWechat(taskId, accessToken, mediaId, category);
+ if (result > 0) {
+ return success("涓婁紶鎴愬姛");
+ } else {
+ return error("涓婁紶澶辫触");
+ }
+ } catch (Exception e) {
+ return error("涓婁紶澶辫触锛�" + e.getMessage());
+ }
+ }
+
+ /**
+ * 涓嬭浇闄勪欢
+ */
+ @GetMapping("/download/{attachmentId}")
+ public void downloadAttachment(@PathVariable("attachmentId") Long attachmentId, HttpServletResponse response) {
+ try {
+ SysTaskAttachment attachment = sysTaskService.getAttachmentById(attachmentId);
+ if (attachment == null) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ File file = new File(attachment.getFilePath());
+ if (!file.exists()) {
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ // 璁剧疆鍝嶅簲澶�
+ response.setContentType("application/octet-stream");
+ response.setHeader("Content-Disposition", "attachment; filename=" + attachment.getFileName());
+ response.setContentLengthLong(file.length());
+
+ // 璇诲彇鏂囦欢骞惰緭鍑�
+ try (FileInputStream fis = new FileInputStream(file);
+ OutputStream os = response.getOutputStream()) {
+ byte[] buffer = new byte[4096];
+ int bytesRead;
+ while ((bytesRead = fis.read(buffer)) != -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+ os.flush();
+ }
+ } catch (Exception e) {
+ logger.error("涓嬭浇闄勪欢澶辫触", e);
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ }
+ }
}
--
Gitblit v1.9.1