浏览代码

feat: onlyoffice 对接

1.更改报告生成文件存储
2.提供onlyoffice 所需参数
3.编写onlyoffice 回调函数
4.放行 word 开头链接
32197351@qq.com 2 年之前
父节点
当前提交
9e1410473e

+ 1 - 0
itdmServer/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java

@@ -99,6 +99,7 @@ public class ShiroConfig {
         filterChainDefinitionMap.put("/sys/getQrcodeToken/**", "anon"); //监听扫码
         filterChainDefinitionMap.put("/sys/checkAuth", "anon"); //授权接口排除
 
+        filterChainDefinitionMap.put("/word/**", "anon"); //word 下载排除
 
         filterChainDefinitionMap.put("/", "anon");
         filterChainDefinitionMap.put("/doc.html", "anon");

+ 16 - 0
itdmServer/module-iTDM/src/main/java/org/jeecg/modules/baogao/controller/ItdmBaogaoController.java

@@ -23,6 +23,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import lombok.extern.slf4j.Slf4j;
 
+import org.jeecg.modules.baogao.vo.ItdmBaogaoVO;
 import org.jeecg.modules.moban.entity.ItdmMoban;
 import org.jeecg.modules.weituo.entity.ItdmWeituoInfo;
 import org.jeecg.modules.weituo.entity.ItdmWeituoYangpin;
@@ -147,6 +148,21 @@ public class ItdmBaogaoController extends JeecgController<ItdmBaogao, IItdmBaoga
 
 
 
+    @ApiOperation(value = "onlyoffice 所需数据", notes = "onlyoffice 所需数据")
+
+
+    @GetMapping(value = "/wordById")
+    public  Result<ItdmBaogaoVO>  wordById(@RequestParam(name = "id", required = true) String id) {
+        ItdmBaogaoVO itdmBaogao = itdmBaogaoService.wordById(id);
+
+        if (itdmBaogao == null) {
+            return Result.error("未找到对应数据");
+        }
+        return Result.OK(itdmBaogao);
+    }
+
+
+
     @GetMapping(value = "/weituo/list")
     public Result<List<ItdmWeituoInfo>> queryweituoList() {
         List<ItdmWeituoInfo> pageList = itdmBaogaoService.queryweituoList();

+ 6 - 0
itdmServer/module-iTDM/src/main/java/org/jeecg/modules/baogao/service/IItdmBaogaoService.java

@@ -4,14 +4,17 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import org.apache.poi.ss.formula.functions.T;
+import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.baogao.dto.ItdmBaogaoCreateDTO;
 import org.jeecg.modules.baogao.entity.ItdmBaogao;
 import com.baomidou.mybatisplus.extension.service.IService;
 import org.jeecg.modules.baogao.vo.ItdmBaogaoListVO;
+import org.jeecg.modules.baogao.vo.ItdmBaogaoVO;
 import org.jeecg.modules.moban.entity.ItdmMoban;
 import org.jeecg.modules.weituo.entity.ItdmWeituoInfo;
 import org.jeecg.modules.weituo.entity.ItdmWeituoYangpin;
 import org.jeecg.modules.weituo.entity.ItdmWeituoYangpinExtend;
+import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.List;
 
@@ -40,4 +43,7 @@ public interface IItdmBaogaoService extends IService<ItdmBaogao> {
     void createBaogao(ItdmBaogaoCreateDTO itdmBaogao);
 
 
+    ItdmBaogaoVO wordById(String id);
+
+
 }

+ 36 - 4
itdmServer/module-iTDM/src/main/java/org/jeecg/modules/baogao/service/impl/ItdmBaogaoServiceImpl.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.jeecg.common.constant.SymbolConstant;
 import org.jeecg.config.JeecgBaseConfig;
 import org.jeecg.modules.baogao.convert.BaogaoConvert;
 import org.jeecg.modules.baogao.dto.ItdmBaogaoCreateDTO;
@@ -13,6 +14,7 @@ import org.jeecg.modules.baogao.mapper.ItdmBaogaoMapper;
 import org.jeecg.modules.baogao.service.IItdmBaogaoService;
 import org.jeecg.modules.baogao.util.PoiTiUtils;
 import org.jeecg.modules.baogao.vo.ItdmBaogaoListVO;
+import org.jeecg.modules.baogao.vo.ItdmBaogaoVO;
 import org.jeecg.modules.chuanganqi.entity.ItdmChuanganqi;
 import org.jeecg.modules.chuanganqi.mapper.ItdmChuanganqiMapper;
 import org.jeecg.modules.itdmDevice.entity.ItdmDevice;
@@ -157,15 +159,16 @@ public class ItdmBaogaoServiceImpl extends ServiceImpl<ItdmBaogaoMapper, ItdmBao
 
         ItdmMoban itdmMoban = mobanMapper.selectById(dto.getBaogaoType());
 
-        String fileName = jeecgBaseConfig.getPath().getUpload() + File.separator + "word" + File.separator + System.currentTimeMillis() + ".docx";
-        FileUtil.touch(fileName);
+        String fileName =  "word" + File.separator + System.currentTimeMillis() + ".docx";
+        String filePath = jeecgBaseConfig.getPath().getUpload() + File.separator +fileName;
+        FileUtil.touch(filePath);
 
 
         try {
             Map map = getMap(dto, itdmWeituoInfo, yangpins, testItems, getChuanganqiList(dto.getTestItemsIds()), getDeviceList(dto.getTestItemsIds()));
 
             PoiTiUtils.create(jeecgBaseConfig.getPath().getUpload() + File.separator + itdmMoban.getFilePath(), map,
-                    new FileOutputStream(fileName));
+                    new FileOutputStream(filePath));
         } catch (IOException e) {
             throw new RuntimeException(e);
         } catch (ParseException e) {
@@ -173,7 +176,12 @@ public class ItdmBaogaoServiceImpl extends ServiceImpl<ItdmBaogaoMapper, ItdmBao
         }
 
 
-        itdmBaogao.setFilePath(fileName);
+        if (fileName.contains(SymbolConstant.DOUBLE_BACKSLASH)) {
+            itdmBaogao.setFilePath(fileName.replace(SymbolConstant.DOUBLE_BACKSLASH, SymbolConstant.SINGLE_SLASH));
+        }else{
+            itdmBaogao.setFilePath(fileName);
+
+        }
 
         this.baseMapper.insert(itdmBaogao);
     }
