mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 17:09:19 +08:00
feat: 添加浏览器扩展下载功能
This commit is contained in:
@@ -2,12 +2,11 @@
|
||||
* 通用文件导出工具
|
||||
* 用于下载 Excel、PDF 等二进制文件
|
||||
*/
|
||||
import { useAppConfig } from '@vben/hooks';
|
||||
import { useAccessStore } from '@vben/stores';
|
||||
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
import { useAppConfig } from '@vben/hooks';
|
||||
|
||||
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
||||
|
||||
function formatToken(token: null | string) {
|
||||
@@ -34,6 +33,67 @@ function parseFilenameFromContentDisposition(
|
||||
return null;
|
||||
}
|
||||
|
||||
async function triggerBlobDownload(
|
||||
response: Response,
|
||||
fallbackFilename: string,
|
||||
) {
|
||||
const contentType = response.headers.get('Content-Type') || '';
|
||||
const blob = await response.blob();
|
||||
|
||||
if (blob.size === 0) {
|
||||
throw new Error('下载文件为空');
|
||||
}
|
||||
|
||||
if (contentType.includes('application/json')) {
|
||||
const text = await blob.text();
|
||||
try {
|
||||
const json = JSON.parse(text);
|
||||
message.error(json.message || '下载失败');
|
||||
} catch {
|
||||
message.error('下载失败');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const headerName =
|
||||
parseFilenameFromContentDisposition(
|
||||
response.headers.get('Content-Disposition'),
|
||||
) || fallbackFilename;
|
||||
|
||||
const downloadUrl = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = downloadUrl;
|
||||
link.download = headerName;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
window.URL.revokeObjectURL(downloadUrl);
|
||||
}
|
||||
|
||||
export async function exportByGet(url: string, filename: string) {
|
||||
const accessStore = useAccessStore();
|
||||
const fullUrl = `${apiURL}${url}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(fullUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: formatToken(accessStore.accessToken) || '',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
await triggerBlobDownload(response, filename);
|
||||
message.success('下载成功');
|
||||
} catch (error) {
|
||||
console.error('下载失败:', error);
|
||||
message.error('下载失败,请稍后重试');
|
||||
}
|
||||
}
|
||||
|
||||
export async function exportByPost(
|
||||
url: string,
|
||||
params: Record<string, unknown>,
|
||||
@@ -56,39 +116,7 @@ export async function exportByPost(
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('Content-Type') || '';
|
||||
const blob = await response.blob();
|
||||
|
||||
if (blob.size === 0) {
|
||||
throw new Error('下载文件为空');
|
||||
}
|
||||
|
||||
// 如果返回的是 JSON 错误(而非文件),提示错误信息
|
||||
if (contentType.includes('application/json')) {
|
||||
const text = await blob.text();
|
||||
try {
|
||||
const json = JSON.parse(text);
|
||||
message.error(json.message || '导出失败');
|
||||
} catch {
|
||||
message.error('导出失败');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const headerName =
|
||||
parseFilenameFromContentDisposition(
|
||||
response.headers.get('Content-Disposition'),
|
||||
) || filename;
|
||||
|
||||
// 触发浏览器下载
|
||||
const downloadUrl = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = downloadUrl;
|
||||
link.download = headerName;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
window.URL.revokeObjectURL(downloadUrl);
|
||||
await triggerBlobDownload(response, filename);
|
||||
message.success('导出成功');
|
||||
} catch (error) {
|
||||
console.error('导出失败:', error);
|
||||
|
||||
Reference in New Issue
Block a user