Commit 2fcb54b57661a935cbe650610e31929e4af9dbfd

Authored by YunaiV
1 parent 84ca9222

增加数据库文档的 html、word、markdown 的导出

ruoyi-ui/src/api/infra/dbDoc.js
... ... @@ -8,3 +8,19 @@ export function exportHtml() {
8 8 responseType: 'blob'
9 9 })
10 10 }
  11 +
  12 +export function exportWord() {
  13 + return request({
  14 + url: '/infra/db-doc/export-word',
  15 + method: 'get',
  16 + responseType: 'blob'
  17 + })
  18 +}
  19 +
  20 +export function exportMarkdown() {
  21 + return request({
  22 + url: '/infra/db-doc/export-markdown',
  23 + method: 'get',
  24 + responseType: 'blob'
  25 + })
  26 +}
... ...
ruoyi-ui/src/main.js
... ... @@ -25,7 +25,10 @@ import {
25 25 download,
26 26 handleTree,
27 27 downloadExcel,
28   - downloadZip
  28 + downloadWord,
  29 + downloadZip,
  30 + downloadHtml,
  31 + downloadMarkdown,
29 32 } from "@/utils/ruoyi";
30 33 import Pagination from "@/components/Pagination";
31 34 // 自定义表格工具扩展
... ... @@ -48,6 +51,9 @@ Vue.prototype.getDictDataLabel = getDictDataLabel
48 51 Vue.prototype.DICT_TYPE = DICT_TYPE
49 52 Vue.prototype.download = download
50 53 Vue.prototype.downloadExcel = downloadExcel
  54 +Vue.prototype.downloadWord = downloadWord
  55 +Vue.prototype.downloadHtml = downloadHtml
  56 +Vue.prototype.downloadMarkdown = downloadMarkdown
51 57 Vue.prototype.downloadZip = downloadZip
52 58 Vue.prototype.handleTree = handleTree
53 59  
... ...
ruoyi-ui/src/utils/ruoyi.js
... ... @@ -120,11 +120,26 @@ export function downloadExcel(data, fileName) {
120 120 download0(data, fileName, 'application/vnd.ms-excel');
121 121 }
122 122  
  123 +// 下载 Word 方法
  124 +export function downloadWord(data, fileName) {
  125 + download0(data, fileName, 'application/msword');
  126 +}
  127 +
123 128 // 下载 Zip 方法
124 129 export function downloadZip(data, fileName) {
125 130 download0(data, fileName, 'application/zip');
126 131 }
127 132  
  133 +// 下载 Html 方法
  134 +export function downloadHtml(data, fileName) {
  135 + download0(data, fileName, 'text/html');
  136 +}
  137 +
  138 +// 下载 Markdown 方法
  139 +export function downloadMarkdown(data, fileName) {
  140 + download0(data, fileName, 'text/markdown');
  141 +}
  142 +
