vue循环调接口

     // 文件导出处理函数
        funExport(exportTypes) {
            //导出附件
            const zip = new JSZip()
            const cache = {}
            const promises = []
            this.loading = true
            this.total = exportTypes.length

            if (this.total === 0) {
                this.$message({
                    message: '请选择要导出的信息!',
                })
                this.loading = false
                return
            }
            let finished = 0
            this.loadingTitle = '正在启动下载,请稍后······'
            for (const i in exportTypes) {
                const promise = this.getFile(exportTypes[i]).then((data) => {
                    finished++
                    this.loadingTitle = '正在下载数据,共' + this.total + '个,已完成' + finished + '个'
                    // 获取每个下载文件的文件名
                    const file_name = exportTypes[i] + '.' + this.titleCards[exportTypes[i]] + '.xls'
                    // 将文件压缩
                    zip.file(file_name, data, { binary: true })
                    cache[file_name] = data
                })
                promises.push(promise)
            }
            //全局loading
            Promise.all(promises)
                .then(() => {
                    let zipName = '排污辅助审核导出_' + moment().format('YYYY_MM_DD HH:mm:ss')
                    zip.generateAsync({ type: 'blob' })
                        .then((content) => {
                            //生成zip文件包
                            FileSaver.saveAs(content, `${zipName}.zip`)
                            this.loading = false
                        })
                        .catch((e) => {
                            console.log(e)
                            this.$message.error('排污辅助审核信息导出失败!')
                            this.loading = false
                        })
                })
                .catch((e) => {
                    console.log(e)
                    this.$message.error('排污辅助审核信息导出失败!')
                    this.loading = false
                })
        },
 
    getFile(export_type) {
            return new Promise((resolve, reject) => {
                getAuxiliaryExport({ type: export_type })
                    .then((res) => {
                        resolve(res.data)
                    })
                    .catch((error) => {
                        reject(error.toString())
                    })
            })
        },
posted @ 2022-03-08 10:52  赵辉Coder  阅读(609)  评论(0编辑  收藏  举报