99 lines
2.7 KiB
TypeScript
99 lines
2.7 KiB
TypeScript
import { message } from 'ant-design-vue';
|
|
|
|
import { requestClient } from '#/api/request';
|
|
|
|
export interface DownloadCenterListParam {
|
|
page?: number;
|
|
pageSize?: number;
|
|
keyword?: string;
|
|
bizKey?: string;
|
|
/** 多个状态用逗号分隔:0排队 2生成中 1已完成 3失败 */
|
|
status?: string;
|
|
createdAt?: string[];
|
|
}
|
|
|
|
export interface DownloadCenterItem {
|
|
id: number;
|
|
taskNo: string;
|
|
bizKey: string;
|
|
bizName: string;
|
|
status: number;
|
|
totalCount: number;
|
|
fileName: string;
|
|
fileSize: number;
|
|
errorMessage?: string;
|
|
createdAt?: string;
|
|
finishTime?: string;
|
|
expireTime?: string;
|
|
deletedTime?: null | string;
|
|
deleteReason?: '' | 'expired' | 'user';
|
|
}
|
|
|
|
export interface DownloadCenterListResult {
|
|
items: DownloadCenterItem[];
|
|
page: number;
|
|
pageSize: number;
|
|
total: number;
|
|
}
|
|
|
|
export interface DownloadCenterRecentResult {
|
|
items: DownloadCenterItem[];
|
|
runningCount: number;
|
|
finishedCount: number;
|
|
}
|
|
|
|
/** 下载中心列表 */
|
|
export function List(params: DownloadCenterListParam) {
|
|
return requestClient.get<DownloadCenterListResult>(
|
|
'/common/downloadCenter/list',
|
|
{
|
|
params,
|
|
},
|
|
);
|
|
}
|
|
|
|
/** 右上角入口:最近任务与进行中数量。
|
|
* 这是辅助信息,无权限或后端未就绪时静默失败,不能弹全局错误打扰用户。 */
|
|
export function Recent(limit = 5) {
|
|
return requestClient.get<DownloadCenterRecentResult>(
|
|
'/common/downloadCenter/recent',
|
|
{ params: { limit }, errorMessageMode: 'none' },
|
|
);
|
|
}
|
|
|
|
/** 下载导出文件:业务错误可能以 HTTP 200 JSON Blob 返回,不能当作 xlsx 保存。 */
|
|
export async function Download(taskId: number): Promise<Blob> {
|
|
const response = await requestClient.download<
|
|
import('axios').AxiosResponse<Blob>
|
|
>(`/common/downloadCenter/download?taskId=${taskId}`, {
|
|
timeout: 120 * 1000,
|
|
responseReturn: 'raw',
|
|
});
|
|
const blob = response.data;
|
|
const contentType = String(
|
|
response.headers['content-type'] || blob?.type || '',
|
|
).toLowerCase();
|
|
if (contentType.includes('json')) {
|
|
let errorMessage = '下载失败,请稍后重试';
|
|
try {
|
|
const payload = JSON.parse(await blob.text());
|
|
errorMessage = payload?.message || errorMessage;
|
|
} catch {
|
|
// Malformed JSON must not be saved as a spreadsheet either.
|
|
}
|
|
message.error(errorMessage);
|
|
throw new Error(errorMessage);
|
|
}
|
|
if (!(blob instanceof Blob) || blob.size === 0) {
|
|
const errorMessage = '下载文件为空,请稍后重试';
|
|
message.error(errorMessage);
|
|
throw new Error(errorMessage);
|
|
}
|
|
return blob;
|
|
}
|
|
|
|
/** 删除导出任务 */
|
|
export function Remove(taskId: number) {
|
|
return requestClient.post('/common/downloadCenter/remove', { taskId });
|
|
}
|