import React, { useState, useCallback, useRef } from 'react'; import { Modal, Upload, Button, Progress, List, Typography, Space, Alert, message, Spin, Tag, Result, } from 'antd'; import { InboxOutlined, FileOutlined, CheckCircleOutlined, CloseCircleOutlined, LoadingOutlined, FolderOpenOutlined, } from '@ant-design/icons'; import type { UploadFile } from 'antd/es/upload/interface'; import { uploadSingleFile, completeUpload, ThreeDModelVO, } from '@/services/threeDModel'; import styles from './ModelUploadModal.less'; const { Dragger } = Upload; const { Text, Paragraph } = Typography; interface ModelUploadModalProps { visible: boolean; model: ThreeDModelVO | null; onCancel: () => void; onComplete: () => void; } interface FileUploadItem { file: File; relativePath: string; status: 'pending' | 'uploading' | 'success' | 'error'; progress: number; url?: string; error?: string; } const ModelUploadModal: React.FC = ({ visible, model, onCancel, onComplete, }) => { const [fileList, setFileList] = useState([]); const [uploading, setUploading] = useState(false); const [uploadComplete, setUploadComplete] = useState(false); const [overallProgress, setOverallProgress] = useState(0); const abortRef = useRef(false); // 重置状态 const resetState = () => { setFileList([]); setUploading(false); setUploadComplete(false); setOverallProgress(0); abortRef.current = false; }; // 处理文件夹选择 const handleFolderSelect = useCallback((e: React.ChangeEvent) => { const files = e.target.files; if (!files || files.length === 0) return; const items: FileUploadItem[] = []; for (let i = 0; i < files.length; i++) { const file = files[i]; // webkitRelativePath 包含完整的相对路径 const relativePath = (file as any).webkitRelativePath || file.name; items.push({ file, relativePath, status: 'pending', progress: 0, }); } // 检查是否包含tileset.json const hasTileset = items.some( (item) => item.relativePath.endsWith('tileset.json') && !item.relativePath.includes('/Block/') ); if (!hasTileset) { message.warning('未检测到根目录下的tileset.json文件,请确保选择了正确的3D Tiles文件夹'); } setFileList(items); setUploadComplete(false); }, []); // 开始上传 const handleUpload = async () => { if (!model?.id || fileList.length === 0) { message.error('请先选择文件'); return; } setUploading(true); abortRef.current = false; let successCount = 0; let failCount = 0; // 更新文件列表 const updateFileStatus = ( index: number, status: FileUploadItem['status'], progress: number, url?: string, error?: string ) => { setFileList((prev) => { const newList = [...prev]; newList[index] = { ...newList[index], status, progress, url, error }; return newList; }); }; // 逐个上传文件 for (let i = 0; i < fileList.length; i++) { if (abortRef.current) break; const item = fileList[i]; updateFileStatus(i, 'uploading', 0); try { const result = await uploadSingleFile( model.id, item.file, item.relativePath, (percent) => { updateFileStatus(i, 'uploading', percent); } ); if (result.resp_code === 0) { updateFileStatus(i, 'success', 100, result.datas); successCount++; } else { throw new Error(result.resp_msg || '上传失败'); } } catch (error: any) { updateFileStatus(i, 'error', 0, undefined, error.message); failCount++; } // 更新总进度 setOverallProgress(Math.round(((i + 1) / fileList.length) * 100)); } // 如果全部成功,调用完成接口 if (successCount === fileList.length) { try { await completeUpload(model.id); message.success('所有文件上传完成!'); setUploadComplete(true); } catch (error) { message.error('更新状态失败'); } } else if (failCount > 0) { message.warning(`上传完成,成功 ${successCount} 个,失败 ${failCount} 个`); } setUploading(false); }; // 取消上传 const handleAbort = () => { abortRef.current = true; message.info('正在取消上传...'); }; // 关闭弹窗 const handleClose = () => { if (uploading) { Modal.confirm({ title: '确定要取消上传吗?', content: '取消后已上传的文件将保留', onOk: () => { handleAbort(); setTimeout(() => { resetState(); onCancel(); }, 500); }, }); } else { if (uploadComplete) { onComplete(); } else { onCancel(); } resetState(); } }; // 统计信息 const stats = { total: fileList.length, success: fileList.filter((f) => f.status === 'success').length, error: fileList.filter((f) => f.status === 'error').length, pending: fileList.filter((f) => f.status === 'pending').length, uploading: fileList.filter((f) => f.status === 'uploading').length, totalSize: fileList.reduce((sum, f) => sum + f.file.size, 0), }; const formatSize = (bytes: number) => { if (bytes === 0) return '0 B'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; return ( 完成 ) : ( {!uploading && fileList.length > 0 && ( )} {uploading && ( )} ) } destroyOnClose >
{uploadComplete ? ( ) : ( <> {/* 文件选择区域 */} {fileList.length === 0 && (
)} {/* 文件列表 */} {fileList.length > 0 && ( <> {/* 统计信息 */}
|}> 共 {stats.total} 个文件 总大小 {formatSize(stats.totalSize)} {stats.success > 0 && ( 成功 {stats.success} )} {stats.error > 0 && 失败 {stats.error}} {stats.uploading > 0 && ( 上传中 {stats.uploading} )}
{/* 总进度 */} {uploading && ( )} {/* 文件列表 */}
(
{item.status === 'pending' && } {item.status === 'uploading' && ( )} {item.status === 'success' && ( )} {item.status === 'error' && ( )} {item.relativePath} {formatSize(item.file.size)}
{item.status === 'uploading' && ( )} {item.status === 'error' && ( {item.error} )}
)} style={{ maxHeight: 400, overflow: 'auto' }} />
{/* 重新选择 */} {!uploading && (
)} )} )}
); }; export default ModelUploadModal;