vue迴圈調介面
阿新 • • 發佈:2022-03-08
// 檔案匯出處理函式
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()) }) }) },
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()) }) }) },