import { ElMessage, type MessageHandler } from 'element-plus' let downloadBusy = false export function isDownloadBusy(): boolean { return downloadBusy } export function formatDownloadSizeHint(size?: number | null): string { if (size == null || !Number.isFinite(size) || size <= 0) return '' if (size < 1024 * 1024) return `约 ${Math.max(1, Math.round(size / 1024))} KB` return `约 ${(size / (1024 * 1024)).toFixed(1)} MB` } export type DownloadFeedbackHandle = { setText: (text: string) => void close: () => void } /** * 下载过程中的轻量提示(非全屏);若已有下载进行中则提示并返回 null。 */ export function beginDownloadFeedback( fileName: string, size?: number | null, actionLabel = '下载', ): DownloadFeedbackHandle | null { if (downloadBusy) { ElMessage.warning(`附件正在${actionLabel}中,请勿重复点击`) return null } downloadBusy = true const name = fileName.trim() || '附件' const sizeHint = formatDownloadSizeHint(size) const text = sizeHint ? `正在准备${actionLabel}「${name}」(${sizeHint}),文件较大时请耐心等待…` : `正在准备${actionLabel}「${name}」,请稍候…` let handler: MessageHandler = ElMessage({ type: 'info', message: text, duration: 0, showClose: true, }) return { setText: (next: string) => { handler.close() handler = ElMessage({ type: 'info', message: next, duration: 0, showClose: true, }) }, close: () => { handler.close() downloadBusy = false }, } } export function notifyDownloadStarted(): void { ElMessage.success('下载已开始,请查看浏览器下载进度') }