Commit 54c98c3a44a5fe17a4fcc57c0501f774995c6be1

Authored by YunaiV
1 parent 6ca92b0e

代码生成器的下载功能,完成~

ruoyi-ui/src/api/tool/codegen.js
@@ -30,7 +30,7 @@ export function updateCodegen(data) { @@ -30,7 +30,7 @@ export function updateCodegen(data) {
30 export function previewCodegen(tableId) { 30 export function previewCodegen(tableId) {
31 return request({ 31 return request({
32 url: '/tool/codegen/preview?tableId=' + tableId, 32 url: '/tool/codegen/preview?tableId=' + tableId,
33 - method: 'get' 33 + method: 'get',
34 }) 34 })
35 } 35 }
36 36
ruoyi-ui/src/utils/ruoyi.js
@@ -103,23 +103,17 @@ export function download(fileName) { @@ -103,23 +103,17 @@ export function download(fileName) {
103 103
104 // 下载 Excel 方法 104 // 下载 Excel 方法
105 export function downloadExcel(data, fileName) { 105 export function downloadExcel(data, fileName) {
106 - // 创建 blob  
107 - let blob = new Blob([data], {type: 'application/vnd.ms-excel'});  
108 - // 创建 href 超链接,点击进行下载  
109 - window.URL = window.URL || window.webkitURL;  
110 - let href = URL.createObjectURL(blob);  
111 - let downA = document.createElement("a");  
112 - downA.href = href;  
113 - downA.download = fileName;  
114 - downA.click();  
115 - // 销毁超连接  
116 - window.URL.revokeObjectURL(href); 106 + download0(data, fileName, 'application/vnd.ms-excel');
117 } 107 }
118 108
119 // 下载 Zip 方法 109 // 下载 Zip 方法
120 export function downloadZip(data, fileName) { 110 export function downloadZip(data, fileName) {
  111 + download0(data, fileName, 'application/zip');
  112 +}
  113 +
  114 +function download0(data, fileName, mineType) {
121 // 创建 blob 115 // 创建 blob
122 - let blob = new Blob([data], {type: 'application/zip'}); 116 + let blob = new Blob([data], {type: mineType});
123 // 创建 href 超链接,点击进行下载 117 // 创建 href 超链接,点击进行下载
124 window.URL = window.URL || window.webkitURL; 118 window.URL = window.URL || window.webkitURL;
125 let href = URL.createObjectURL(blob); 119 let href = URL.createObjectURL(blob);
ruoyi-ui/src/utils/zipdownload.js deleted
1 -import axios from 'axios'  
2 -import { getToken } from '@/utils/auth'  
3 -  
4 -const mimeMap = {  
5 - xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',  
6 - zip: 'application/zip'  
7 -}  
8 -  
9 -const baseUrl = process.env.VUE_APP_BASE_API  
10 -export function downLoadZip(str, filename) {  
11 - var url = baseUrl + str  
12 - axios({  
13 - method: 'get',  
14 - url: url,  
15 - responseType: 'blob',  
16 - headers: { 'Authorization': 'Bearer ' + getToken() }  
17 - }).then(res => {  
18 - resolveBlob(res, mimeMap.zip)  
19 - })  
20 -}  
21 -/**  
22 - * 解析blob响应内容并下载  
23 - * @param {*} res blob响应内容  
24 - * @param {String} mimeType MIME类型  
25 - */  
26 -export function resolveBlob(res, mimeType) {  
27 - const aLink = document.createElement('a')  
28 - var blob = new Blob([res.data], { type: mimeType })  
29 - // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;  
30 - var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')  
31 - var contentDisposition = decodeURI(res.headers['content-disposition'])  
32 - var result = patt.exec(contentDisposition)  
33 - var fileName = result[1]  
34 - fileName = fileName.replace(/\"/g, '')  
35 - aLink.href = URL.createObjectURL(blob)  
36 - aLink.setAttribute('download', fileName) // 设置下载文件名称  
37 - document.body.appendChild(aLink)  
38 - aLink.click()  
39 - document.body.appendChild(aLink)  
40 -}  
ruoyi-ui/src/views/tool/gen/index.vue
@@ -85,15 +85,15 @@ @@ -85,15 +85,15 @@
85 </template> 85 </template>
86 86
87 <script> 87 <script>
88 -import { previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";  
89 -import { getCodeGenTablePage, previewCodegen, } from "@/api/tool/codegen"; 88 +import { delTable, synchDb } from "@/api/tool/gen";
  89 +import { getCodeGenTablePage, previewCodegen, downloadCodegen } from "@/api/tool/codegen";
90 90
91 import importTable from "./importTable"; 91 import importTable from "./importTable";
92 -import { downLoadZip } from "@/utils/zipdownload";  
93 // 代码高亮插件 92 // 代码高亮插件
94 import hljs from "highlight.js/lib/highlight"; 93 import hljs from "highlight.js/lib/highlight";
95 import "highlight.js/styles/github-gist.css"; 94 import "highlight.js/styles/github-gist.css";
96 import {list} from "@/api/system/loginlog"; 95 import {list} from "@/api/system/loginlog";
  96 +import {exportOperateLog} from "@/api/system/operatelog";
97 hljs.registerLanguage("java", require("highlight.js/lib/languages/java")); 97 hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
98 hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml")); 98 hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));
99 hljs.registerLanguage("html", require("highlight.js/lib/languages/xml")); 99 hljs.registerLanguage("html", require("highlight.js/lib/languages/xml"));
@@ -167,20 +167,9 @@ export default { @@ -167,20 +167,9 @@ export default {
167 }, 167 },
168 /** 生成代码操作 */ 168 /** 生成代码操作 */
169 handleGenTable(row) { 169 handleGenTable(row) {
170 - // const tableNames = row.tableName || this.tableNames;  
171 - // if (tableNames === "") {  
172 - // this.msgError("请选择要生成的数据");  
173 - // return;  
174 - // }  
175 - // if(row.genType === "1") {  
176 - // genCode(row.tableName).then(response => {  
177 - // this.msgSuccess("成功生成到自定义路径:" + row.genPath);  
178 - // });  
179 - // } else {  
180 - // downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");  
181 - // }  
182 -  
183 - 170 + downloadCodegen(row.id).then(response => {
  171 + this.downloadZip(response, 'codegen-' + row.tableName + '.zip');
  172 + })
184 }, 173 },
185 /** 同步数据库操作 */ 174 /** 同步数据库操作 */
186 handleSynchDb(row) { 175 handleSynchDb(row) {