import request from "@/utils/request"; import { Message, MessageBox } from "element-ui"; export function delay(ms = 240) { 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 closeMessage = loadingMessage({ message: "文件下载中..." }); 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(() => closeMessage()); } /** * * @param {ElMessageOptions} options * @returns */ export function loadingMessage({ message, ...args } = {}) { if (!message) return; const loadingMessage = Message.info({ message, duration: 0, customClass: "loading-message", iconClass: "el-icon-loading", ...args, }); return () => loadingMessage.close(); } /** * * @param {{ message: string, title: string; [x: string]: any }} param0 * @returns */ export function confirm({ title = "提示", message = "确定要执行该操作吗?", ...args } = {}) { return new Promise((resolve, reject) => { MessageBox.confirm(message, title, { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning", ...args, }) .then(() => { resolve(); }) .catch(() => { reject("取消 Confirm"); }); }); }