PageOfficeController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.goldenwater.web.controller.efinder;
  2. import com.goldenwater.slgc.entity.efinder.DocFile;
  3. import com.goldenwater.slgc.service.efinder.IDocFileService;
  4. import com.goldenwater.common.core.controller.BaseController;
  5. import com.goldenwater.common.core.domain.AjaxResult;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import com.goldenwater.common.annotation.Log;
  10. import com.goldenwater.common.enums.BusinessType;
  11. import jakarta.servlet.http.HttpServletResponse;
  12. import io.swagger.v3.oas.annotations.Operation;
  13. import io.swagger.v3.oas.annotations.Parameter;
  14. import io.swagger.v3.oas.annotations.tags.Tag;
  15. import java.io.IOException;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. @Tag(name = "01.03 文件管理-文档编辑", description = "文档在线编辑相关接口")
  19. @RestController
  20. @RequestMapping("/efinder/pageoffice")
  21. public class PageOfficeController extends BaseController {
  22. @Autowired
  23. private IDocFileService docFileService;
  24. /**
  25. * 获取文件信息(供前端打开编辑器)
  26. */
  27. @Operation(summary = "获取文件信息")
  28. @GetMapping("/open/{fileId}")
  29. public AjaxResult open(@Parameter(description = "文件ID") @PathVariable Long fileId) {
  30. DocFile file = docFileService.selectById(fileId);
  31. if (file == null) {
  32. return error("文件不存在");
  33. }
  34. Map<String, Object> result = new HashMap<>();
  35. result.put("id", file.getId());
  36. result.put("name", file.getName());
  37. result.put("path", file.getPath());
  38. result.put("type", file.getType());
  39. result.put("size", file.getSize());
  40. return success(result);
  41. }
  42. /**
  43. * 下载Word文档(供前端编辑前下载)
  44. */
  45. @Operation(summary = "下载Word文档")
  46. @GetMapping("/download/{fileId}")
  47. public void download(@Parameter(description = "文件ID") @PathVariable Long fileId, HttpServletResponse response) {
  48. docFileService.download(fileId, response);
  49. }
  50. /**
  51. * 保存编辑后的Word文档(上传覆盖原文件)
  52. */
  53. @Operation(summary = "保存编辑后的Word文档")
  54. @Log(title = "PageOffice文档保存", businessType = BusinessType.UPDATE)
  55. @PostMapping("/save/{fileId}")
  56. public AjaxResult save(@Parameter(description = "文件ID") @PathVariable Long fileId,
  57. @Parameter(description = "上传的文件") @RequestParam("file") MultipartFile file) {
  58. try {
  59. DocFile docFile = docFileService.selectById(fileId);
  60. if (docFile == null) {
  61. return error("文件不存在");
  62. }
  63. String absPath = docFile.getPath();
  64. java.io.File dest = new java.io.File(absPath);
  65. if (dest.getParentFile() != null && !dest.getParentFile().exists()) {
  66. dest.getParentFile().mkdirs();
  67. }
  68. file.transferTo(dest);
  69. docFile.setSize(file.getSize());
  70. docFileService.update(docFile);
  71. return success("保存成功");
  72. } catch (IOException e) {
  73. return error("保存失败: " + e.getMessage());
  74. }
  75. }
  76. /**
  77. * 返回在线编辑器HTML页面(iframe嵌入方式)
  78. * 前端可通过 iframe 加载此页面实现文档预览
  79. */
  80. @Operation(summary = "返回在线编辑器HTML页面")
  81. @GetMapping("/editor/{fileId}")
  82. public void editor(@Parameter(description = "文件ID") @PathVariable Long fileId, HttpServletResponse response) {
  83. DocFile file = docFileService.selectById(fileId);
  84. if (file == null) {
  85. return;
  86. }
  87. try {
  88. response.setContentType("text/html;charset=UTF-8");
  89. StringBuilder html = new StringBuilder();
  90. html.append("<!DOCTYPE html><html><head><meta charset='UTF-8'>");
  91. html.append("<title>").append(file.getName()).append(" - 在线编辑</title>");
  92. html.append("<style>");
  93. html.append("body{margin:0;padding:0;display:flex;flex-direction:column;height:100vh;font-family:sans-serif;}");
  94. html.append(".toolbar{padding:8px 16px;background:#f5f5f5;border-bottom:1px solid #ddd;display:flex;gap:8px;align-items:center;}");
  95. html.append(".toolbar .title{font-size:14px;font-weight:600;flex:1;}");
  96. html.append(".toolbar button{padding:4px 12px;cursor:pointer;border:1px solid #ccc;background:#fff;border-radius:3px;}");
  97. html.append(".toolbar button:hover{background:#e8e8e8;}");
  98. html.append(".content{flex:1;display:flex;align-items:center;justify-content:center;background:#fafafa;}");
  99. html.append(".content iframe{width:100%;height:100%;border:none;}");
  100. html.append(".hint{color:#999;font-size:14px;text-align:center;padding:20px;}");
  101. html.append("</style>");
  102. html.append("</head><body>");
  103. html.append("<div class='toolbar'>");
  104. html.append("<span class='title'>").append(file.getName()).append("</span>");
  105. html.append("<button onclick=\"window.open('/slgc/efinder/pageoffice/download/").append(fileId).append("','_blank')\">下载文档</button>");
  106. html.append("</div>");
  107. html.append("<div class='content'>");
  108. html.append("<div class='hint'>");
  109. html.append("<p>文档预览不支持直接在线编辑</p>");
  110. html.append("<p>请点击上方「下载文档」按钮下载到本地编辑</p>");
  111. html.append("<p>编辑完成后可通过页面上传功能保存修改</p>");
  112. html.append("</div>");
  113. html.append("</div>");
  114. html.append("</body></html>");
  115. response.getWriter().write(html.toString());
  116. } catch (IOException e) {
  117. throw new RuntimeException(e);
  118. }
  119. }
  120. }