@@ -265,4 +273,28 @@ public class ItdmBaogaoServiceImpl extends ServiceImpl<ItdmBaogaoMapper, ItdmBao
         return jeecgBaseConfig.getPath().getUpload() + File.separator + filepath;
     }
 
+
+    @Override
+    public ItdmBaogaoVO wordById(String id) {
+        ItdmBaogao baoVo = this.baseMapper.selectById(id);
+
+        String filepath = baoVo.getFilePath();
+
+        String key = FileUtil.mainName(filepath);
+        String fileType = FileUtil.extName(filepath);
+        String url = filepath;
+        String title = FileUtil.getName(filepath);
+
+
+        return new ItdmBaogaoVO(key, url,fileType, title);
+    }
+
+
+    public static void main(String[] args) {
+        String s = "temap\\user.docx";
+        System.out.println(s.replace(SymbolConstant.DOUBLE_BACKSLASH, SymbolConstant.SINGLE_SLASH));
+
+
+        System.out.println(FileUtil.mainName(s));
+    }
 }

+ 26 - 0
itdmServer/module-iTDM/src/main/java/org/jeecg/modules/baogao/vo/ItdmBaogaoVO.java

@@ -0,0 +1,26 @@
+package org.jeecg.modules.baogao.vo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @Description: 报告生成
+ * @Author: jeecg-boot
+ * @Date: 2023-05-19
+ * @Version: V1.0
+ */
+@Data
+@NoArgsConstructor
+
+@AllArgsConstructor
+public class ItdmBaogaoVO implements Serializable {
+    private  String key;
+    private  String url;
+    private String fileType;
+    private String title;
+}

+ 75 - 0
itdmServer/module-iTDM/src/main/java/org/jeecg/modules/word/WordController.java

@@ -0,0 +1,75 @@
+package org.jeecg.modules.word;
+
+import com.alibaba.fastjson.JSONObject;
+import com.auth0.jwt.JWT;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.jeecg.common.exception.JeecgBootException;
+import org.jeecg.config.JeecgBaseConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URL;
+import java.security.Signer;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Scanner;
+
+@RestController
+@RequestMapping("word")
+public class WordController {
+
+
+    @Autowired
+    private ObjectMapper objectMapper;
+
+    @Autowired
+    private JeecgBaseConfig jeecgBaseConfig;
+
+
+    @PostMapping("/save")
+    @ResponseBody
+    public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
+
+
+        PrintWriter writer = response.getWriter();
+
+        Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
+        String body = scanner.hasNext() ? scanner.next() : "";
+
+        Map<String, Object> map = objectMapper.readValue(body, objectMapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class));
+
+
+        System.out.println(map);
+        if ((Integer) map.get("status") == 2||(Integer) map.get("status") ==6&&(Integer) map.get("status") ==7) {
+            String downloadUri = (String) map.get("url");
+            String fileName = map.get("key") + "." + map.get("filetype");
+
+            URL url = new URL(downloadUri);
+            java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
+            InputStream stream = connection.getInputStream();
+            String pathForSave = jeecgBaseConfig.getPath().getUpload() + File.separator + "word" + File.separator + fileName;
+
+            File savedFile = new File(pathForSave);
+            try (FileOutputStream out = new FileOutputStream(savedFile)) {
+                int read;
+                final byte[] bytes = new byte[1024];
+                while ((read = stream.read(bytes)) != -1) {
+                    out.write(bytes, 0, read);
+                }
+
+                out.flush();
+            }
+
+            connection.disconnect();
+        }
+        writer.write("{\"error\":0}");
+    }
+
+
+
+}
+
+