Commit 656a4e8d by xinla

公用导出功能封装,下载优化等

1 parent 67fc0149
...@@ -117,28 +117,6 @@ export default { ...@@ -117,28 +117,6 @@ export default {
this.loading = false this.loading = false
}) })
}, },
exprotTable() {
this.$confirm('是否确认导出吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
this.loading = true
let query = Object.assign({}, this.query, { pageNum: undefined, pageSize: undefined })
return this.requestExport(query)
})
.then((resBlob) => {
this.downloadBlob(resBlob, this.exportFileName)
})
.catch((error) => {
console.error(error)
})
.finally(() => {
this.loading = false
})
},
}, },
} }
</script> </script>
...@@ -150,7 +128,11 @@ export default { ...@@ -150,7 +128,11 @@ export default {
<div class="RequestChartTable-operation" v-if="requestExport || $slots.query"> <div class="RequestChartTable-operation" v-if="requestExport || $slots.query">
<slot name="operation" v-bind="query"></slot> <slot name="operation" v-bind="query"></slot>
<el-button v-if="requestExport" style="position: absolute; right: 0" type="primary" @click="exprotTable"> <el-button
v-if="requestExport"
style="position: absolute; right: 0"
type="primary"
@click="$exportData(requestExport.bind(null, query), exportFileName)">
导出 Excel 导出 Excel
</el-button> </el-button>
</div> </div>
......
...@@ -100,8 +100,8 @@ if (h) { ...@@ -100,8 +100,8 @@ if (h) {
// if (process.env.VUE_APP_ENV === 'production') document.documentElement.style.filter = 'grayscale(100%)' // if (process.env.VUE_APP_ENV === 'production') document.documentElement.style.filter = 'grayscale(100%)'
// el-button点击后自动失焦 去除焦点样式 // el-button点击后自动失焦 去除焦点样式
document.addEventListener('click', (target) => { document.addEventListener('click', (event) => {
target = target.target let target = event.target
if (target.className.includes('el-button')) { if (target.className.includes('el-button')) {
target.blur() target.blur()
} else if (target.parentNode.className.includes('el-button')) { } else if (target.parentNode.className.includes('el-button')) {
......
...@@ -99,11 +99,12 @@ export function selectDictLabels(datas, value, separator) { ...@@ -99,11 +99,12 @@ export function selectDictLabels(datas, value, separator) {
/** /**
* 下载bolb文件 * 下载bolb文件
* @param {*} blob bolo源数据 必需 * @param {*} blob bolo源数据 必需
* @param {*} type 文件类型 必需 * @param {*} downloadName 下载文件名,需含文件后缀名 必需
* @param {*} downloadName 下载文件名,需含文件后缀名 可选 * @param {*} type 文件类型 enum: excel,zip,image
* @param {*} callback 成功回调
*/ */
export function downloadBlob(blob, downloadName, type) { export function downloadBlob(blob, downloadName, type, callback) {
if (!blob || !downloadName) return if (!blob || !downloadName) throw '文件或文件名不存在'
if (blob instanceof Blob) { if (blob instanceof Blob) {
const typeDict = { const typeDict = {
excel: 'application/vnd.ms-excel', excel: 'application/vnd.ms-excel',
...@@ -118,6 +119,7 @@ export function downloadBlob(blob, downloadName, type) { ...@@ -118,6 +119,7 @@ export function downloadBlob(blob, downloadName, type) {
a.click() a.click()
a.remove() a.remove()
URL.revokeObjectURL(url) // 释放内存 URL.revokeObjectURL(url) // 释放内存
callback && callback()
} else if (/^http/.test(blob) && type === 'image') { } else if (/^http/.test(blob) && type === 'image') {
let image = new Image() let image = new Image()
image.setAttribute('crossOrigin', 'anonymous') image.setAttribute('crossOrigin', 'anonymous')
...@@ -132,6 +134,8 @@ export function downloadBlob(blob, downloadName, type) { ...@@ -132,6 +134,8 @@ export function downloadBlob(blob, downloadName, type) {
downloadBlob(blob, downloadName) downloadBlob(blob, downloadName)
}) })
} }
} else {
throw 'blob:文件类型错误'
} }
} }
...@@ -146,7 +150,7 @@ export function download(fileName) { ...@@ -146,7 +150,7 @@ export function download(fileName) {
}, },
}).then((res) => { }).then((res) => {
const { data, headers } = res const { data, headers } = res
downloadBlob(data, headers['content-type'], fileName) downloadBlob(data, fileName, headers['content-type'])
// const blob = new Blob([data], { type: headers['content-type'] }) // const blob = new Blob([data], { type: headers['content-type'] })
// let dom = document.createElement('a') // let dom = document.createElement('a')
...@@ -172,7 +176,7 @@ export function downloadNet(url, fileName) { ...@@ -172,7 +176,7 @@ export function downloadNet(url, fileName) {
}, },
}).then((res) => { }).then((res) => {
const { data, headers } = res const { data, headers } = res
downloadBlob(data, headers['content-type'], fileName) downloadBlob(data, fileName, headers['content-type'])
// const blob = new Blob([data], { type: headers['content-type'] }) // const blob = new Blob([data], { type: headers['content-type'] })
// let dom = document.createElement('a') // let dom = document.createElement('a')
...@@ -346,7 +350,11 @@ export function filType(file) { ...@@ -346,7 +350,11 @@ export function filType(file) {
} }
} }
// 通用删除确认弹窗 /**
* 通用删除
* @param {*} remove 删除接口
* @param {*} callback 成功回调
*/
export function $delConfirm(remove, callback) { export function $delConfirm(remove, callback) {
this.$confirm('是否确认删除?', '警告', { this.$confirm('是否确认删除?', '警告', {
confirmButtonText: '确定', confirmButtonText: '确定',
...@@ -361,9 +369,12 @@ export function $delConfirm(remove, callback) { ...@@ -361,9 +369,12 @@ export function $delConfirm(remove, callback) {
// this.getList && this.getList() // this.getList && this.getList()
this.msgSuccess() this.msgSuccess()
}) })
.catch(function () {}) .catch((error) => {
console.error(error)
})
} }
// 复制文字
export function $copyText(txt) { export function $copyText(txt) {
let clipboard = new this.ClipboardJS(event.currentTarget.localName, { let clipboard = new this.ClipboardJS(event.currentTarget.localName, {
target: function (trigger) { target: function (trigger) {
...@@ -379,5 +390,44 @@ export function $copyText(txt) { ...@@ -379,5 +390,44 @@ export function $copyText(txt) {
} }
/** /**
* 通用导出
* @param {*} requestExport 导出接口 必需
* @param {*} exportFileName 导出文件名 需含文件后缀名 必需
* @param {*} type 文件类型 enum: excel,zip,image 必需
* @param {*} callback 成功回调
*/
export function $exportData(requestExport, exportFileName = '导出.xlsx', callback, type) {
this.$confirm('是否确认导出吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
// debugger
return new Promise((resolve, reject) => {
this.$alert('正在导出,请稍等...', '提示', {
confirmButtonText: '取消导出',
confirmButtonClass: 'exportData',
showClose: false,
}).then(() => {
reject()
})
requestExport().then((resBlob) => {
resolve(resBlob)
})
})
})
.then((resBlob) => {
downloadBlob(resBlob, exportFileName, type, () => {
document.querySelector('.el-message-box__btns .exportData').click()
this.msgSuccess('操作成功')
})
})
.catch((error) => {
console.error(error)
})
}
/**
* 通用js方法封装处理,挂载到 Vue.prototype * 通用js方法封装处理,挂载到 Vue.prototype
*/ */
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!