gaoluyang
2026-09-01 9f1ae9374801f614272cf25101f515d4b0145dd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<script lang="ts" setup>
import { DatabaseBackup } from '../../../../../icons/src';
import { VbenIconButton } from '../../../../../@core/ui-kit/shadcn-ui/src';
import { useVbenModal } from '../../../../../@core/ui-kit/popup-ui/src';
import { backupDatabase } from '../../../../../../api/system/database';
import { ref } from 'vue';
 
const isDownloading = ref(false);
const downloadProgress = ref(0);
 
const [Modal, modalApi] = useVbenModal({
  onConfirm: async () => {
    modalApi.close();
    try {
      isDownloading.value = true;
      downloadProgress.value = 0;
      await backupDatabase((progressEvent) => {
        if (progressEvent.total) {
          downloadProgress.value = Math.round((progressEvent.loaded * 100) / progressEvent.total);
        }
      });
    } catch (error: any) {
      // Ignore if it's the Blob being thrown by the response interceptor
      if (!(error instanceof Blob) && !(error?.response?.data instanceof Blob)) {
        console.error('Backup failed:', error);
      }
    } finally {
      isDownloading.value = false;
      downloadProgress.value = 0;
    }
  },
});
 
function handleOpen() {
  modalApi.open();
}
</script>
 
<template>
  <div class="relative">
    <VbenIconButton
      v-access:code="['system:database:backup']"
      class="hover:animate-[shrink_0.3s_ease-in-out]"
      @click="handleOpen"
    >
      <DatabaseBackup class="size-4 text-foreground" />
    </VbenIconButton>
 
    <Modal
      title="提示"
      content-class="px-8 min-h-10"
      footer-class="border-none mb-3 mr-3"
      header-class="border-none"
    >
      确认要下载数据库的备份文件吗?
    </Modal>
 
    <teleport to="body">
      <div
        v-if="isDownloading"
        class="fixed inset-0 z-[9999] flex items-center justify-center bg-black/50 backdrop-blur-sm"
      >
        <div class="flex flex-col items-center space-y-4 rounded-lg bg-background p-6 shadow-lg">
          <div class="size-8 animate-spin rounded-full border-4 border-primary border-t-transparent"></div>
          <p class="text-lg font-medium text-foreground">正在下载备份: {{ downloadProgress }}%</p>
        </div>
      </div>
    </teleport>
  </div>
</template>