评价打分

This commit is contained in:
linxd
2025-06-27 15:02:47 +08:00
parent ebacfc4135
commit ad241f7adb
14 changed files with 1110 additions and 200 deletions

58
src/utils/download.ts Normal file
View File

@ -0,0 +1,58 @@
/**
* 通用文件下载工具(基于 umi-request + file-saver
*/
import { extend } from 'umi-request';
import { saveAs } from 'file-saver';
// 单独配置一个不拦截响应的 request 实例
const downloadRequest = extend({
timeout: 10000,
responseType: 'blob', // 关键
credentials: 'include', // 根据实际需要携带 cookie
prefix: REQUEST_BASE,
});
/**
* 下载文件通用函数
* @param url 请求地址
* @param params 请求参数(可选)
* @param filename 下载后的文件名(可选)
* @param method 请求方法(默认 POST
*/
export async function downloadFile({
url,
params,
filename,
method = 'POST',
}: {
url: string;
params?: Record<string, any>;
filename?: string;
method?: 'GET' | 'POST';
}) {
try {
const blob: Blob = await downloadRequest(url, {
method,
data: method === 'POST' ? params : undefined,
params: method === 'GET' ? params : undefined,
});
// 尝试从响应头中获取文件名
const contentDisposition = (blob as any).response?.headers?.get?.('content-disposition');
let finalFilename = filename;
if (!finalFilename && contentDisposition) {
const match = contentDisposition.match(/filename="?([^"]+)"?/);
if (match && match[1]) {
finalFilename = decodeURIComponent(match[1]);
}
}
finalFilename = finalFilename || '下载文件';
saveAs(blob, finalFilename);
} catch (error) {
console.error('文件下载失败:', error);
}
}