使用时做了全屏锁定防止影响下载
<el-button type="primary" size="mini" @click="handleExport"
v-loading.fullscreen.lock="fullscreenLoading">导出
</el-button>
调用公共方法,按需要的文件格式跟文件名
/** 导出按钮操作 */
handleExport() {
//导出按钮上同步改
this.fullscreenLoading = true;
exportExcel.Excel('/ms/factory/export', this.queryParams, `厂商管理列表.xlsx`).then(() => {
this.fullscreenLoading = false
})
}
import axios from 'axios'
import { Message } from 'element-ui'
import { saveAs } from 'file-saver'
import { getToken } from '@/util/auth'
import errorCode from '@/util/errorCode'
import store from '../store/index.js'
import { blobValidate } from "@/util/ruoyi";
const prefix = store.state.prefix
const preUrl = store.state.preUrl
export default {
Excel(url,params,name) {
var loading = true;
var url = prefix + url
return axios({
method: 'post',
url: url,
data:params,
responseType: 'blob',
headers: {
// 'Authorization': 'Bearer ' + getToken()
'Authorization': sessionStorage.getItem('master-token')
}
}).then(async (res) => {
// console.info(res)
const isLogin = await blobValidate(res.data);
if (isLogin) {
const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
this.saveAs(blob, name)
} else {
this.printErrMsg(res.data);
}
})
},
Word(url,params,name) {
var loading = false;
var url = prefix + url
axios({
method: 'post',
url: url,
data:params,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(async (res) => {
const isLogin = await blobValidate(res.data);
if (isLogin) {
const blob = new Blob([res.data], { type: 'application/vnd.ms-word' })
this.saveAs(blob, name)
} else {
this.printErrMsg(res.data);
}
})
return loading;
},
DownLoad(uuid, name) {
var url = preUrl + uuid
const type = name.split('.')[1]
// console.log(url, type)
axios({
method: 'post',
url: url,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(async (res) => {
const isLogin = await blobValidate(res.data);
if (isLogin) {
if(type == 'xls' || type == 'XLS') {
const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
this.saveAs(blob, name)
}else if(type == 'doc' || type == 'docx' || type == 'DOC' || type == 'DOCX') {
const blob = new Blob([res.data], { type: 'application/vnd.ms-word' })
this.saveAs(blob, name)
}else if(type == 'pdf' || type == 'PDF') {
const blob = new Blob([res.data], { type: 'application/pdf' })
this.saveAs(blob, name)
}else if(type == 'txt' || type == 'TXT') {
const blob = new Blob([res.data], { type: 'text/plain' })
this.saveAs(blob, name)
}else if(type == 'png' || type == 'PNG') {
const blob = new Blob([res.data], { type: 'image/png' })
this.saveAs(blob, name)
}else if(type == 'jpg' || type == 'jpeg' || type == 'JPG' || type == 'JPEG') {
const blob = new Blob([res.data], { type: 'image/jpeg' })
this.saveAs(blob, name)
}
} else {
this.printErrMsg(res.data);
}
})
},
saveAs(text, name, opts) {
saveAs(text, name, opts);
},
async printErrMsg(data) {
const resText = await data.text();
const rspObj = JSON.parse(resText);
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
this.$message.error(errMsg);
}
}