2025-04-22 22:10:33 +08:00
|
|
|
|
import type { AxiosRequestConfig, PageParam, PageResult } from '@vben/request';
|
|
|
|
|
|
|
2025-04-07 10:22:27 +08:00
|
|
|
|
import { requestClient } from '#/api/request';
|
2025-04-17 23:30:34 +08:00
|
|
|
|
|
|
|
|
|
|
/** Axios 上传进度事件 */
|
|
|
|
|
|
export type AxiosProgressEvent = AxiosRequestConfig['onUploadProgress'];
|
2025-04-07 10:22:27 +08:00
|
|
|
|
|
|
|
|
|
|
export namespace InfraFileApi {
|
|
|
|
|
|
/** 文件信息 */
|
2025-04-22 22:10:33 +08:00
|
|
|
|
export interface File {
|
2025-04-07 10:22:27 +08:00
|
|
|
|
id?: number;
|
|
|
|
|
|
configId?: number;
|
|
|
|
|
|
path: string;
|
|
|
|
|
|
name?: string;
|
|
|
|
|
|
url?: string;
|
|
|
|
|
|
size?: number;
|
|
|
|
|
|
type?: string;
|
|
|
|
|
|
createTime?: Date;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 文件预签名地址 */
|
|
|
|
|
|
export interface FilePresignedUrlRespVO {
|
|
|
|
|
|
configId: number; // 文件配置编号
|
|
|
|
|
|
uploadUrl: string; // 文件上传 URL
|
|
|
|
|
|
url: string; // 文件 URL
|
2025-05-02 19:59:05 +08:00
|
|
|
|
path: string; // 文件路径
|
2025-04-07 10:22:27 +08:00
|
|
|
|
}
|
2025-04-18 18:30:50 +08:00
|
|
|
|
|
|
|
|
|
|
/** 上传文件 */
|
|
|
|
|
|
export interface FileUploadReqVO {
|
2025-04-23 12:56:35 +08:00
|
|
|
|
file: globalThis.File;
|
2025-05-02 19:59:05 +08:00
|
|
|
|
directory?: string;
|
2025-04-18 18:30:50 +08:00
|
|
|
|
}
|
2025-04-07 10:22:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 查询文件列表 */
|
|
|
|
|
|
export function getFilePage(params: PageParam) {
|
2025-04-22 22:10:33 +08:00
|
|
|
|
return requestClient.get<PageResult<InfraFileApi.File>>('/infra/file/page', {
|
|
|
|
|
|
params,
|
2025-04-07 10:22:27 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 删除文件 */
|
|
|
|
|
|
export function deleteFile(id: number) {
|
|
|
|
|
|
return requestClient.delete(`/infra/file/delete?id=${id}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 获取文件预签名地址 */
|
2025-05-02 19:59:05 +08:00
|
|
|
|
export function getFilePresignedUrl(name: string, directory?: string) {
|
2025-04-22 22:10:33 +08:00
|
|
|
|
return requestClient.get<InfraFileApi.FilePresignedUrlRespVO>(
|
|
|
|
|
|
'/infra/file/presigned-url',
|
|
|
|
|
|
{
|
2025-05-02 19:59:05 +08:00
|
|
|
|
params: { name, directory },
|
2025-04-22 22:10:33 +08:00
|
|
|
|
},
|
|
|
|
|
|
);
|
2025-04-07 10:22:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 创建文件 */
|
2025-04-22 22:10:33 +08:00
|
|
|
|
export function createFile(data: InfraFileApi.File) {
|
2025-04-07 10:22:27 +08:00
|
|
|
|
return requestClient.post('/infra/file/create', data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 上传文件 */
|
2025-04-22 22:10:33 +08:00
|
|
|
|
export function uploadFile(
|
|
|
|
|
|
data: InfraFileApi.FileUploadReqVO,
|
|
|
|
|
|
onUploadProgress?: AxiosProgressEvent,
|
|
|
|
|
|
) {
|
2025-05-02 19:59:05 +08:00
|
|
|
|
// 特殊:由于 upload 内部封装,即使 directory 为 undefined,也会传递给后端
|
|
|
|
|
|
if (!data.directory) {
|
|
|
|
|
|
delete data.directory;
|
|
|
|
|
|
}
|
2025-04-17 23:30:34 +08:00
|
|
|
|
return requestClient.upload('/infra/file/upload', data, { onUploadProgress });
|
2025-04-07 10:22:27 +08:00
|
|
|
|
}
|