408 lines
12 KiB
TypeScript
408 lines
12 KiB
TypeScript
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<ModelUploadModalProps> = ({
|
||
|
|
visible,
|
||
|
|
model,
|
||
|
|
onCancel,
|
||
|
|
onComplete,
|
||
|
|
}) => {
|
||
|
|
const [fileList, setFileList] = useState<FileUploadItem[]>([]);
|
||
|
|
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<HTMLInputElement>) => {
|
||
|
|
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 (
|
||
|
|
<Modal
|
||
|
|
title={`上传模型文件 - ${model?.modelName || ''}`}
|
||
|
|
open={visible}
|
||
|
|
onCancel={handleClose}
|
||
|
|
width={800}
|
||
|
|
footer={
|
||
|
|
uploadComplete ? (
|
||
|
|
<Button type="primary" onClick={handleClose}>
|
||
|
|
完成
|
||
|
|
</Button>
|
||
|
|
) : (
|
||
|
|
<Space>
|
||
|
|
<Button onClick={handleClose}>{uploading ? '取消' : '关闭'}</Button>
|
||
|
|
{!uploading && fileList.length > 0 && (
|
||
|
|
<Button type="primary" onClick={handleUpload}>
|
||
|
|
开始上传
|
||
|
|
</Button>
|
||
|
|
)}
|
||
|
|
{uploading && (
|
||
|
|
<Button danger onClick={handleAbort}>
|
||
|
|
停止上传
|
||
|
|
</Button>
|
||
|
|
)}
|
||
|
|
</Space>
|
||
|
|
)
|
||
|
|
}
|
||
|
|
destroyOnClose
|
||
|
|
>
|
||
|
|
<div className={styles.uploadContainer}>
|
||
|
|
{uploadComplete ? (
|
||
|
|
<Result
|
||
|
|
status="success"
|
||
|
|
title="上传完成"
|
||
|
|
subTitle={`成功上传 ${stats.success} 个文件,共 ${formatSize(stats.totalSize)}`}
|
||
|
|
/>
|
||
|
|
) : (
|
||
|
|
<>
|
||
|
|
{/* 文件选择区域 */}
|
||
|
|
{fileList.length === 0 && (
|
||
|
|
<div className={styles.selectArea}>
|
||
|
|
<Alert
|
||
|
|
message="请选择3D Tiles模型文件夹"
|
||
|
|
description="选择大疆建图输出的 terra_b3dms 文件夹,包含 tileset.json 和所有 .b3dm 文件"
|
||
|
|
type="info"
|
||
|
|
showIcon
|
||
|
|
style={{ marginBottom: 16 }}
|
||
|
|
/>
|
||
|
|
<div className={styles.folderSelector}>
|
||
|
|
<input
|
||
|
|
type="file"
|
||
|
|
id="folderInput"
|
||
|
|
/* @ts-ignore */
|
||
|
|
webkitdirectory=""
|
||
|
|
directory=""
|
||
|
|
multiple
|
||
|
|
onChange={handleFolderSelect}
|
||
|
|
style={{ display: 'none' }}
|
||
|
|
/>
|
||
|
|
<label htmlFor="folderInput" className={styles.folderLabel}>
|
||
|
|
<FolderOpenOutlined style={{ fontSize: 48, color: '#1890ff' }} />
|
||
|
|
<div className={styles.labelText}>
|
||
|
|
<Text strong>点击选择文件夹</Text>
|
||
|
|
<Text type="secondary">或将文件夹拖放到此处</Text>
|
||
|
|
</div>
|
||
|
|
</label>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
|
||
|
|
{/* 文件列表 */}
|
||
|
|
{fileList.length > 0 && (
|
||
|
|
<>
|
||
|
|
{/* 统计信息 */}
|
||
|
|
<div className={styles.statsBar}>
|
||
|
|
<Space split={<span style={{ color: '#d9d9d9' }}>|</span>}>
|
||
|
|
<Text>共 {stats.total} 个文件</Text>
|
||
|
|
<Text>总大小 {formatSize(stats.totalSize)}</Text>
|
||
|
|
{stats.success > 0 && (
|
||
|
|
<Text type="success">成功 {stats.success}</Text>
|
||
|
|
)}
|
||
|
|
{stats.error > 0 && <Text type="danger">失败 {stats.error}</Text>}
|
||
|
|
{stats.uploading > 0 && (
|
||
|
|
<Text type="warning">上传中 {stats.uploading}</Text>
|
||
|
|
)}
|
||
|
|
</Space>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{/* 总进度 */}
|
||
|
|
{uploading && (
|
||
|
|
<Progress
|
||
|
|
percent={overallProgress}
|
||
|
|
status="active"
|
||
|
|
strokeColor={{
|
||
|
|
'0%': '#108ee9',
|
||
|
|
'100%': '#87d068',
|
||
|
|
}}
|
||
|
|
style={{ marginBottom: 16 }}
|
||
|
|
/>
|
||
|
|
)}
|
||
|
|
|
||
|
|
{/* 文件列表 */}
|
||
|
|
<div className={styles.fileList}>
|
||
|
|
<List
|
||
|
|
size="small"
|
||
|
|
dataSource={fileList}
|
||
|
|
renderItem={(item, index) => (
|
||
|
|
<List.Item
|
||
|
|
className={`${styles.fileItem} ${styles[item.status]}`}
|
||
|
|
>
|
||
|
|
<div className={styles.fileInfo}>
|
||
|
|
<Space>
|
||
|
|
{item.status === 'pending' && <FileOutlined />}
|
||
|
|
{item.status === 'uploading' && (
|
||
|
|
<LoadingOutlined spin />
|
||
|
|
)}
|
||
|
|
{item.status === 'success' && (
|
||
|
|
<CheckCircleOutlined style={{ color: '#52c41a' }} />
|
||
|
|
)}
|
||
|
|
{item.status === 'error' && (
|
||
|
|
<CloseCircleOutlined style={{ color: '#ff4d4f' }} />
|
||
|
|
)}
|
||
|
|
<Text
|
||
|
|
ellipsis={{ tooltip: item.relativePath }}
|
||
|
|
style={{ maxWidth: 400 }}
|
||
|
|
>
|
||
|
|
{item.relativePath}
|
||
|
|
</Text>
|
||
|
|
</Space>
|
||
|
|
<Text type="secondary" style={{ fontSize: 12 }}>
|
||
|
|
{formatSize(item.file.size)}
|
||
|
|
</Text>
|
||
|
|
</div>
|
||
|
|
{item.status === 'uploading' && (
|
||
|
|
<Progress
|
||
|
|
percent={item.progress}
|
||
|
|
size="small"
|
||
|
|
style={{ width: 100 }}
|
||
|
|
/>
|
||
|
|
)}
|
||
|
|
{item.status === 'error' && (
|
||
|
|
<Text type="danger" style={{ fontSize: 12 }}>
|
||
|
|
{item.error}
|
||
|
|
</Text>
|
||
|
|
)}
|
||
|
|
</List.Item>
|
||
|
|
)}
|
||
|
|
style={{ maxHeight: 400, overflow: 'auto' }}
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{/* 重新选择 */}
|
||
|
|
{!uploading && (
|
||
|
|
<div style={{ marginTop: 16, textAlign: 'center' }}>
|
||
|
|
<input
|
||
|
|
type="file"
|
||
|
|
id="folderInputReset"
|
||
|
|
/* @ts-ignore */
|
||
|
|
webkitdirectory=""
|
||
|
|
directory=""
|
||
|
|
multiple
|
||
|
|
onChange={handleFolderSelect}
|
||
|
|
style={{ display: 'none' }}
|
||
|
|
/>
|
||
|
|
<label htmlFor="folderInputReset">
|
||
|
|
<Button type="link">重新选择文件夹</Button>
|
||
|
|
</label>
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
</Modal>
|
||
|
|
);
|
||
|
|
};
|
||
|
|
|
||
|
|
export default ModelUploadModal;
|