origin/apps/web/src/app/main/admin/deptsettingpage/page.tsx

304 lines
13 KiB
TypeScript
Raw Normal View History

2025-03-21 10:55:44 +08:00
import { useTusUpload } from "@web/src/hooks/useTusUpload";
import { ShareCodeGenerator } from "../sharecode/sharecodegenerator";
import { ShareCodeValidator } from "../sharecode/sharecodevalidator";
2025-03-21 17:45:35 +08:00
import { useState, useRef, useCallback } from "react";
2025-03-21 22:34:24 +08:00
import { message, Progress, Button, Tabs, DatePicker } from "antd";
2025-03-21 17:45:35 +08:00
import { UploadOutlined, DeleteOutlined, InboxOutlined } from "@ant-design/icons";
2025-03-21 22:34:24 +08:00
import {env} from '../../../../env'
2025-03-21 10:55:44 +08:00
const { TabPane } = Tabs;
2025-03-20 23:09:41 +08:00
export default function DeptSettingPage() {
2025-03-21 10:55:44 +08:00
const [uploadedFileId, setUploadedFileId] = useState<string>('');
2025-03-21 18:11:36 +08:00
const [uploadedFileName, setUploadedFileName] = useState<string>('');
2025-03-21 17:25:39 +08:00
const [fileNameMap, setFileNameMap] = useState<Record<string, string>>({});
2025-03-21 17:45:35 +08:00
const [uploadedFiles, setUploadedFiles] = useState<{id: string, name: string}[]>([]);
const [isDragging, setIsDragging] = useState(false);
2025-03-21 22:34:24 +08:00
const [expireTime, setExpireTime] = useState<Date | null>(null);
2025-03-21 17:45:35 +08:00
const dropRef = useRef<HTMLDivElement>(null);
2025-03-21 10:55:44 +08:00
// 使用您的 useTusUpload hook
const { uploadProgress, isUploading, uploadError, handleFileUpload } = useTusUpload({
2025-03-21 18:11:36 +08:00
onSuccess: (result) => {
setUploadedFileId(result.fileId);
setUploadedFileName(result.fileName);
2025-03-21 10:55:44 +08:00
message.success('文件上传成功');
},
onError: (error: Error) => {
message.error('上传失败:' + error.message);
}
});
// 处理文件上传
const handleFileSelect = async (file: File) => {
const fileKey = `file-${Date.now()}`; // 生成唯一的文件标识
2025-03-21 17:25:39 +08:00
2025-03-21 10:55:44 +08:00
handleFileUpload(
file,
2025-03-21 17:25:39 +08:00
async (result) => {
2025-03-21 10:55:44 +08:00
setUploadedFileId(result.fileId);
2025-03-21 18:11:36 +08:00
setUploadedFileName(result.fileName);
2025-03-21 17:25:39 +08:00
2025-03-21 17:45:35 +08:00
// 添加到已上传文件列表
setUploadedFiles(prev => [...prev, {id: result.fileId, name: file.name}]);
2025-03-21 17:25:39 +08:00
// 在前端保存文件名映射(用于当前会话)
setFileNameMap(prev => ({
...prev,
[result.fileId]: file.name
}));
// 上传成功后保存原始文件名到数据库
try {
2025-03-21 18:11:36 +08:00
console.log('正在保存文件名到数据库:', result.fileName, '对应文件ID:', result.fileId);
2025-03-21 17:25:39 +08:00
2025-03-21 22:34:24 +08:00
const response = await fetch(`http://${env.SERVER_IP}:${env.SERVER_PORT}/upload/filename`, {
2025-03-21 17:25:39 +08:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
fileId: result.fileId,
2025-03-21 18:11:36 +08:00
fileName: file.name
2025-03-21 17:25:39 +08:00
}),
});
2025-03-21 18:11:36 +08:00
2025-03-21 17:25:39 +08:00
const responseText = await response.text();
console.log('保存文件名响应:', response.status, responseText);
if (!response.ok) {
console.error('保存文件名失败:', responseText);
message.warning('文件名保存失败,下载时可能无法显示原始文件名');
} else {
console.log('文件名保存成功:', file.name);
}
} catch (error) {
console.error('保存文件名请求失败:', error);
message.warning('文件名保存失败,下载时可能无法显示原始文件名');
}
2025-03-21 10:55:44 +08:00
message.success('文件上传成功');
},
(error) => {
message.error('上传失败:' + error.message);
},
fileKey
);
};
2025-03-21 17:45:35 +08:00
// 处理多个文件上传
2025-03-21 22:34:24 +08:00
// const handleFilesUpload = (file: File) => {
// handleFileSelect(file);
// };
2025-03-21 17:45:35 +08:00
// 拖拽相关处理函数
const handleDragEnter = useCallback((e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(true);
}, []);
const handleDragLeave = useCallback((e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
}, []);
const handleDragOver = useCallback((e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(true);
}, []);
const handleDrop = useCallback((e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
2025-03-21 22:34:24 +08:00
handleFileSelect(e.dataTransfer.files[0]);
2025-03-21 17:45:35 +08:00
}, []);
2025-03-21 10:55:44 +08:00
// 处理分享码生成成功
const handleShareSuccess = (code: string) => {
message.success('分享码生成成功:' + code);
// 可以在这里添加其他逻辑,比如保存到历史记录
};
// 处理分享码验证成功
2025-03-21 18:11:36 +08:00
const handleValidSuccess = async (fileId: string, fileName: string) => {
2025-03-21 10:55:44 +08:00
try {
2025-03-21 18:11:36 +08:00
// 构建下载URL包含文件名参数
const downloadUrl = `/upload/download/${fileId}?fileName=${encodeURIComponent(fileName)}`;
const response = await fetch(downloadUrl);
2025-03-21 10:55:44 +08:00
if (!response.ok) {
throw new Error('文件下载失败');
}
2025-03-21 18:11:36 +08:00
2025-03-21 10:55:44 +08:00
// 创建下载链接
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
2025-03-21 17:25:39 +08:00
2025-03-21 18:11:36 +08:00
// 直接使用传入的 fileName
link.download = fileName;
2025-03-21 17:25:39 +08:00
// 触发下载
2025-03-21 10:55:44 +08:00
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
2025-03-21 18:11:36 +08:00
2025-03-21 10:55:44 +08:00
message.success('文件下载开始');
} catch (error) {
console.error('下载失败:', error);
message.error('文件下载失败');
}
};
2025-03-21 22:34:24 +08:00
2025-03-20 23:09:41 +08:00
return (
2025-03-21 10:55:44 +08:00
<div style={{ maxWidth: '800px', margin: '0 auto', padding: '20px' }}>
<h2></h2>
<Tabs defaultActiveKey="upload">
<TabPane tab="上传分享" key="upload">
{/* 文件上传区域 */}
<div style={{ marginBottom: '40px' }}>
<h3></h3>
2025-03-21 17:45:35 +08:00
<div
ref={dropRef}
onDragEnter={handleDragEnter}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
style={{
padding: '20px',
border: `2px dashed ${isDragging ? '#1890ff' : '#d9d9d9'}`,
borderRadius: '8px',
textAlign: 'center',
backgroundColor: isDragging ? 'rgba(24, 144, 255, 0.05)' : 'transparent',
transition: 'all 0.3s',
marginBottom: '20px'
}}
>
<InboxOutlined style={{ fontSize: '48px', color: isDragging ? '#1890ff' : '#d9d9d9' }} />
<p></p>
2025-03-21 22:34:24 +08:00
<p style={{ fontSize: '12px', color: '#888' }}></p>
2025-03-21 17:45:35 +08:00
2025-03-21 10:55:44 +08:00
<input
type="file"
id="file-input"
style={{ display: 'none' }}
onChange={(e) => {
2025-03-21 22:34:24 +08:00
const file = e.target.files[0];
if (file) {
handleFileSelect(file);
2025-03-21 10:55:44 +08:00
}
}}
disabled={isUploading}
2025-03-21 22:34:24 +08:00
2025-03-21 10:55:44 +08:00
/>
<label
htmlFor="file-input"
style={{
display: 'inline-block',
2025-03-21 17:45:35 +08:00
padding: '8px 16px',
2025-03-21 10:55:44 +08:00
backgroundColor: '#1890ff',
color: 'white',
2025-03-21 17:45:35 +08:00
borderRadius: '4px',
2025-03-21 10:55:44 +08:00
cursor: 'pointer',
2025-03-21 17:45:35 +08:00
marginTop: '10px'
2025-03-21 10:55:44 +08:00
}}
>
<UploadOutlined />
</label>
</div>
2025-03-21 17:45:35 +08:00
{/* 已上传文件列表 */}
{uploadedFiles.length > 0 && (
<div style={{
border: '1px solid #f0f0f0',
borderRadius: '4px',
overflow: 'hidden'
}}>
{uploadedFiles.map((file, index) => (
<div key={file.id} style={{
display: 'flex',
alignItems: 'center',
padding: '10px 15px',
borderBottom: index < uploadedFiles.length - 1 ? '1px solid #f0f0f0' : 'none',
backgroundColor: index % 2 === 0 ? '#fafafa' : 'white'
}}>
<div style={{
display: 'flex',
alignItems: 'center',
flex: 1
}}>
<div style={{
width: '20px',
height: '20px',
borderRadius: '50%',
backgroundColor: '#52c41a',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
marginRight: '10px'
}}>
<span style={{ color: 'white', fontSize: '12px' }}></span>
</div>
<span>{file.name}</span>
</div>
<Button
type="text"
icon={<DeleteOutlined style={{ color: '#ff4d4f' }} />}
/>
</div>
))}
</div>
)}
{isUploading && (
<div style={{ marginTop: '20px' }}>
<Progress
percent={Math.round(Object.values(uploadProgress)[0] || 0)}
status="active"
/>
</div>
)}
{uploadError && (
<div style={{ color: '#ff4d4f', marginTop: '10px' }}>
{uploadError}
</div>
)}
2025-03-21 10:55:44 +08:00
</div>
{/* 生成分享码区域 */}
{uploadedFileId && (
<div style={{ marginBottom: '40px' }}>
<h3></h3>
2025-03-21 22:34:24 +08:00
2025-03-21 10:55:44 +08:00
<ShareCodeGenerator
fileId={uploadedFileId}
onSuccess={handleShareSuccess}
/>
</div>
)}
</TabPane>
{/* 使用分享码区域 */}
<TabPane tab="下载文件" key="download">
<div>
<h3>使</h3>
<ShareCodeValidator
onValidSuccess={handleValidSuccess}
/>
</div>
</TabPane>
</Tabs>
2025-03-20 23:09:41 +08:00
</div>
);
}