128 143 function download0(data, fileName, mineType) {
129 144 // 创建 blob
130 145 let blob = new Blob([data], {type: mineType});
... ...
ruoyi-ui/src/views/tool/dbDoc/index.vue
1 1 <template>
2   - <div v-loading="loading" :style="'height:'+ height">
3   - <iframe :src="src" frameborder="no" style="width: 100%;height: 100%" scrolling="auto" />
  2 + <div class="app-container">
  3 + <el-row :gutter="10" class="mb8">
  4 + <el-col :span="1.5">
  5 + <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleExportHtml">导出 HTML</el-button>
  6 + <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleExportWord">导出 Word</el-button>
  7 + <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleExportMarkdown">导出 Markdown</el-button>
  8 + </el-col>
  9 + </el-row>
  10 +
  11 + <!-- 展示文档 -->
  12 + <div v-loading="loading" :style="'height:'+ height">
  13 + <iframe :src="src" frameborder="no" style="width: 100%;height: 100%" scrolling="auto" />
  14 + </div>
4 15 </div>
5 16 </template>
6 17 <script>
7   -import {exportHtml} from "@/api/infra/dbDoc";
  18 +import { exportHtml, exportWord, exportMarkdown} from "@/api/infra/dbDoc";
8 19  
9 20 export default {
10 21 name: "DBDoc",
... ... @@ -25,10 +36,31 @@ export default {
25 36 };
26 37 },
27 38 created() {
  39 + // 加载 Html,进行预览
28 40 exportHtml().then(response => {
29   - // var blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'], {type : 'text/html'});
30   - this.src = window.URL.createObjectURL(response);
  41 + let blob = new Blob([response], {type : 'text/html'});
  42 + this.src = window.URL.createObjectURL(blob);
31 43 })
32 44 },
  45 + methods: {
  46 + /** 处理导出 HTML */
  47 + handleExportHtml() {
  48 + exportHtml().then(response => {
  49 + this.downloadHtml(response, '数据库文档.html');
  50 + })
  51 + },
  52 + /** 处理导出 Word */
  53 + handleExportWord() {
  54 + exportWord().then(response => {
  55 + this.downloadWord(response, '数据库文档.doc');
  56 + })
  57 + },
  58 + /** 处理导出 Markdown */
  59 + handleExportMarkdown() {
  60 + exportMarkdown().then(response => {
  61 + this.downloadMarkdown(response, '数据库文档.md');
  62 + })
  63 + }
  64 + }
33 65 };
34 66 </script>
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java
1 1 package cn.iocoder.dashboard.modules.infra.controller.doc;
2 2  
3 3 import cn.hutool.core.io.FileUtil;
4   -import cn.hutool.core.io.IoUtil;
5   -import cn.hutool.core.lang.UUID;
6 4 import cn.hutool.core.util.IdUtil;
7   -import cn.hutool.extra.servlet.ServletUtil;
8 5 import cn.iocoder.dashboard.util.servlet.ServletUtils;
9 6 import cn.smallbun.screw.core.Configuration;
10 7 import cn.smallbun.screw.core.engine.EngineConfig;
... ... @@ -16,11 +13,8 @@ import com.zaxxer.hikari.HikariConfig;
16 13 import com.zaxxer.hikari.HikariDataSource;
17 14 import io.swagger.annotations.Api;
18 15 import io.swagger.annotations.ApiImplicitParam;
19   -import io.swagger.annotations.ApiImplicitParams;
20 16 import io.swagger.annotations.ApiOperation;
21 17 import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
22   -import org.springframework.http.MediaType;
23   -import org.springframework.util.StreamUtils;
24 18 import org.springframework.web.bind.annotation.GetMapping;
25 19 import org.springframework.web.bind.annotation.RequestMapping;
26 20 import org.springframework.web.bind.annotation.RequestParam;
... ... @@ -28,7 +22,8 @@ import org.springframework.web.bind.annotation.RestController;
28 22  
29 23 import javax.annotation.Resource;
30 24 import javax.servlet.http.HttpServletResponse;
31   -import java.io.*;
  25 +import java.io.File;
  26 +import java.io.IOException;
32 27 import java.util.Collections;
33 28  
34 29 @Api(tags = "数据库文档")
... ... @@ -45,9 +40,8 @@ public class InfDbDocController {
45 40 private static final String DOC_VERSION = "1.0.0";
46 41 private static final String DOC_DESCRIPTION = "文档描述";
47 42  
48   -
49 43 @GetMapping("/export-html")
50   - @ApiOperation("导出html格式的数据文档")
  44 + @ApiOperation("导出 html 格式的数据文档")
51 45 @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class)
52 46 public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile,
53 47 HttpServletResponse response) throws IOException {
... ... @@ -55,7 +49,7 @@ public class InfDbDocController {
55 49 }
56 50  
57 51 @GetMapping("/export-word")
58   - @ApiOperation("导出word格式的数据文档")
  52 + @ApiOperation("导出 word 格式的数据文档")
59 53 @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class)
60 54 public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile,
61 55 HttpServletResponse response) throws IOException {
... ... @@ -63,7 +57,7 @@ public class InfDbDocController {
63 57 }
64 58  
65 59 @GetMapping("/export-markdown")
66   - @ApiOperation("导出markdown格式的数据文档")
  60 + @ApiOperation("导出 markdown 格式的数据文档")
67 61 @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class)
68 62 public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile,
69 63 HttpServletResponse response) throws IOException {
... ... @@ -77,9 +71,8 @@ public class InfDbDocController {
77 71 String downloadFileName = DOC_FILE_NAME + fileOutputType.getFileSuffix(); //下载后的文件名
78 72 try {
79 73 // 读取,返回
80   - //这里不用hutool工具类,它的中文文件名编码有问题,导致在浏览器下载时有问题
81 74 ServletUtils.writeAttachment(response, downloadFileName, FileUtil.readBytes(filePath));
82   - }finally {
  75 + } finally {
83 76 handleDeleteFile(deleteFile, filePath);
84 77 }
85 78 }
... ... @@ -123,7 +116,6 @@ public class InfDbDocController {
123 116 private HikariDataSource buildDataSource() {
124 117 // 创建 HikariConfig 配置类
125 118 HikariConfig hikariConfig = new HikariConfig();
126   -// hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
127 119 hikariConfig.setJdbcUrl(dataSourceProperties.getUrl());
128 120 hikariConfig.setUsername(dataSourceProperties.getUsername());
129 121 hikariConfig.setPassword(dataSourceProperties.getPassword());
... ...