| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package com.goldenwater.web.controller.efinder;
- import com.goldenwater.slgc.entity.efinder.DocFile;
- import com.goldenwater.slgc.service.efinder.IDocFileService;
- import com.goldenwater.common.core.controller.BaseController;
- import com.goldenwater.common.core.domain.AjaxResult;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import com.goldenwater.common.annotation.Log;
- import com.goldenwater.common.enums.BusinessType;
- import jakarta.servlet.http.HttpServletResponse;
- import io.swagger.v3.oas.annotations.Operation;
- import io.swagger.v3.oas.annotations.Parameter;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- @Tag(name = "01.03 文件管理-文档编辑", description = "文档在线编辑相关接口")
- @RestController
- @RequestMapping("/efinder/pageoffice")
- public class PageOfficeController extends BaseController {
- @Autowired
- private IDocFileService docFileService;
- /**
- * 获取文件信息(供前端打开编辑器)
- */
- @Operation(summary = "获取文件信息")
- @GetMapping("/open/{fileId}")
- public AjaxResult open(@Parameter(description = "文件ID") @PathVariable Long fileId) {
- DocFile file = docFileService.selectById(fileId);
- if (file == null) {
- return error("文件不存在");
- }
- Map<String, Object> result = new HashMap<>();
- result.put("id", file.getId());
- result.put("name", file.getName());
- result.put("path", file.getPath());
- result.put("type", file.getType());
- result.put("size", file.getSize());
- return success(result);
- }
- /**
- * 下载Word文档(供前端编辑前下载)
- */
- @Operation(summary = "下载Word文档")
- @GetMapping("/download/{fileId}")
- public void download(@Parameter(description = "文件ID") @PathVariable Long fileId, HttpServletResponse response) {
- docFileService.download(fileId, response);
- }
- /**
- * 保存编辑后的Word文档(上传覆盖原文件)
- */
- @Operation(summary = "保存编辑后的Word文档")
- @Log(title = "PageOffice文档保存", businessType = BusinessType.UPDATE)
- @PostMapping("/save/{fileId}")
- public AjaxResult save(@Parameter(description = "文件ID") @PathVariable Long fileId,
- @Parameter(description = "上传的文件") @RequestParam("file") MultipartFile file) {
- try {
- DocFile docFile = docFileService.selectById(fileId);
- if (docFile == null) {
- return error("文件不存在");
- }
- String absPath = docFile.getPath();
- java.io.File dest = new java.io.File(absPath);
- if (dest.getParentFile() != null && !dest.getParentFile().exists()) {
- dest.getParentFile().mkdirs();
- }
- file.transferTo(dest);
- docFile.setSize(file.getSize());
- docFileService.update(docFile);
- return success("保存成功");
- } catch (IOException e) {
- return error("保存失败: " + e.getMessage());
- }
- }
- /**
- * 返回在线编辑器HTML页面(iframe嵌入方式)
- * 前端可通过 iframe 加载此页面实现文档预览
- */
- @Operation(summary = "返回在线编辑器HTML页面")
- @GetMapping("/editor/{fileId}")
- public void editor(@Parameter(description = "文件ID") @PathVariable Long fileId, HttpServletResponse response) {
- DocFile file = docFileService.selectById(fileId);
- if (file == null) {
- return;
- }
- try {
- response.setContentType("text/html;charset=UTF-8");
- StringBuilder html = new StringBuilder();
- html.append("<!DOCTYPE html><html><head><meta charset='UTF-8'>");
- html.append("<title>").append(file.getName()).append(" - 在线编辑</title>");
- html.append("<style>");
- html.append("body{margin:0;padding:0;display:flex;flex-direction:column;height:100vh;font-family:sans-serif;}");
- html.append(".toolbar{padding:8px 16px;background:#f5f5f5;border-bottom:1px solid #ddd;display:flex;gap:8px;align-items:center;}");
- html.append(".toolbar .title{font-size:14px;font-weight:600;flex:1;}");
- html.append(".toolbar button{padding:4px 12px;cursor:pointer;border:1px solid #ccc;background:#fff;border-radius:3px;}");
- html.append(".toolbar button:hover{background:#e8e8e8;}");
- html.append(".content{flex:1;display:flex;align-items:center;justify-content:center;background:#fafafa;}");
- html.append(".content iframe{width:100%;height:100%;border:none;}");
- html.append(".hint{color:#999;font-size:14px;text-align:center;padding:20px;}");
- html.append("</style>");
- html.append("</head><body>");
- html.append("<div class='toolbar'>");
- html.append("<span class='title'>").append(file.getName()).append("</span>");
- html.append("<button onclick=\"window.open('/slgc/efinder/pageoffice/download/").append(fileId).append("','_blank')\">下载文档</button>");
- html.append("</div>");
- html.append("<div class='content'>");
- html.append("<div class='hint'>");
- html.append("<p>文档预览不支持直接在线编辑</p>");
- html.append("<p>请点击上方「下载文档」按钮下载到本地编辑</p>");
- html.append("<p>编辑完成后可通过页面上传功能保存修改</p>");
- html.append("</div>");
- html.append("</div>");
- html.append("</body></html>");
- response.getWriter().write(html.toString());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
|