wlzboy
2025-11-07 2aebbc9601ab439707f69b08e467808df9f7549c
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());
        }
    }
    /**
     * 从微信mediaId上传附件(微信小程序专用)
     */
    @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);
        }
    }
}