zhouzhou
21 小时以前 fc9103d3c93617bf4b739a05d01bca6b22f8ecc2
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { $t } from '@/locales'
import { useUserStore } from '@/store/modules/user'
import type { BaseResponse } from '@/types'
import request from '@/utils/http'
 
const { VITE_API_URL, VITE_WITH_CREDENTIALS } = import.meta.env
 
export function fetchDbBackupConfig() {
  return request.get<Api.System.DbBackup.DbBackupConfig>({
    url: '/dbBackup/config'
  })
}
 
export function saveDbBackupConfig(data: Api.System.DbBackup.DbBackupConfig) {
  return request.post<void>({
    url: '/dbBackup/config/save',
    data
  })
}
 
export function fetchDbBackupTables() {
  return request.get<Api.System.DbBackup.DbBackupTable[]>({
    url: '/dbBackup/tables'
  })
}
 
export function runDbBackup(tables: string[]) {
  return request.post<{ id: number; status: string }>({
    url: '/dbBackup/run',
    data: { tables }
  })
}
 
export function fetchDbBackupProgress() {
  return request.get<Api.System.DbBackup.DbBackupProgress>({
    url: '/dbBackup/progress'
  })
}
 
export function fetchDbBackupHistory(params: Api.System.DbBackup.DbBackupHistoryParams) {
  return request.get<Api.System.DbBackup.DbBackupHistoryResponse>({
    url: '/dbBackup/history',
    params
  })
}
 
export function deleteDbBackup(id: number | string) {
  return request.post<void>({
    url: '/dbBackup/delete',
    data: { id }
  })
}
 
const parseFilename = (contentDisposition: string | null, fallbackFilename: string) => {
  if (!contentDisposition) return fallbackFilename
 
  const utf8Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i)
  if (utf8Match?.[1]) {
    return decodeURIComponent(utf8Match[1])
  }
 
  const plainMatch = contentDisposition.match(/filename="?([^";]+)"?/i)
  if (plainMatch?.[1]) {
    return decodeURIComponent(plainMatch[1])
  }
 
  return fallbackFilename
}
 
export async function downloadDbBackup(row: Api.System.DbBackup.DbBackupRecord) {
  const { accessToken, tenantId } = useUserStore()
  const response = await fetch(`${VITE_API_URL}/dbBackup/download?id=${row.id}`, {
    method: 'GET',
    credentials: VITE_WITH_CREDENTIALS === 'true' ? 'include' : 'same-origin',
    headers: {
      Authorization: accessToken || '',
      ...(tenantId !== null && tenantId !== undefined && tenantId !== ''
        ? { 'X-Tenant-Id': String(tenantId) }
        : {})
    }
  })
 
  const contentType = response.headers.get('content-type') || ''
  if (!response.ok || contentType.includes('application/json')) {
    let errorMessage = '文件下载失败'
    try {
      const errorResponse = (await response.json()) as BaseResponse<unknown>
      errorMessage = errorResponse.msg || errorMessage
    } catch {
      errorMessage = response.statusText || errorMessage
    }
    throw new Error(errorMessage || $t('httpMsg.requestFailed'))
  }
 
  const blob = await response.blob()
  const downloadUrl = window.URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = downloadUrl
  link.download = parseFilename(
    response.headers.get('content-disposition'),
    row.fileName ? `${row.fileName}.zip` : 'db-backup.zip'
  )
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  window.URL.revokeObjectURL(downloadUrl)
}