| | |
| | | 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; |
| | |
| | | @Autowired |
| | | private ISysTaskService sysTaskService; |
| | | |
| | | @Autowired |
| | | private WechatConfig wechatConfig; |
| | | |
| | | /** |
| | | * 查询任务附件列表 |
| | | */ |
| | |
| | | @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 { |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |