Joe
10 months ago
5 changed files with 114 additions and 43 deletions
@ -1,3 +1,56 @@ |
|||||
|
import request from "@/utils/request"; |
||||
|
import { Message } from "element-ui"; |
||||
|
|
||||
export function delay(ms = 240) { |
export function delay(ms = 240) { |
||||
return new Promise((resolve) => setTimeout(() => resolve(void 0), ms)); |
return new Promise((resolve) => setTimeout(() => resolve(void 0), ms)); |
||||
} |
} |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @param {{ method?: string; url: string; data?: string; type?: string; filename?: string; }} options |
||||
|
*/ |
||||
|
export function exportFile({ |
||||
|
url, |
||||
|
method = "post", |
||||
|
data, |
||||
|
type = "application/vnd.ms-excel", |
||||
|
filename = "download", |
||||
|
ext = "xlsx", |
||||
|
} = {}) { |
||||
|
if (!url) return; |
||||
|
const loadingMessage = Message.info({ |
||||
|
message: `文件下载中...`, |
||||
|
duration: 0, |
||||
|
customClass: "loading-message", |
||||
|
iconClass: "el-icon-loading", |
||||
|
}); |
||||
|
|
||||
|
request({ |
||||
|
url, |
||||
|
method, |
||||
|
responseType: "blob", |
||||
|
data: { |
||||
|
...data, |
||||
|
}, |
||||
|
params: { |
||||
|
...data, |
||||
|
}, |
||||
|
}) |
||||
|
.then((result) => { |
||||
|
const blob = new Blob([result], { type }); |
||||
|
const downloadElement = document.createElement("a"); |
||||
|
const href = window.URL.createObjectURL(blob); |
||||
|
downloadElement.href = href; |
||||
|
downloadElement.download = result.filename || `${filename}.${ext}`; |
||||
|
document.body.appendChild(downloadElement); |
||||
|
downloadElement.click(); |
||||
|
document.body.removeChild(downloadElement); |
||||
|
window.URL.revokeObjectURL(href); |
||||
|
|
||||
|
Message.success("文件下载成功"); |
||||
|
}) |
||||
|
.catch((err) => { |
||||
|
Message.error("文件下载失败"); |
||||
|
}) |
||||
|
.finally(() => loadingMessage.close()); |
||||
|
} |
||||
|
Loading…
Reference in new issue