2026-09-02 5dbd5f73288a1dd739de5d2383678e19ba2aece9
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
import { requestClient } from '#/api/request';
 
function downloadBlob(blob: Blob, headers?: any) {
  let filename = 'backup.sql';
  if (headers) {
    const disposition = headers['content-disposition'] || headers['Content-Disposition'];
    if (disposition && disposition.indexOf('filename=') !== -1) {
      const matches = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
      if (matches != null && matches[1]) {
        filename = matches[1].replace(/['"]/g, '');
      }
    }
  }
  
  const url = window.URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.style.display = 'none';
  link.href = url;
  link.setAttribute('download', decodeURIComponent(filename));
  document.body.append(link);
  link.click();
  link.remove();
  window.URL.revokeObjectURL(url);
}
 
/** 下载数据库备份文件 */
export function backupDatabase(onDownloadProgress?: (progressEvent: any) => void) {
  return requestClient.request('/system/database/backup', {
    method: 'GET',
    responseType: 'blob',
    timeout: 300000, // 5分钟
    onDownloadProgress,
    responseReturn: 'raw',
  }).then((res: any) => {
    const blob = res.data || res;
    if (blob instanceof Blob) {
      downloadBlob(blob, res.headers);
    }
    return res;
  }).catch((err: any) => {
    if (err instanceof Blob) {
      downloadBlob(err);
      return err;
    } else if (err?.response?.data instanceof Blob) {
      downloadBlob(err.response.data, err.response.headers);
      return err;
    }
    throw err;
  });
}