|
|
@@ -55,32 +55,6 @@ export const FilesPage: React.FC = () => {
|
|
|
return await response.json() as FileListResponse;
|
|
|
};
|
|
|
|
|
|
- // 获取文件下载URL
|
|
|
- const getFileDownloadUrl = async (fileId: number) => {
|
|
|
- try {
|
|
|
- const response = await fileClient[':id']['download'].$get({ param: { id: fileId } });
|
|
|
- if (!response.ok) throw new Error('获取文件下载URL失败');
|
|
|
- const data = await response.json();
|
|
|
- return data;
|
|
|
- } catch (error) {
|
|
|
- toast.error('获取文件下载URL失败');
|
|
|
- return null;
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- // 获取文件预览URL
|
|
|
- const getFilePreviewUrl = async (fileId: number) => {
|
|
|
- try {
|
|
|
- const response = await fileClient[':id']['url'].$get({ param: { id: fileId } });
|
|
|
- if (!response.ok) throw new Error('获取文件URL失败');
|
|
|
- const data = await response.json();
|
|
|
- return data.url;
|
|
|
- } catch (error) {
|
|
|
- toast.error('获取文件URL失败');
|
|
|
- return null;
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
const { data, isLoading, error } = useQuery({
|
|
|
queryKey: ['files', pagination.current, pagination.pageSize, searchText],
|
|
|
queryFn: () => fetchFiles({ page: pagination.current, pageSize: pagination.pageSize }),
|
|
|
@@ -114,29 +88,21 @@ export const FilesPage: React.FC = () => {
|
|
|
});
|
|
|
|
|
|
// 处理文件下载
|
|
|
- const handleDownload = async (record: FileItem) => {
|
|
|
- const result = await getFileDownloadUrl(record.id);
|
|
|
- if (result?.url) {
|
|
|
- const a = document.createElement('a');
|
|
|
- a.href = result.url;
|
|
|
- a.download = result.filename || record.name;
|
|
|
- document.body.appendChild(a);
|
|
|
- a.click();
|
|
|
- document.body.removeChild(a);
|
|
|
- }
|
|
|
+ const handleDownload = (record: FileItem) => {
|
|
|
+ const a = document.createElement('a');
|
|
|
+ a.href = record.fullUrl;
|
|
|
+ a.download = record.name;
|
|
|
+ document.body.appendChild(a);
|
|
|
+ a.click();
|
|
|
+ document.body.removeChild(a);
|
|
|
};
|
|
|
|
|
|
// 处理文件预览
|
|
|
- const handlePreview = async (record: FileItem) => {
|
|
|
- const url = await getFilePreviewUrl(record.id);
|
|
|
- if (url) {
|
|
|
- if (record.type.startsWith('image/')) {
|
|
|
- window.open(url, '_blank');
|
|
|
- } else if (record.type.startsWith('video/')) {
|
|
|
- window.open(url, '_blank');
|
|
|
- } else {
|
|
|
- toast.warning('该文件类型不支持预览');
|
|
|
- }
|
|
|
+ const handlePreview = (record: FileItem) => {
|
|
|
+ if (isPreviewable(record.type)) {
|
|
|
+ window.open(record.fullUrl, '_blank');
|
|
|
+ } else {
|
|
|
+ toast.warning('该文件类型不支持预览');
|
|
|
}
|
|
|
};
|
|
|
|