fix: 修复S3备份下载URL生成逻辑并导出API基础URL函数
- 导出`getApiBaseUrl`函数以便复用 - 修改`downloadS3BackupUrl`函数,使其使用正确的API基础URL - 重构S3备份下载处理,使用fetch API并添加授权头,支持blob下载
This commit is contained in:
@@ -424,5 +424,9 @@ export function deleteS3Backup(key: string): Promise<{ message: string }> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function downloadS3BackupUrl(key: string): string {
|
export function downloadS3BackupUrl(key: string): string {
|
||||||
return `/admin/backup/s3/download?key=${encodeURIComponent(key)}`;
|
const rawEnvApiBaseUrl = (import.meta.env.VITE_API_BASE_URL || '').trim();
|
||||||
|
const stored = (localStorage.getItem('cwd_admin_api_base_url') || '').trim();
|
||||||
|
const source = stored || rawEnvApiBaseUrl;
|
||||||
|
const apiBaseUrl = source.replace(/\/+$/, '');
|
||||||
|
return `${apiBaseUrl}/admin/backup/s3/download?key=${encodeURIComponent(key)}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const rawEnvApiBaseUrl = (import.meta.env.VITE_API_BASE_URL || '').trim();
|
const rawEnvApiBaseUrl = (import.meta.env.VITE_API_BASE_URL || '').trim();
|
||||||
|
|
||||||
function getApiBaseUrl(): string {
|
export function getApiBaseUrl(): string {
|
||||||
const stored = (localStorage.getItem('cwd_admin_api_base_url') || '').trim();
|
const stored = (localStorage.getItem('cwd_admin_api_base_url') || '').trim();
|
||||||
const source = stored || rawEnvApiBaseUrl;
|
const source = stored || rawEnvApiBaseUrl;
|
||||||
const apiBaseUrl = source.replace(/\/+$/, '');
|
const apiBaseUrl = source.replace(/\/+$/, '');
|
||||||
|
|||||||
@@ -57,9 +57,9 @@ import { useI18n } from "vue-i18n";
|
|||||||
import {
|
import {
|
||||||
fetchS3BackupList,
|
fetchS3BackupList,
|
||||||
deleteS3Backup,
|
deleteS3Backup,
|
||||||
downloadS3BackupUrl,
|
|
||||||
S3BackupItem,
|
S3BackupItem,
|
||||||
} from "../../../api/admin";
|
} from "../../../api/admin";
|
||||||
|
import { getApiBaseUrl } from "../../../api/http";
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
visible: boolean;
|
visible: boolean;
|
||||||
@@ -83,9 +83,34 @@ const handleClose = () => {
|
|||||||
emit("close");
|
emit("close");
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDownload = (key: string) => {
|
const handleDownload = async (key: string) => {
|
||||||
const url = downloadS3BackupUrl(key);
|
try {
|
||||||
window.open(url, "_blank");
|
const apiBaseUrl = getApiBaseUrl();
|
||||||
|
const token = localStorage.getItem('cwd_admin_token');
|
||||||
|
const url = `${apiBaseUrl}/admin/backup/s3/download?key=${encodeURIComponent(key)}`;
|
||||||
|
|
||||||
|
const res = await fetch(url, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: token ? { 'Authorization': `Bearer ${token}` } : {},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(`下载失败: ${res.status} ${res.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const blob = await res.blob();
|
||||||
|
const fileName = key;
|
||||||
|
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = window.URL.createObjectURL(blob);
|
||||||
|
a.download = fileName;
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
window.URL.revokeObjectURL(a.href);
|
||||||
|
document.body.removeChild(a);
|
||||||
|
} catch (e: any) {
|
||||||
|
console.error('Download error:', e);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = async (key: string) => {
|
const handleDelete = async (key: string) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user