1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import axios from 'axios';
let downInstanceSet = axios.create({
baseURL: process.env.VUE_APP_BASE_URL,
timeout: 60000,
withCredentials: true,
responseType: 'blob'
})
/*为请求添加请求头中的token和请求所需参数userid*/
downInstanceSet.interceptors.request.use(config => {
if (sessionStorage.token) {
config.headers.Authorization = sessionStorage.getItem('token');
if (typeof (config.params) == "undefined") {
config.params = {}
}
}
return config;//赋值完后把config返回回去
}, error => {
// 请求错误后把我们的error返回回去
return Promise.reject(error);
})
//响应拦截器
downInstanceSet.interceptors.response.use(
response => {
let blob = new Blob([response.data], {type: "application/vnd.ms-excel"});
let downloadElement = document.createElement('a')
let url = window.URL.createObjectURL(blob);
downloadElement.href = url;
downloadElement.download = decodeURI(response.headers.filename); //下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
window.URL.revokeObjectURL(url); //释放blob对象
return true;
});
export const downInstance=downInstanceSet;