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

This commit is contained in:
YunaiV 2021-02-10 19:05:22 +08:00
parent 6ca92b0efc
commit 54c98c3a44
4 changed files with 13 additions and 70 deletions

View File

@ -30,7 +30,7 @@ export function updateCodegen(data) {
export function previewCodegen(tableId) { export function previewCodegen(tableId) {
return request({ return request({
url: '/tool/codegen/preview?tableId=' + tableId, url: '/tool/codegen/preview?tableId=' + tableId,
method: 'get' method: 'get',
}) })
} }

View File

@ -103,23 +103,17 @@ export function download(fileName) {
// 下载 Excel 方法 // 下载 Excel 方法
export function downloadExcel(data, fileName) { export function downloadExcel(data, fileName) {
// 创建 blob download0(data, fileName, 'application/vnd.ms-excel');
let blob = new Blob([data], {type: 'application/vnd.ms-excel'});
// 创建 href 超链接,点击进行下载
window.URL = window.URL || window.webkitURL;
let href = URL.createObjectURL(blob);
let downA = document.createElement("a");
downA.href = href;
downA.download = fileName;
downA.click();
// 销毁超连接
window.URL.revokeObjectURL(href);
} }
// 下载 Zip 方法 // 下载 Zip 方法
export function downloadZip(data, fileName) { export function downloadZip(data, fileName) {
download0(data, fileName, 'application/zip');
}
function download0(data, fileName, mineType) {
// 创建 blob // 创建 blob
let blob = new Blob([data], {type: 'application/zip'}); let blob = new Blob([data], {type: mineType});
// 创建 href 超链接,点击进行下载 // 创建 href 超链接,点击进行下载
window.URL = window.URL || window.webkitURL; window.URL = window.URL || window.webkitURL;
let href = URL.createObjectURL(blob); let href = URL.createObjectURL(blob);

View File

@ -1,40 +0,0 @@
import axios from 'axios'
import { getToken } from '@/utils/auth'
const mimeMap = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
zip: 'application/zip'
}
const baseUrl = process.env.VUE_APP_BASE_API
export function downLoadZip(str, filename) {
var url = baseUrl + str
axios({
method: 'get',
url: url,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(res => {
resolveBlob(res, mimeMap.zip)
})
}
/**
* 解析blob响应内容并下载
* @param {*} res blob响应内容
* @param {String} mimeType MIME类型
*/
export function resolveBlob(res, mimeType) {
const aLink = document.createElement('a')
var blob = new Blob([res.data], { type: mimeType })
// //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
var contentDisposition = decodeURI(res.headers['content-disposition'])
var result = patt.exec(contentDisposition)
var fileName = result[1]
fileName = fileName.replace(/\"/g, '')
aLink.href = URL.createObjectURL(blob)
aLink.setAttribute('download', fileName) // 设置下载文件名称
document.body.appendChild(aLink)
aLink.click()
document.body.appendChild(aLink)
}

View File

@ -85,15 +85,15 @@
</template> </template>
<script> <script>
import { previewTable, delTable, genCode, synchDb } from "@/api/tool/gen"; import { delTable, synchDb } from "@/api/tool/gen";
import { getCodeGenTablePage, previewCodegen, } from "@/api/tool/codegen"; import { getCodeGenTablePage, previewCodegen, downloadCodegen } from "@/api/tool/codegen";
import importTable from "./importTable"; import importTable from "./importTable";
import { downLoadZip } from "@/utils/zipdownload";
// //
import hljs from "highlight.js/lib/highlight"; import hljs from "highlight.js/lib/highlight";
import "highlight.js/styles/github-gist.css"; import "highlight.js/styles/github-gist.css";
import {list} from "@/api/system/loginlog"; import {list} from "@/api/system/loginlog";
import {exportOperateLog} from "@/api/system/operatelog";
hljs.registerLanguage("java", require("highlight.js/lib/languages/java")); hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml")); hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));
hljs.registerLanguage("html", require("highlight.js/lib/languages/xml")); hljs.registerLanguage("html", require("highlight.js/lib/languages/xml"));
@ -167,20 +167,9 @@ export default {
}, },
/** 生成代码操作 */ /** 生成代码操作 */
handleGenTable(row) { handleGenTable(row) {
// const tableNames = row.tableName || this.tableNames; downloadCodegen(row.id).then(response => {
// if (tableNames === "") { this.downloadZip(response, 'codegen-' + row.tableName + '.zip');
// this.msgError(""); })
// return;
// }
// if(row.genType === "1") {
// genCode(row.tableName).then(response => {
// this.msgSuccess("" + row.genPath);
// });
// } else {
// downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");
// }
}, },
/** 同步数据库操作 */ /** 同步数据库操作 */
handleSynchDb(row) { handleSynchDb(row) {