| | |
| | | import axios from 'axios' |
| | | import { |
| | | handleResponseSuccess, |
| | | handleResponseFail, |
| | | handleRequest |
| | | } from './interceptor-handler' |
| | | import { |
| | | showFullScreenLoading |
| | | } from '@/utils/loading' |
| | | import {handleRequest, handleResponseFail, handleResponseSuccess} from './interceptor-handler' |
| | | import {showFullScreenLoading} from '@/utils/loading' |
| | | |
| | | // URL 记录器 |
| | | import urlRecorder from './url-recorder' |
| | |
| | | return axiosInstance(config) |
| | | }, |
| | | // DELETE更新数据 |
| | | delete(url, data = {}, throwError) { |
| | | delete(url, params = {}, data = {}, throwError) { |
| | | const config = { |
| | | method: 'delete', |
| | | params, |
| | | url, |
| | | data: JSON.stringify(data), |
| | | errorHandler: (!throwError && this.defaultErrorHandler) || null, |
| | |
| | | return axiosInstance(config) |
| | | }, |
| | | /** |
| | | * 通过表单get下载文件。并统一处理常见的错误 |
| | | * 通过表单下载文件。并统一处理常见的错误 |
| | | * @param {string} url |
| | | * @param {String} fileName 文件名 |
| | | * @param params 传参 |
| | | * @param {Object?} data 上传进度回调,参数为event |
| | | * @param {Function?} throwError 是否不使用默认的异常处理方法,而把异常抛出来 |
| | | * @return {Promise} 返回一个promise对象 |
| | | */ |
| | | downloadFile(url, params, data = {}, throwError) { |
| | | const { |
| | | fileName, |
| | | type |
| | | } = params |
| | | return axiosInstance({ |
| | | method: 'GET', |
| | | downloadFile(url, params= {}, data = {}, throwError) { |
| | | const config = { |
| | | method: 'POST', |
| | | url, |
| | | responseType: 'arraybuffer', |
| | | params, |
| | | data: JSON.stringify(data), |
| | | responseType: 'arraybuffer', |
| | | errorHandler: (!throwError && this.defaultErrorHandler) || null, |
| | | timeout: 50000, |
| | | headers: { |
| | | 'Content-Type': 'application/json' |
| | | } |
| | | }).then(response => { |
| | | const blob = new Blob([response], { |
| | | type |
| | | }) // 不兼容type |
| | | // 利用a标签实现下载 |
| | | const link = document.createElement('a') |
| | | link.style.display = 'none' |
| | | link.setAttribute('type', MimeType) // 并不支持 |
| | | const downUrl = window.URL.createObjectURL(blob) |
| | | link.href = downUrl |
| | | // 添加到浏览器为了兼容 firefox |
| | | document.body.appendChild(link) |
| | | // 为了兼容qq浏览器,fileName中必须加上文件后缀 |
| | | link.download = fileName |
| | | link.click() |
| | | document.body.removeChild(link) |
| | | }) |
| | | }, |
| | | /** |
| | | * 通过a标签打开文件 |
| | | * @param {string} url |
| | | */ |
| | | downloadFileByA(url) { |
| | | // const {fileName} = params |
| | | // 利用a标签实现下载 |
| | | const link = document.createElement('a') |
| | | link.style.display = 'none' |
| | | link.href = `${process.env.VUE_APP_DOWN_FILE}${url}` |
| | | // 添加到浏览器为了兼容 firefox |
| | | document.body.appendChild(link) |
| | | // 为了兼容qq浏览器,fileName中必须加上文件后缀 |
| | | // link.download = fileName |
| | | link.click() |
| | | document.body.removeChild(link) |
| | | return Promise.resolve() |
| | | }, |
| | | /** |
| | | * 通过a标签下载文件 |
| | | * @param {string} url |
| | | */ |
| | | downloadFileByAtag(url) { |
| | | // 利用a标签实现下载 |
| | | const link = document.createElement('a') |
| | | link.download = url |
| | | link.target = '_blank' |
| | | link.style.display = 'none' |
| | | link.href = url |
| | | document.body.appendChild(link) |
| | | link.click() |
| | | document.body.removeChild(link) |
| | | return Promise.resolve() |
| | | }, |
| | | withCredentials: this.withCredentials |
| | | } |
| | | urlRecorder.add(config) |
| | | if (data.showLoading === undefined) { |
| | | showFullScreenLoading() |
| | | } |
| | | return axiosInstance(config) |
| | | }, |
| | | /** |
| | | * 通过表单post上传文件并接收json格式的数据。并统一处理常见的错误 |