| | |
| | | // 通用下载方法
|
| | | export function download(url, params, filename, config) {
|
| | | downloadLoadingInstance = ElLoading.service({ text: "正在下载数据,请稍候", background: "rgba(0, 0, 0, 0.7)", })
|
| | | return service.post(url, params, {
|
| | | transformRequest: [(params) => { return tansParams(params) }],
|
| | | headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
| | | responseType: 'blob',
|
| | | ...config
|
| | | }).then(async (data) => {
|
| | | const downloadName = config?.filename || filename |
| | | const requestMethod = (config?.method || 'post').toLowerCase() |
| | | const requestPromise = requestMethod === 'get' |
| | | ? service.get(url, { |
| | | params, |
| | | responseType: 'blob', |
| | | ...config |
| | | }) |
| | | : service.post(url, params, { |
| | | transformRequest: [(params) => { return tansParams(params) }], |
| | | headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| | | responseType: 'blob', |
| | | ...config |
| | | }) |
| | | return requestPromise.then(async (data) => { |
| | | const isBlob = blobValidate(data)
|
| | | if (isBlob) {
|
| | | const blob = new Blob([data])
|
| | | saveAs(blob, filename)
|
| | | saveAs(blob, downloadName) |
| | | } else {
|
| | | const resText = await data.text()
|
| | | const rspObj = JSON.parse(resText)
|