刘翔宇-旅管家 3 years ago
parent 3cea880acf
commit ecf09d5541

@ -0,0 +1,92 @@
import axios from 'axios'
import { getToken } from '@/utils/auth'
import { Loading, Message } from 'element-ui'
/*
* @params {string} url 请求拼接地址
* @params {object} info 请求参数params或data
*/
let loading;
export async function download(url,method='get',info,filename){
loading = Loading.service({
lock:true,
background:"rgba(0,0,0,0.4)",
text:"文件正在生成中..."
})
let options = {
baseURL:process.env.VUE_APP_DOMIAN,
url,
method,
responseType: 'blob',
timeout:10000,
headers: {
Accept: 'application/json','Content-Type':'application/json; charset=utf-8',
withCredentials: true,
Authorization:"Bearer " + getToken()
},
}
if(method === 'get'){
Object.defineProperty(options,'params',{
value:info,
enumerable:true,
writable:false
})
}
if(method === 'post'){
Object.defineProperty(options,'data',{
value:info,
enumerable:true,
writable:false
})
}
try {
const response = await axios.request(options)
loading.close()
// 提取文件名
if(!filename){
filename = response.headers['content-disposition']?.match(
/filename=(.*)/
)[1] || ''
}
// 将二进制流转为blob
const blob = new Blob([response.data], { type: 'application/octet-stream' })
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// 兼容IEwindow.navigator.msSaveBlob以本地方式保存文件
window.navigator.msSaveBlob(blob, decodeURI(filename))
} else {
// 创建新的URL并指向File对象或者Blob对象的地址
const blobURL = window.URL.createObjectURL(blob)
// 创建a标签用于跳转至下载链接
const tempLink = document.createElement('a')
tempLink.style.display = 'none'
tempLink.href = blobURL
tempLink.setAttribute('download', decodeURI(filename))
// 兼容某些浏览器不支持HTML5的download属性
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank')
}
// 挂载a标签
document.body.appendChild(tempLink)
tempLink.click()
document.body.removeChild(tempLink)
// 释放blob URL地址
window.URL.revokeObjectURL(blobURL)
}
}catch (err){
console.error(err)
loading.close()
Message({
type:'error',
message:err
})
}
}

@ -146,7 +146,7 @@
@click=" select = {showDatePicker:'',ageIndex:1,startDate:'',endDate:'',type:'',department:'',purchaseModality:'',purchaseMethods:'',priceMin:null,priceMax:null,status:''}">
重置
</Button>
<Button type="primary" @click="toExport()" style="margin-left: 10px">导出</Button>
<Button type="primary" @click="downloadExel()" style="margin-left: 10px">导出</Button>
<!-- <Button type="primary" style="margin-left: 10px">导出</Button>-->
</div>
</slot>
@ -503,7 +503,10 @@ import detail from "./components/detailContract"
import paymentRegistration from "./components/paymentRegistration";
import contractSign from "@/views/contract/components/contractSign";
import contractPaymentRegistration from "@/views/contract/components/contractPaymentRegistration";
import {
download
} from '@/utils/downloadRequest'
export default {
components: {
editor,
@ -1509,6 +1512,20 @@ export default {
this.select.pageIndex = 1;
this.getContracts();
},
downloadExel() {
download(
'/api/admin/contract/index',
'get', {
is_auth: 1,
is_export: 1,
...this.select
},
'合同列表.xlsx')
},
//
async getContracts(is_export) {
@ -1520,18 +1537,18 @@ export default {
})
let tokens = getToken();
if (is_export) {
this.select.is_export == 1
var url = "/api/admin/contract/index?is_auth=1&token=" + tokens
Object.keys(this.select).map((key, item) => {
url += "&" + key + "=" + this.select[key];
});
url = location.host + url;
console.log(url)
window.open("http://" + url, '_blank')
this.select.is_export = 0
return;
}
// if (is_export) {
// this.select.is_export == 1
// var url = "/api/admin/contract/index?is_auth=1&token=" + tokens
// Object.keys(this.select).map((key, item) => {
// url += "&" + key + "=" + this.select[key];
// });
// url = location.host + url;
// console.log(url)
// window.open("http://" + url, '_blank')
// this.select.is_export = 0
// return;
// }
this.list = res.list.data
this.total = res.list.total

Loading…
Cancel
Save