update 封装通用下载方法简化下载使用
This commit is contained in:
parent
75b22e9a23
commit
99dcbe0207
@ -573,7 +573,7 @@ export default {
|
|||||||
#end
|
#end
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/${moduleName}/${businessName}/export', this.queryParams);
|
this.#[[$download]]#.excel('/${moduleName}/${businessName}/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -17,7 +17,6 @@ import './assets/icons' // icon
|
|||||||
import './permission' // permission control
|
import './permission' // permission control
|
||||||
import { getDicts } from "@/api/system/dict/data";
|
import { getDicts } from "@/api/system/dict/data";
|
||||||
import { getConfigKey } from "@/api/system/config";
|
import { getConfigKey } from "@/api/system/config";
|
||||||
import { downLoadExcel } from "@/utils/download";
|
|
||||||
import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from "@/utils/ruoyi";
|
import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from "@/utils/ruoyi";
|
||||||
// 分页组件
|
// 分页组件
|
||||||
import Pagination from "@/components/Pagination";
|
import Pagination from "@/components/Pagination";
|
||||||
@ -44,7 +43,6 @@ Vue.prototype.resetForm = resetForm
|
|||||||
Vue.prototype.addDateRange = addDateRange
|
Vue.prototype.addDateRange = addDateRange
|
||||||
Vue.prototype.selectDictLabel = selectDictLabel
|
Vue.prototype.selectDictLabel = selectDictLabel
|
||||||
Vue.prototype.selectDictLabels = selectDictLabels
|
Vue.prototype.selectDictLabels = selectDictLabels
|
||||||
Vue.prototype.downLoadExcel = downLoadExcel
|
|
||||||
Vue.prototype.handleTree = handleTree
|
Vue.prototype.handleTree = handleTree
|
||||||
|
|
||||||
// 全局组件挂载
|
// 全局组件挂载
|
||||||
|
@ -5,28 +5,51 @@ import { getToken } from '@/utils/auth'
|
|||||||
const baseURL = process.env.VUE_APP_BASE_API
|
const baseURL = process.env.VUE_APP_BASE_API
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name(name, isDelete = true) {
|
excel(url, params) {
|
||||||
var url = baseURL + "/common/download?fileName=" + encodeURI(name) + "&delete=" + isDelete
|
// get请求映射params参数
|
||||||
|
if (params) {
|
||||||
|
let urlparams = url + '?';
|
||||||
|
for (const propName of Object.keys(params)) {
|
||||||
|
const value = params[propName];
|
||||||
|
var part = encodeURIComponent(propName) + "=";
|
||||||
|
if (value !== null && typeof(value) !== "undefined") {
|
||||||
|
if (typeof value === 'object') {
|
||||||
|
for (const key of Object.keys(value)) {
|
||||||
|
if (value[key] !== null && typeof (value[key]) !== 'undefined') {
|
||||||
|
let params = propName + '[' + key + ']';
|
||||||
|
let subPart = encodeURIComponent(params) + '=';
|
||||||
|
urlparams += subPart + encodeURIComponent(value[key]) + '&';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
urlparams += part + encodeURIComponent(value) + "&";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
urlparams = urlparams.slice(0, -1);
|
||||||
|
url = urlparams;
|
||||||
|
}
|
||||||
|
url = baseURL + url
|
||||||
axios({
|
axios({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: url,
|
url: url,
|
||||||
responseType: 'blob',
|
responseType: 'blob',
|
||||||
headers: { 'Authorization': 'Bearer ' + getToken() }
|
headers: { 'Authorization': 'Bearer ' + getToken() }
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
const blob = new Blob([res.data])
|
const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
|
||||||
this.saveAs(blob, decodeURI(res.headers['download-filename']))
|
this.saveAs(blob, decodeURI(res.headers['download-filename']))
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
resource(resource) {
|
oss(ossId, name) {
|
||||||
var url = baseURL + "/common/download/resource?resource=" + encodeURI(resource);
|
var url = baseURL + '/system/oss/download/' + ossId
|
||||||
axios({
|
axios({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: url,
|
url: url,
|
||||||
responseType: 'blob',
|
responseType: 'blob',
|
||||||
headers: { 'Authorization': 'Bearer ' + getToken() }
|
headers: { 'Authorization': 'Bearer ' + getToken() }
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
const blob = new Blob([res.data])
|
const blob = new Blob([res.data], { type: 'application/octet-stream' })
|
||||||
this.saveAs(blob, decodeURI(res.headers['download-filename']))
|
this.saveAs(blob, name)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
zip(url, name) {
|
zip(url, name) {
|
||||||
|
@ -1,91 +0,0 @@
|
|||||||
import axios from 'axios'
|
|
||||||
import { getToken } from '@/utils/auth'
|
|
||||||
|
|
||||||
const mimeMap = {
|
|
||||||
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
||||||
zip: 'application/zip',
|
|
||||||
oss: 'application/octet-stream'
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function downLoadOss(ossId) {
|
|
||||||
var url = baseUrl + '/system/oss/download/' + ossId
|
|
||||||
axios({
|
|
||||||
method: 'get',
|
|
||||||
url: url,
|
|
||||||
responseType: 'blob',
|
|
||||||
headers: { 'Authorization': 'Bearer ' + getToken() }
|
|
||||||
}).then(res => {
|
|
||||||
resolveBlob(res, mimeMap.oss)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function downLoadExcel(url, params) {
|
|
||||||
// get请求映射params参数
|
|
||||||
if (params) {
|
|
||||||
let urlparams = url + '?';
|
|
||||||
for (const propName of Object.keys(params)) {
|
|
||||||
const value = params[propName];
|
|
||||||
var part = encodeURIComponent(propName) + "=";
|
|
||||||
if (value !== null && typeof(value) !== "undefined") {
|
|
||||||
if (typeof value === 'object') {
|
|
||||||
for (const key of Object.keys(value)) {
|
|
||||||
if (value[key] !== null && typeof (value[key]) !== 'undefined') {
|
|
||||||
let params = propName + '[' + key + ']';
|
|
||||||
let subPart = encodeURIComponent(params) + '=';
|
|
||||||
urlparams += subPart + encodeURIComponent(value[key]) + '&';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
urlparams += part + encodeURIComponent(value) + "&";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
urlparams = urlparams.slice(0, -1);
|
|
||||||
url = urlparams;
|
|
||||||
}
|
|
||||||
url = baseUrl + url
|
|
||||||
axios({
|
|
||||||
method: 'get',
|
|
||||||
url: url,
|
|
||||||
responseType: 'blob',
|
|
||||||
headers: { 'Authorization': 'Bearer ' + getToken() }
|
|
||||||
}).then(res => {
|
|
||||||
resolveBlob(res, mimeMap.xlsx)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析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.style.display = 'none'
|
|
||||||
aLink.href = URL.createObjectURL(blob)
|
|
||||||
aLink.setAttribute('download', decodeURI(fileName)) // 设置下载文件名称
|
|
||||||
document.body.appendChild(aLink)
|
|
||||||
aLink.click()
|
|
||||||
URL.revokeObjectURL(aLink.href);//清除引用
|
|
||||||
document.body.removeChild(aLink);
|
|
||||||
}
|
|
@ -358,7 +358,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/demo/demo/export', this.queryParams);
|
this.$download.excel('/demo/demo/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -510,7 +510,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/monitor/job/export', this.queryParams);
|
this.$download.excel('/monitor/job/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -293,7 +293,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/monitor/jobLog/export', this.queryParams);
|
this.$download.excel('/monitor/jobLog/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -216,7 +216,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/monitor/logininfor/export', this.queryParams);
|
this.$download.excel('/monitor/logininfor/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -303,7 +303,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/monitor/operlog/export', this.queryParams);
|
this.$download.excel('/monitor/operlog/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -334,7 +334,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/system/config/export', this.queryParams);
|
this.$download.excel('/system/config/export', this.queryParams);
|
||||||
},
|
},
|
||||||
/** 刷新缓存按钮操作 */
|
/** 刷新缓存按钮操作 */
|
||||||
handleRefreshCache() {
|
handleRefreshCache() {
|
||||||
|
@ -380,7 +380,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/system/dict/data/export', this.queryParams);
|
this.$download.excel('/system/dict/data/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -338,7 +338,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/system/dict/type/export', this.queryParams);
|
this.$download.excel('/system/dict/type/export', this.queryParams);
|
||||||
},
|
},
|
||||||
/** 刷新缓存按钮操作 */
|
/** 刷新缓存按钮操作 */
|
||||||
handleRefreshCache() {
|
handleRefreshCache() {
|
||||||
|
@ -188,7 +188,6 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { listOss, delOss, changePreviewListResource } from "@/api/system/oss";
|
import { listOss, delOss, changePreviewListResource } from "@/api/system/oss";
|
||||||
import { downLoadOss } from "@/utils/download";
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Oss",
|
name: "Oss",
|
||||||
@ -325,7 +324,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 下载按钮操作 */
|
/** 下载按钮操作 */
|
||||||
handleDownload(row) {
|
handleDownload(row) {
|
||||||
downLoadOss(row.ossId)
|
this.$download.oss(row.ossId)
|
||||||
},
|
},
|
||||||
/** 删除按钮操作 */
|
/** 删除按钮操作 */
|
||||||
handleDelete(row) {
|
handleDelete(row) {
|
||||||
|
@ -305,7 +305,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/system/post/export', this.queryParams);
|
this.$download.excel('/system/post/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -613,7 +613,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/system/role/export', this.queryParams);
|
this.$download.excel('/system/role/export', this.queryParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -643,7 +643,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.downLoadExcel('/system/user/export', this.queryParams);
|
this.$download.excel('/system/user/export', this.queryParams);
|
||||||
},
|
},
|
||||||
/** 导入按钮操作 */
|
/** 导入按钮操作 */
|
||||||
handleImport() {
|
handleImport() {
|
||||||
@ -652,7 +652,7 @@ export default {
|
|||||||
},
|
},
|
||||||
/** 下载模板操作 */
|
/** 下载模板操作 */
|
||||||
importTemplate() {
|
importTemplate() {
|
||||||
this.downLoadExcel('/system/user/importTemplate');
|
this.$download.excel('/system/user/importTemplate');
|
||||||
},
|
},
|
||||||
// 文件上传中处理
|
// 文件上传中处理
|
||||||
handleFileUploadProgress(event, file, fileList) {
|
handleFileUploadProgress(event, file, fileList) {
|
||||||
|
@ -180,7 +180,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
|
import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
|
||||||
import importTable from "./importTable";
|
import importTable from "./importTable";
|
||||||
import { downLoadZip } from "@/utils/download";
|
|
||||||
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";
|
||||||
hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
|
hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user