From ecc0784a80319eb8fe709a8829ae67dc2dca9886 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 22 Feb 2021 09:22:41 +0800 Subject: [PATCH] refactor: backup file download. (#297) --- src/api/backup.js | 21 ++++++++++++ .../system/components/BackupWorkDirDrawer.vue | 20 ++++++++++- .../system/components/ExportDataDrawer.vue | 34 ++++++++++++++----- .../components/ExportMarkdownDrawer.vue | 20 ++++++++++- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/src/api/backup.js b/src/api/backup.js index 352e8183..a18c6050 100644 --- a/src/api/backup.js +++ b/src/api/backup.js @@ -30,6 +30,13 @@ backupApi.listWorkDirBackups = () => { }) } +backupApi.fetchWorkDir = filename => { + return service({ + url: `${baseUrl}/work-dir/fetch?filename=${filename}`, + method: 'get' + }) +} + backupApi.deleteWorkDirBackup = filename => { return service({ url: `${baseUrl}/work-dir`, @@ -55,6 +62,13 @@ backupApi.listExportedData = () => { }) } +backupApi.fetchData = filename => { + return service({ + url: `${baseUrl}/data/fetch?filename=${filename}`, + method: 'get' + }) +} + backupApi.deleteExportedData = filename => { return service({ url: `${baseUrl}/data`, @@ -83,6 +97,13 @@ backupApi.listExportedMarkdowns = () => { }) } +backupApi.fetchMarkdown = filename => { + return service({ + url: `${baseUrl}/markdown/fetch?filename=${filename}`, + method: 'get' + }) +} + backupApi.deleteExportedMarkdown = filename => { return service({ url: `${baseUrl}/markdown/export`, diff --git a/src/views/system/components/BackupWorkDirDrawer.vue b/src/views/system/components/BackupWorkDirDrawer.vue index f9bc4112..389a7704 100644 --- a/src/views/system/components/BackupWorkDirDrawer.vue +++ b/src/views/system/components/BackupWorkDirDrawer.vue @@ -40,7 +40,8 @@ { + var downloadElement = document.createElement('a') + var href = new window.URL(response.data.data.downloadLink) + downloadElement.href = href + downloadElement.download = response.data.data.filename + document.body.appendChild(downloadElement) + downloadElement.click() + document.body.removeChild(downloadElement) + window.URL.revokeObjectURL(href) + }) + .catch((error) => { + this.$message.error(error.data.message) + }) + }, onClose() { this.$emit('close', false) }, diff --git a/src/views/system/components/ExportDataDrawer.vue b/src/views/system/components/ExportDataDrawer.vue index dd8ca677..465ae117 100644 --- a/src/views/system/components/ExportDataDrawer.vue +++ b/src/views/system/components/ExportDataDrawer.vue @@ -40,7 +40,8 @@ { + .then((response) => { this.files = response.data.data }) .finally(() => { @@ -151,9 +152,26 @@ export default { this.handleListBackups() }) }, + handleDownloadBackupFile(item) { + backupApi + .fetchData(item.filename) + .then((response) => { + var downloadElement = document.createElement('a') + var href = new window.URL(response.data.data.downloadLink) + downloadElement.href = href + downloadElement.download = response.data.data.filename + document.body.appendChild(downloadElement) + downloadElement.click() + document.body.removeChild(downloadElement) + window.URL.revokeObjectURL(href) + }) + .catch(() => { + this.$message.error('下载失败!') + }) + }, onClose() { this.$emit('close', false) - } - } + }, + }, } diff --git a/src/views/system/components/ExportMarkdownDrawer.vue b/src/views/system/components/ExportMarkdownDrawer.vue index e5b371bc..fca2bc36 100644 --- a/src/views/system/components/ExportMarkdownDrawer.vue +++ b/src/views/system/components/ExportMarkdownDrawer.vue @@ -40,7 +40,8 @@ { + var downloadElement = document.createElement('a') + var href = new window.URL(response.data.data.downloadLink) + downloadElement.href = href + downloadElement.download = response.data.data.filename + document.body.appendChild(downloadElement) + downloadElement.click() + document.body.removeChild(downloadElement) + window.URL.revokeObjectURL(href) + }) + .catch(() => { + this.$message.error('下载失败!') + }) + }, onClose() { this.$emit('close', false) },