|
@@ -1,33 +1,54 @@
|
|
|
-import React, { useState, useEffect } from 'react';
|
|
|
|
|
-import { Table, Button, Space, Input, Modal, Form, Select, DatePicker, Upload, Popconfirm, Image } from 'antd';
|
|
|
|
|
-import { App } from 'antd';
|
|
|
|
|
-import { PlusOutlined, EditOutlined, DeleteOutlined, SearchOutlined, UploadOutlined, DownloadOutlined, EyeOutlined } from '@ant-design/icons';
|
|
|
|
|
|
|
+import React, { useState } from 'react';
|
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
+import { Button } from '@/client/components/ui/button';
|
|
|
|
|
+import { Input } from '@/client/components/ui/input';
|
|
|
|
|
+import { Card, CardContent, CardHeader, CardTitle } from '@/client/components/ui/card';
|
|
|
|
|
+import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/client/components/ui/dialog';
|
|
|
|
|
+import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/client/components/ui/form';
|
|
|
|
|
+import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/client/components/ui/table';
|
|
|
|
|
+import { Badge } from '@/client/components/ui/badge';
|
|
|
|
|
+import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/client/components/ui/alert-dialog';
|
|
|
|
|
+import { useForm } from 'react-hook-form';
|
|
|
|
|
+import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
|
+import { toast } from 'sonner';
|
|
|
|
|
+import { Eye, Download, Edit, Trash2, Search, FileText, Upload } from 'lucide-react';
|
|
|
import { fileClient } from '@/client/api';
|
|
import { fileClient } from '@/client/api';
|
|
|
import type { InferResponseType, InferRequestType } from 'hono/client';
|
|
import type { InferResponseType, InferRequestType } from 'hono/client';
|
|
|
import dayjs from 'dayjs';
|
|
import dayjs from 'dayjs';
|
|
|
-import { uploadMinIOWithPolicy } from '@/client/utils/minio';
|
|
|
|
|
|
|
+import MinioUploader from '@/client/admin/components/MinioUploader';
|
|
|
|
|
+import { UpdateFileDto } from '@/server/modules/files/file.schema';
|
|
|
|
|
+import * as z from 'zod';
|
|
|
|
|
|
|
|
// 定义类型
|
|
// 定义类型
|
|
|
type FileItem = InferResponseType<typeof fileClient.$get, 200>['data'][0];
|
|
type FileItem = InferResponseType<typeof fileClient.$get, 200>['data'][0];
|
|
|
type FileListResponse = InferResponseType<typeof fileClient.$get, 200>;
|
|
type FileListResponse = InferResponseType<typeof fileClient.$get, 200>;
|
|
|
type UpdateFileRequest = InferRequestType<typeof fileClient[':id']['$put']>['json'];
|
|
type UpdateFileRequest = InferRequestType<typeof fileClient[':id']['$put']>['json'];
|
|
|
|
|
+type FileFormData = z.infer<typeof UpdateFileDto>;
|
|
|
|
|
|
|
|
export const FilesPage: React.FC = () => {
|
|
export const FilesPage: React.FC = () => {
|
|
|
- const { message } = App.useApp();
|
|
|
|
|
- const [form] = Form.useForm();
|
|
|
|
|
- const [modalVisible, setModalVisible] = useState(false);
|
|
|
|
|
- const [editingKey, setEditingKey] = useState<number | null>(null);
|
|
|
|
|
|
|
+ const [isModalOpen, setIsModalOpen] = useState(false);
|
|
|
|
|
+ const [isUploadModalOpen, setIsUploadModalOpen] = useState(false);
|
|
|
|
|
+ const [editingFile, setEditingFile] = useState<FileItem | null>(null);
|
|
|
const [searchText, setSearchText] = useState('');
|
|
const [searchText, setSearchText] = useState('');
|
|
|
const [pagination, setPagination] = useState({
|
|
const [pagination, setPagination] = useState({
|
|
|
current: 1,
|
|
current: 1,
|
|
|
pageSize: 10,
|
|
pageSize: 10,
|
|
|
total: 0,
|
|
total: 0,
|
|
|
});
|
|
});
|
|
|
|
|
+ const [deleteFileId, setDeleteFileId] = useState<number | null>(null);
|
|
|
|
|
+ const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
|
|
|
|
|
|
|
|
const queryClient = useQueryClient();
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 表单初始化
|
|
|
|
|
+ const form = useForm<FileFormData>({
|
|
|
|
|
+ resolver: zodResolver(UpdateFileDto),
|
|
|
|
|
+ defaultValues: {
|
|
|
|
|
+ name: '',
|
|
|
|
|
+ description: '',
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
// 获取文件列表数据
|
|
// 获取文件列表数据
|
|
|
const fetchFiles = async ({ page, pageSize }: { page: number; pageSize: number }): Promise<FileListResponse> => {
|
|
const fetchFiles = async ({ page, pageSize }: { page: number; pageSize: number }): Promise<FileListResponse> => {
|
|
|
const response = await fileClient.$get({ query: { page, pageSize, keyword: searchText } });
|
|
const response = await fileClient.$get({ query: { page, pageSize, keyword: searchText } });
|
|
@@ -35,389 +56,413 @@ export const FilesPage: React.FC = () => {
|
|
|
return await response.json() as FileListResponse;
|
|
return await response.json() as FileListResponse;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // 获取文件下载URL
|
|
|
|
|
- const getFileUrl = 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) {
|
|
|
|
|
- message.error('获取文件URL失败');
|
|
|
|
|
- return null;
|
|
|
|
|
|
|
+ const { data, isLoading, error } = useQuery({
|
|
|
|
|
+ queryKey: ['files', pagination.current, pagination.pageSize, searchText],
|
|
|
|
|
+ queryFn: () => fetchFiles({ page: pagination.current, pageSize: pagination.pageSize }),
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 更新文件记录
|
|
|
|
|
+ const updateFile = useMutation({
|
|
|
|
|
+ mutationFn: ({ id, data }: { id: number; data: UpdateFileRequest }) =>
|
|
|
|
|
+ fileClient[':id'].$put({ param: { id: id.toString() }, json: data }),
|
|
|
|
|
+ onSuccess: () => {
|
|
|
|
|
+ toast.success('文件记录更新成功');
|
|
|
|
|
+ queryClient.invalidateQueries({ queryKey: ['files'] });
|
|
|
|
|
+ setIsModalOpen(false);
|
|
|
|
|
+ setEditingFile(null);
|
|
|
|
|
+ },
|
|
|
|
|
+ onError: (error: Error) => {
|
|
|
|
|
+ toast.error(`操作失败: ${error.message}`);
|
|
|
}
|
|
}
|
|
|
- };
|
|
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- // 获取文件下载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) {
|
|
|
|
|
- message.error('获取文件下载URL失败');
|
|
|
|
|
- return null;
|
|
|
|
|
|
|
+ // 删除文件记录
|
|
|
|
|
+ const deleteFile = useMutation({
|
|
|
|
|
+ mutationFn: (id: number) => fileClient[':id'].$delete({ param: { id: id.toString() } }),
|
|
|
|
|
+ onSuccess: () => {
|
|
|
|
|
+ toast.success('文件记录删除成功');
|
|
|
|
|
+ queryClient.invalidateQueries({ queryKey: ['files'] });
|
|
|
|
|
+ },
|
|
|
|
|
+ onError: (error: Error) => {
|
|
|
|
|
+ toast.error(`删除失败: ${error.message}`);
|
|
|
}
|
|
}
|
|
|
- };
|
|
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
// 处理文件下载
|
|
// 处理文件下载
|
|
|
- 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 getFileUrl(record.id);
|
|
|
|
|
- if (url) {
|
|
|
|
|
- if (record.type.startsWith('image/')) {
|
|
|
|
|
- window.open(url, '_blank');
|
|
|
|
|
- } else if (record.type.startsWith('video/')) {
|
|
|
|
|
- window.open(url, '_blank');
|
|
|
|
|
- } else {
|
|
|
|
|
- message.warning('该文件类型不支持预览');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const handlePreview = (record: FileItem) => {
|
|
|
|
|
+ if (isPreviewable(record.type)) {
|
|
|
|
|
+ window.open(record.fullUrl, '_blank');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ toast.warning('该文件类型不支持预览');
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// 检查是否为可预览的文件类型
|
|
// 检查是否为可预览的文件类型
|
|
|
- const isPreviewable = (fileType: string) => {
|
|
|
|
|
|
|
+ const isPreviewable = (fileType: string | null) => {
|
|
|
|
|
+ if (!fileType) return false;
|
|
|
return fileType.startsWith('image/') || fileType.startsWith('video/');
|
|
return fileType.startsWith('image/') || fileType.startsWith('video/');
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
- const { data, isLoading: loading, error: filesError } = useQuery({
|
|
|
|
|
- queryKey: ['files', pagination.current, pagination.pageSize, searchText],
|
|
|
|
|
- queryFn: () => fetchFiles({ page: pagination.current, pageSize: pagination.pageSize }),
|
|
|
|
|
- });
|
|
|
|
|
|
|
|
|
|
- // 错误处理
|
|
|
|
|
- if (filesError) {
|
|
|
|
|
- message.error(`获取文件列表失败: ${filesError instanceof Error ? filesError.message : '未知错误'}`);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 从API响应获取分页数据
|
|
|
|
|
- const tablePagination = data?.pagination || pagination;
|
|
|
|
|
-
|
|
|
|
|
- // 搜索
|
|
|
|
|
- const handleSearch = () => {
|
|
|
|
|
- setPagination({ ...pagination, current: 1 });
|
|
|
|
|
|
|
+ // 处理上传成功回调
|
|
|
|
|
+ const handleUploadSuccess = (fileKey: string, fileUrl: string, file: File) => {
|
|
|
|
|
+ toast.success('文件上传成功');
|
|
|
|
|
+ queryClient.invalidateQueries({ queryKey: ['files'] });
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
- // 分页变化
|
|
|
|
|
- const handleTableChange = (newPagination: any) => {
|
|
|
|
|
- setPagination(newPagination);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 处理上传失败回调
|
|
|
|
|
+ const handleUploadError = (error: Error, file: File) => {
|
|
|
|
|
+ toast.error(`上传失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 显示编辑弹窗
|
|
// 显示编辑弹窗
|
|
|
- const showModal = (record: FileItem) => {
|
|
|
|
|
- setModalVisible(true);
|
|
|
|
|
- setEditingKey(record.id);
|
|
|
|
|
- form.setFieldsValue({
|
|
|
|
|
|
|
+ const showEditModal = (record: FileItem) => {
|
|
|
|
|
+ setEditingFile(record);
|
|
|
|
|
+ setIsModalOpen(true);
|
|
|
|
|
+ form.reset({
|
|
|
name: record.name,
|
|
name: record.name,
|
|
|
- description: record.description,
|
|
|
|
|
- type: record.type,
|
|
|
|
|
- size: record.size,
|
|
|
|
|
|
|
+ description: record.description || '',
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // 关闭弹窗
|
|
|
|
|
- const handleCancel = () => {
|
|
|
|
|
- setModalVisible(false);
|
|
|
|
|
- form.resetFields();
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- // 更新文件记录
|
|
|
|
|
- const updateFile = useMutation({
|
|
|
|
|
- mutationFn: ({ id, data }: { id: number; data: UpdateFileRequest }) =>
|
|
|
|
|
- fileClient[':id'].$put({ param: { id }, json: data }),
|
|
|
|
|
- onSuccess: () => {
|
|
|
|
|
- message.success('文件记录更新成功');
|
|
|
|
|
- queryClient.invalidateQueries({ queryKey: ['files'] });
|
|
|
|
|
- setModalVisible(false);
|
|
|
|
|
- },
|
|
|
|
|
- onError: (error: Error) => {
|
|
|
|
|
- message.error(`操作失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
|
|
|
|
+ // 处理表单提交
|
|
|
|
|
+ const handleFormSubmit = async (data: FileFormData) => {
|
|
|
|
|
+ if (editingFile) {
|
|
|
|
|
+ await updateFile.mutateAsync({
|
|
|
|
|
+ id: editingFile.id,
|
|
|
|
|
+ data: {
|
|
|
|
|
+ name: data.name,
|
|
|
|
|
+ description: data.description,
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 删除文件记录
|
|
|
|
|
- const deleteFile = useMutation({
|
|
|
|
|
- mutationFn: (id: number) => fileClient[':id'].$delete({ param: { id } }),
|
|
|
|
|
- onSuccess: () => {
|
|
|
|
|
- message.success('文件记录删除成功');
|
|
|
|
|
- queryClient.invalidateQueries({ queryKey: ['files'] });
|
|
|
|
|
- },
|
|
|
|
|
- onError: (error: Error) => {
|
|
|
|
|
- message.error(`删除失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 直接上传文件
|
|
|
|
|
- const handleDirectUpload = async () => {
|
|
|
|
|
- const input = document.createElement('input');
|
|
|
|
|
- input.type = 'file';
|
|
|
|
|
- input.multiple = false;
|
|
|
|
|
-
|
|
|
|
|
- input.onchange = async (e) => {
|
|
|
|
|
- const file = (e.target as HTMLInputElement).files?.[0];
|
|
|
|
|
- if (!file) return;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- message.loading('正在上传文件...');
|
|
|
|
|
- await uploadMinIOWithPolicy('/files', file, file.name);
|
|
|
|
|
- message.success('文件上传成功');
|
|
|
|
|
- queryClient.invalidateQueries({ queryKey: ['files'] });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- message.error(`上传失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- input.click();
|
|
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
- // 提交表单(仅用于编辑已上传文件)
|
|
|
|
|
- const handleSubmit = async () => {
|
|
|
|
|
- try {
|
|
|
|
|
- const values = await form.validateFields();
|
|
|
|
|
-
|
|
|
|
|
- const payload = {
|
|
|
|
|
- name: values.name,
|
|
|
|
|
- description: values.description,
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- if (editingKey) {
|
|
|
|
|
- await updateFile.mutateAsync({ id: editingKey, data: payload });
|
|
|
|
|
- }
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- message.error('表单验证失败,请检查输入');
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 处理删除确认
|
|
|
|
|
+ const handleDeleteConfirm = () => {
|
|
|
|
|
+ if (deleteFileId) {
|
|
|
|
|
+ deleteFile.mutate(deleteFileId);
|
|
|
|
|
+ setIsDeleteDialogOpen(false);
|
|
|
|
|
+ setDeleteFileId(null);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
- // 表格列定义
|
|
|
|
|
- const columns = [
|
|
|
|
|
- {
|
|
|
|
|
- title: '文件ID',
|
|
|
|
|
- dataIndex: 'id',
|
|
|
|
|
- key: 'id',
|
|
|
|
|
- width: 80,
|
|
|
|
|
- align: 'center' as const,
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- title: '文件名称',
|
|
|
|
|
- dataIndex: 'name',
|
|
|
|
|
- key: 'name',
|
|
|
|
|
- width: 300,
|
|
|
|
|
- ellipsis: true,
|
|
|
|
|
- render: (name: string, record: FileItem) => (
|
|
|
|
|
- <div className="flex items-center">
|
|
|
|
|
- <span className="flex-1">{name}</span>
|
|
|
|
|
- </div>
|
|
|
|
|
- ),
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- title: '文件类型',
|
|
|
|
|
- dataIndex: 'type',
|
|
|
|
|
- key: 'type',
|
|
|
|
|
- width: 120,
|
|
|
|
|
- render: (type: string) => (
|
|
|
|
|
- <span className="inline-block px-2 py-1 text-xs bg-blue-50 text-blue-700 rounded-full">
|
|
|
|
|
- {type}
|
|
|
|
|
- </span>
|
|
|
|
|
- ),
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- title: '文件大小',
|
|
|
|
|
- dataIndex: 'size',
|
|
|
|
|
- key: 'size',
|
|
|
|
|
- width: 120,
|
|
|
|
|
- render: (size: number) => (
|
|
|
|
|
- <span className="text-sm">
|
|
|
|
|
- {size ? `${(size / 1024).toFixed(2)} KB` : '-'}
|
|
|
|
|
- </span>
|
|
|
|
|
- ),
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- title: '上传时间',
|
|
|
|
|
- dataIndex: 'uploadTime',
|
|
|
|
|
- key: 'uploadTime',
|
|
|
|
|
- width: 180,
|
|
|
|
|
- render: (time: string) => (
|
|
|
|
|
- <span className="text-sm text-gray-600">
|
|
|
|
|
- {time ? dayjs(time).format('YYYY-MM-DD HH:mm:ss') : '-'}
|
|
|
|
|
- </span>
|
|
|
|
|
- ),
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- title: '上传用户',
|
|
|
|
|
- dataIndex: 'uploadUser',
|
|
|
|
|
- key: 'uploadUser',
|
|
|
|
|
- width: 120,
|
|
|
|
|
- render: (uploadUser?: { username: string; nickname?: string }) => (
|
|
|
|
|
- <span className="text-sm">
|
|
|
|
|
- {uploadUser ? (uploadUser.nickname || uploadUser.username) : '-'}
|
|
|
|
|
- </span>
|
|
|
|
|
- ),
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- title: '操作',
|
|
|
|
|
- key: 'action',
|
|
|
|
|
- width: 200,
|
|
|
|
|
- fixed: 'right' as const,
|
|
|
|
|
- render: (_: any, record: FileItem) => (
|
|
|
|
|
- <Space size="small">
|
|
|
|
|
- <Button
|
|
|
|
|
- type="text"
|
|
|
|
|
- icon={<EyeOutlined />}
|
|
|
|
|
- onClick={() => handlePreview(record)}
|
|
|
|
|
- className="text-green-600 hover:text-green-800 hover:bg-green-50"
|
|
|
|
|
- disabled={!isPreviewable(record.type)}
|
|
|
|
|
- title={isPreviewable(record.type) ? '预览文件' : '该文件类型不支持预览'}
|
|
|
|
|
- />
|
|
|
|
|
- <Button
|
|
|
|
|
- type="text"
|
|
|
|
|
- icon={<DownloadOutlined />}
|
|
|
|
|
- onClick={() => handleDownload(record)}
|
|
|
|
|
- className="text-blue-600 hover:text-blue-800 hover:bg-blue-50"
|
|
|
|
|
- title="下载文件"
|
|
|
|
|
- />
|
|
|
|
|
- <Button
|
|
|
|
|
- type="text"
|
|
|
|
|
- icon={<EditOutlined />}
|
|
|
|
|
- onClick={() => showModal(record)}
|
|
|
|
|
- className="text-purple-600 hover:text-purple-800 hover:bg-purple-50"
|
|
|
|
|
- title="编辑文件信息"
|
|
|
|
|
- />
|
|
|
|
|
- <Popconfirm
|
|
|
|
|
- title="确认删除"
|
|
|
|
|
- description={`确定要删除文件"${record.name}"吗?此操作不可恢复。`}
|
|
|
|
|
- onConfirm={() => deleteFile.mutate(record.id)}
|
|
|
|
|
- okText="确认"
|
|
|
|
|
- cancelText="取消"
|
|
|
|
|
- okButtonProps={{ danger: true }}
|
|
|
|
|
- >
|
|
|
|
|
- <Button
|
|
|
|
|
- type="text"
|
|
|
|
|
- danger
|
|
|
|
|
- icon={<DeleteOutlined />}
|
|
|
|
|
- className="hover:bg-red-50"
|
|
|
|
|
- title="删除文件"
|
|
|
|
|
- >
|
|
|
|
|
- 删除
|
|
|
|
|
- </Button>
|
|
|
|
|
- </Popconfirm>
|
|
|
|
|
- </Space>
|
|
|
|
|
- ),
|
|
|
|
|
- },
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const handleSearch = () => {
|
|
|
|
|
+ setPagination({ ...pagination, current: 1 });
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // 格式化文件大小
|
|
|
|
|
+ const formatFileSize = (bytes: number | null) => {
|
|
|
|
|
+ if (!bytes || bytes === 0) return '0 Bytes';
|
|
|
|
|
+ const k = 1024;
|
|
|
|
|
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
|
|
|
|
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
|
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // 分页数据
|
|
|
|
|
+ const tablePagination = data?.pagination || pagination;
|
|
|
|
|
+
|
|
|
|
|
+ if (error) {
|
|
|
|
|
+ return (
|
|
|
|
|
+ <div className="p-6">
|
|
|
|
|
+ <Card>
|
|
|
|
|
+ <CardContent className="text-center py-8">
|
|
|
|
|
+ <FileText className="h-12 w-12 mx-auto text-gray-400 mb-4" />
|
|
|
|
|
+ <p className="text-gray-600">获取文件列表失败</p>
|
|
|
|
|
+ </CardContent>
|
|
|
|
|
+ </Card>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return (
|
|
return (
|
|
|
- <div className="p-6">
|
|
|
|
|
- <div className="mb-6 flex justify-between items-center">
|
|
|
|
|
- <h2 className="text-2xl font-bold text-gray-900">文件管理</h2>
|
|
|
|
|
- <Button
|
|
|
|
|
- type="primary"
|
|
|
|
|
- icon={<UploadOutlined />}
|
|
|
|
|
- onClick={handleDirectUpload}
|
|
|
|
|
- className="h-10 flex items-center"
|
|
|
|
|
- >
|
|
|
|
|
|
|
+ <div className="p-6 space-y-6">
|
|
|
|
|
+ <div className="flex justify-between items-center">
|
|
|
|
|
+ <h1 className="text-3xl font-bold">文件管理</h1>
|
|
|
|
|
+ <Button onClick={() => setIsUploadModalOpen(true)}>
|
|
|
|
|
+ <Upload className="h-4 w-4 mr-2" />
|
|
|
上传文件
|
|
上传文件
|
|
|
</Button>
|
|
</Button>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
- <div className="mb-6">
|
|
|
|
|
- <div className="flex items-center gap-4">
|
|
|
|
|
- <Input
|
|
|
|
|
- placeholder="搜索文件名称或类型"
|
|
|
|
|
- prefix={<SearchOutlined />}
|
|
|
|
|
- value={searchText}
|
|
|
|
|
- onChange={(e) => setSearchText(e.target.value)}
|
|
|
|
|
- onPressEnter={handleSearch}
|
|
|
|
|
- className="w-80 h-10"
|
|
|
|
|
- allowClear
|
|
|
|
|
- />
|
|
|
|
|
- <Button
|
|
|
|
|
- type="default"
|
|
|
|
|
- onClick={handleSearch}
|
|
|
|
|
- className="h-10"
|
|
|
|
|
- >
|
|
|
|
|
- 搜索
|
|
|
|
|
- </Button>
|
|
|
|
|
- </div>
|
|
|
|
|
- </div>
|
|
|
|
|
-
|
|
|
|
|
- <div className="bg-white rounded-lg shadow-sm transition-all duration-300 hover:shadow-md">
|
|
|
|
|
- <Table
|
|
|
|
|
- columns={columns}
|
|
|
|
|
- dataSource={data?.data || []}
|
|
|
|
|
- rowKey="id"
|
|
|
|
|
- loading={loading}
|
|
|
|
|
- pagination={{
|
|
|
|
|
- ...tablePagination,
|
|
|
|
|
- showSizeChanger: true,
|
|
|
|
|
- showQuickJumper: true,
|
|
|
|
|
- showTotal: (total, range) =>
|
|
|
|
|
- `显示 ${range[0]}-${range[1]} 条,共 ${total} 条`,
|
|
|
|
|
- }}
|
|
|
|
|
- onChange={handleTableChange}
|
|
|
|
|
- bordered={false}
|
|
|
|
|
- scroll={{ x: 'max-content' }}
|
|
|
|
|
- className="[&_.ant-table]:!rounded-lg [&_.ant-table-thead>tr>th]:!bg-gray-50 [&_.ant-table-thead>tr>th]:!font-semibold [&_.ant-table-thead>tr>th]:!text-gray-700 [&_.ant-table-thead>tr>th]:!border-b-2 [&_.ant-table-thead>tr>th]:!border-gray-200"
|
|
|
|
|
- rowClassName={(record, index) => index % 2 === 0 ? 'bg-white' : 'bg-gray-50'}
|
|
|
|
|
- />
|
|
|
|
|
- </div>
|
|
|
|
|
-
|
|
|
|
|
- <Modal
|
|
|
|
|
- title="编辑文件信息"
|
|
|
|
|
- open={modalVisible}
|
|
|
|
|
- onCancel={handleCancel}
|
|
|
|
|
- footer={[
|
|
|
|
|
- <Button key="cancel" onClick={handleCancel}>
|
|
|
|
|
- 取消
|
|
|
|
|
- </Button>,
|
|
|
|
|
- <Button
|
|
|
|
|
- key="submit"
|
|
|
|
|
- type="primary"
|
|
|
|
|
- onClick={handleSubmit}
|
|
|
|
|
- loading={updateFile.isPending}
|
|
|
|
|
- >
|
|
|
|
|
- 确定
|
|
|
|
|
- </Button>,
|
|
|
|
|
- ]}
|
|
|
|
|
- width={600}
|
|
|
|
|
- centered
|
|
|
|
|
- destroyOnClose
|
|
|
|
|
- maskClosable={false}
|
|
|
|
|
- >
|
|
|
|
|
- <Form form={form} layout="vertical">
|
|
|
|
|
- <Form.Item name="name" label="文件名称">
|
|
|
|
|
- <Input className="h-10" />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
|
|
+ <Card>
|
|
|
|
|
+ <CardHeader>
|
|
|
|
|
+ <CardTitle>文件列表</CardTitle>
|
|
|
|
|
+ </CardHeader>
|
|
|
|
|
+ <CardContent>
|
|
|
|
|
+ <div className="mb-4 flex gap-4">
|
|
|
|
|
+ <div className="flex-1">
|
|
|
|
|
+ <Input
|
|
|
|
|
+ placeholder="搜索文件名称或类型"
|
|
|
|
|
+ value={searchText}
|
|
|
|
|
+ onChange={(e) => setSearchText(e.target.value)}
|
|
|
|
|
+ onKeyPress={(e) => e.key === 'Enter' && handleSearch()}
|
|
|
|
|
+ className="max-w-sm"
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <Button onClick={handleSearch}>
|
|
|
|
|
+ <Search className="h-4 w-4 mr-2" />
|
|
|
|
|
+ 搜索
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="overflow-x-auto">
|
|
|
|
|
+ <Table>
|
|
|
|
|
+ <TableHeader>
|
|
|
|
|
+ <TableRow>
|
|
|
|
|
+ <TableHead className="w-16">ID</TableHead>
|
|
|
|
|
+ <TableHead>预览</TableHead>
|
|
|
|
|
+ <TableHead>文件名称</TableHead>
|
|
|
|
|
+ <TableHead>文件类型</TableHead>
|
|
|
|
|
+ <TableHead>文件大小</TableHead>
|
|
|
|
|
+ <TableHead>上传时间</TableHead>
|
|
|
|
|
+ <TableHead>上传用户</TableHead>
|
|
|
|
|
+ <TableHead className="text-right">操作</TableHead>
|
|
|
|
|
+ </TableRow>
|
|
|
|
|
+ </TableHeader>
|
|
|
|
|
+ <TableBody>
|
|
|
|
|
+ {isLoading ? (
|
|
|
|
|
+ <TableRow>
|
|
|
|
|
+ <TableCell colSpan={7} className="text-center">
|
|
|
|
|
+ <div className="flex justify-center items-center py-8">
|
|
|
|
|
+ <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ </TableRow>
|
|
|
|
|
+ ) : data?.data?.length === 0 ? (
|
|
|
|
|
+ <TableRow>
|
|
|
|
|
+ <TableCell colSpan={7} className="text-center py-8">
|
|
|
|
|
+ <FileText className="h-12 w-12 mx-auto text-gray-400 mb-4" />
|
|
|
|
|
+ <p className="text-gray-600">暂无文件</p>
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ </TableRow>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ data?.data?.map((file) => (
|
|
|
|
|
+ <TableRow key={file.id}>
|
|
|
|
|
+ <TableCell className="font-medium">{file.id}</TableCell>
|
|
|
|
|
+ <TableCell>
|
|
|
|
|
+ {isPreviewable(file.type) ? (
|
|
|
|
|
+ <img
|
|
|
|
|
+ src={file.fullUrl}
|
|
|
|
|
+ alt={file.name}
|
|
|
|
|
+ className="w-12 h-12 object-cover rounded border cursor-pointer hover:opacity-80 transition-opacity"
|
|
|
|
|
+ onClick={() => handlePreview(file)}
|
|
|
|
|
+ title="点击查看大图"
|
|
|
|
|
+ />
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <div className="w-12 h-12 flex items-center justify-center bg-gray-100 rounded border">
|
|
|
|
|
+ <FileText className="h-6 w-6 text-gray-400" />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ <TableCell>
|
|
|
|
|
+ <div className="max-w-xs truncate" title={file.name}>
|
|
|
|
|
+ {file.name}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ <TableCell>
|
|
|
|
|
+ <Badge variant="secondary">{file.type}</Badge>
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ <TableCell>{formatFileSize(file.size)}</TableCell>
|
|
|
|
|
+ <TableCell>
|
|
|
|
|
+ {file.uploadTime ? dayjs(file.uploadTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ <TableCell>
|
|
|
|
|
+ {file.uploadUser ? (file.uploadUser.nickname || file.uploadUser.username) : '-'}
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ <TableCell className="text-right">
|
|
|
|
|
+ <div className="flex justify-end gap-2">
|
|
|
|
|
+ <Button
|
|
|
|
|
+ variant="ghost"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ onClick={() => handlePreview(file)}
|
|
|
|
|
+ disabled={!isPreviewable(file.type)}
|
|
|
|
|
+ title={isPreviewable(file.type) ? '预览文件' : '该文件类型不支持预览'}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Eye className="h-4 w-4" />
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ <Button
|
|
|
|
|
+ variant="ghost"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ onClick={() => handleDownload(file)}
|
|
|
|
|
+ title="下载文件"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Download className="h-4 w-4" />
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ <Button
|
|
|
|
|
+ variant="ghost"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ onClick={() => showEditModal(file)}
|
|
|
|
|
+ title="编辑文件信息"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Edit className="h-4 w-4" />
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ <Button
|
|
|
|
|
+ variant="ghost"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ onClick={() => {
|
|
|
|
|
+ setDeleteFileId(file.id);
|
|
|
|
|
+ setIsDeleteDialogOpen(true);
|
|
|
|
|
+ }}
|
|
|
|
|
+ className="text-red-600 hover:text-red-700"
|
|
|
|
|
+ title="删除文件"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Trash2 className="h-4 w-4" />
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </TableCell>
|
|
|
|
|
+ </TableRow>
|
|
|
|
|
+ ))
|
|
|
|
|
+ )}
|
|
|
|
|
+ </TableBody>
|
|
|
|
|
+ </Table>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 分页 */}
|
|
|
|
|
+ {tablePagination.total > 0 && (
|
|
|
|
|
+ <div className="flex justify-between items-center mt-4">
|
|
|
|
|
+ <div className="text-sm text-gray-600">
|
|
|
|
|
+ 显示 {((tablePagination.current - 1) * tablePagination.pageSize + 1)}-
|
|
|
|
|
+ {Math.min(tablePagination.current * tablePagination.pageSize, tablePagination.total)} 条,
|
|
|
|
|
+ 共 {tablePagination.total} 条
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div className="flex gap-2">
|
|
|
|
|
+ <Button
|
|
|
|
|
+ variant="outline"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ disabled={tablePagination.current <= 1}
|
|
|
|
|
+ onClick={() => setPagination({ ...pagination, current: tablePagination.current - 1 })}
|
|
|
|
|
+ >
|
|
|
|
|
+ 上一页
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ <span className="px-3 py-1 text-sm">
|
|
|
|
|
+ 第 {tablePagination.current} 页
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <Button
|
|
|
|
|
+ variant="outline"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ disabled={tablePagination.current >= Math.ceil(tablePagination.total / tablePagination.pageSize)}
|
|
|
|
|
+ onClick={() => setPagination({ ...pagination, current: tablePagination.current + 1 })}
|
|
|
|
|
+ >
|
|
|
|
|
+ 下一页
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </CardContent>
|
|
|
|
|
+ </Card>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 上传文件对话框 */}
|
|
|
|
|
+ <Dialog open={isUploadModalOpen} onOpenChange={setIsUploadModalOpen}>
|
|
|
|
|
+ <DialogContent className="sm:max-w-[600px]">
|
|
|
|
|
+ <DialogHeader>
|
|
|
|
|
+ <DialogTitle>上传文件</DialogTitle>
|
|
|
|
|
+ <DialogDescription>
|
|
|
|
|
+ 选择要上传的文件,支持拖拽上传
|
|
|
|
|
+ </DialogDescription>
|
|
|
|
|
+ </DialogHeader>
|
|
|
|
|
|
|
|
- <Form.Item name="description" label="文件描述">
|
|
|
|
|
- <Input.TextArea
|
|
|
|
|
- rows={4}
|
|
|
|
|
- placeholder="请输入文件描述"
|
|
|
|
|
- className="rounded-md"
|
|
|
|
|
|
|
+ <div className="py-4">
|
|
|
|
|
+ <MinioUploader
|
|
|
|
|
+ uploadPath="/files"
|
|
|
|
|
+ maxSize={500}
|
|
|
|
|
+ multiple={false}
|
|
|
|
|
+ onUploadSuccess={(fileKey, fileUrl, file) => {
|
|
|
|
|
+ handleUploadSuccess(fileKey, fileUrl, file);
|
|
|
|
|
+ setIsUploadModalOpen(false);
|
|
|
|
|
+ }}
|
|
|
|
|
+ onUploadError={handleUploadError}
|
|
|
|
|
+ buttonText="点击或拖拽上传文件"
|
|
|
|
|
+ tipText="支持单文件上传,单个文件大小不超过500MB"
|
|
|
|
|
+ size="default"
|
|
|
/>
|
|
/>
|
|
|
- </Form.Item>
|
|
|
|
|
-
|
|
|
|
|
- <Form.Item name="type" label="文件类型" hidden>
|
|
|
|
|
- <Input />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
|
|
+ </div>
|
|
|
|
|
|
|
|
- <Form.Item name="size" label="文件大小" hidden>
|
|
|
|
|
- <Input />
|
|
|
|
|
- </Form.Item>
|
|
|
|
|
- </Form>
|
|
|
|
|
- </Modal>
|
|
|
|
|
|
|
+ <DialogFooter>
|
|
|
|
|
+ <Button variant="outline" onClick={() => setIsUploadModalOpen(false)}>
|
|
|
|
|
+ 取消
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </DialogFooter>
|
|
|
|
|
+ </DialogContent>
|
|
|
|
|
+ </Dialog>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 编辑对话框 */}
|
|
|
|
|
+ <Dialog open={isModalOpen} onOpenChange={setIsModalOpen}>
|
|
|
|
|
+ <DialogContent className="sm:max-w-[500px]">
|
|
|
|
|
+ <DialogHeader>
|
|
|
|
|
+ <DialogTitle>编辑文件信息</DialogTitle>
|
|
|
|
|
+ <DialogDescription>
|
|
|
|
|
+ 修改文件的基本信息
|
|
|
|
|
+ </DialogDescription>
|
|
|
|
|
+ </DialogHeader>
|
|
|
|
|
+ <Form {...form}>
|
|
|
|
|
+ <form onSubmit={form.handleSubmit(handleFormSubmit)} className="space-y-4">
|
|
|
|
|
+ <FormField
|
|
|
|
|
+ control={form.control}
|
|
|
|
|
+ name="name"
|
|
|
|
|
+ render={({ field }) => (
|
|
|
|
|
+ <FormItem>
|
|
|
|
|
+ <FormLabel>文件名称</FormLabel>
|
|
|
|
|
+ <FormControl>
|
|
|
|
|
+ <Input placeholder="请输入文件名称" {...field} />
|
|
|
|
|
+ </FormControl>
|
|
|
|
|
+ <FormMessage />
|
|
|
|
|
+ </FormItem>
|
|
|
|
|
+ )}
|
|
|
|
|
+ />
|
|
|
|
|
+ <FormField
|
|
|
|
|
+ control={form.control}
|
|
|
|
|
+ name="description"
|
|
|
|
|
+ render={({ field }) => (
|
|
|
|
|
+ <FormItem>
|
|
|
|
|
+ <FormLabel>文件描述</FormLabel>
|
|
|
|
|
+ <FormControl>
|
|
|
|
|
+ <Input placeholder="请输入文件描述" {...field} />
|
|
|
|
|
+ </FormControl>
|
|
|
|
|
+ <FormMessage />
|
|
|
|
|
+ </FormItem>
|
|
|
|
|
+ )}
|
|
|
|
|
+ />
|
|
|
|
|
+ <DialogFooter>
|
|
|
|
|
+ <Button type="button" variant="outline" onClick={() => setIsModalOpen(false)}>
|
|
|
|
|
+ 取消
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ <Button type="submit" disabled={updateFile.isPending}>
|
|
|
|
|
+ {updateFile.isPending ? '保存中...' : '保存'}
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </DialogFooter>
|
|
|
|
|
+ </form>
|
|
|
|
|
+ </Form>
|
|
|
|
|
+ </DialogContent>
|
|
|
|
|
+ </Dialog>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 删除确认对话框 */}
|
|
|
|
|
+ <AlertDialog open={isDeleteDialogOpen} onOpenChange={setIsDeleteDialogOpen}>
|
|
|
|
|
+ <AlertDialogContent>
|
|
|
|
|
+ <AlertDialogHeader>
|
|
|
|
|
+ <AlertDialogTitle>确认删除</AlertDialogTitle>
|
|
|
|
|
+ <AlertDialogDescription>
|
|
|
|
|
+ 确定要删除这个文件记录吗?此操作不可恢复。
|
|
|
|
|
+ </AlertDialogDescription>
|
|
|
|
|
+ </AlertDialogHeader>
|
|
|
|
|
+ <AlertDialogFooter>
|
|
|
|
|
+ <AlertDialogCancel>取消</AlertDialogCancel>
|
|
|
|
|
+ <AlertDialogAction onClick={handleDeleteConfirm} className="bg-red-600 hover:bg-red-700">
|
|
|
|
|
+ 确认删除
|
|
|
|
|
+ </AlertDialogAction>
|
|
|
|
|
+ </AlertDialogFooter>
|
|
|
|
|
+ </AlertDialogContent>
|
|
|
|
|
+ </AlertDialog>
|
|
|
</div>
|
|
</div>
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|