美文网首页
封装一个Ant-Design-Vue的展示下载进度的组件

封装一个Ant-Design-Vue的展示下载进度的组件

作者: FrankFang7 | 来源:发表于2024-12-25 16:51 被阅读0次

/src/libs/util.downloadNotification.js

import { ref, h } from 'vue'
import { notification, Progress } from 'ant-design-vue'

// 默认总用时
const total = 20000
const percent = ref({})
const message = ref({})
const status = ref({})
const intervalTimer = ref({})
const timeoutTimer = ref({})
// 开始下载
export const openDownload = (key) => {
    percent.value[key] = 0
    message.value[key] = '文件下载中...'
    status.value[key] = null
    intervalTimer.value[key] = setInterval(() => {
        if (percent.value[key] < 98) {
            percent.value[key]++
        } else {
            clearInterval(intervalTimer.value[key])
            intervalTimer.value[key] = null
        }
    }, total / 100)
    notification.open({
        message: () => message.value[key],
        duration: 0,
        description: () =>
            h(
                Progress,
                {
                    percent: percent.value[key],
                    size: 'small',
                    status: status.value[key]
                },
            ),
        top: 60,
        style: {
            width: '280px',
            marginRight: '-15px'
        },
        key,
        class: 'download-notification',
    })
}
// 结束下载
export const closeDownload = (key, succeed) => {
    console.log('close')
    if (succeed) {
        message.value[key] = '下载成功'
        percent.value[key] = 100
    } else {
        message.value[key] = '下载失败'
        status.value[key] = 'exception'
    }
    clearInterval(intervalTimer.value[key])
    intervalTimer.value[key] = null
    timeoutTimer.value[key] = setTimeout(() => {
        notification.close(key)
        clearTimeout(timeoutTimer.value[key])
        timeoutTimer.value[key] = null
    }, 3000)
}

使用

import { openDownload, closeDownload } from '@/libs/util.downloadNotification.js'
const key = `key${Date.now()}`
openDownload(key)
closeDownload(key)

相关文章

网友评论

      本文标题:封装一个Ant-Design-Vue的展示下载进度的组件

      本文链接:https://www.haomeiwen.com/subject/zvrbejtx.html