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;






