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)
|
}
|