init
This commit is contained in:
537
nas-uploader/static/css/style.css
Normal file
537
nas-uploader/static/css/style.css
Normal file
@@ -0,0 +1,537 @@
|
||||
/* ========== Reset & Base ========== */
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
background: #f0f2f5;
|
||||
color: #333;
|
||||
line-height: 1.6;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 720px;
|
||||
margin: 0 auto;
|
||||
padding: 20px 16px 40px;
|
||||
}
|
||||
|
||||
/* ========== Header ========== */
|
||||
.header {
|
||||
text-align: center;
|
||||
padding: 24px 0 20px;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
color: #1a1a2e;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: #888;
|
||||
font-size: 14px;
|
||||
margin-top: 6px;
|
||||
}
|
||||
|
||||
/* ========== Folder Section ========== */
|
||||
.folder-section {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 20px;
|
||||
margin-bottom: 16px;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.form-group:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: #555;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.form-group select {
|
||||
width: 100%;
|
||||
height: 44px;
|
||||
padding: 0 14px;
|
||||
border: 1.5px solid #d9d9d9;
|
||||
border-radius: 8px;
|
||||
font-size: 15px;
|
||||
color: #333;
|
||||
background: #fafafa;
|
||||
appearance: none;
|
||||
-webkit-appearance: none;
|
||||
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23999' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
|
||||
background-repeat: no-repeat;
|
||||
background-position: right 14px center;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.2s, box-shadow 0.2s;
|
||||
}
|
||||
|
||||
.form-group select:focus {
|
||||
outline: none;
|
||||
border-color: #4f6ef7;
|
||||
box-shadow: 0 0 0 3px rgba(79, 110, 247, 0.1);
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
.form-group select:disabled {
|
||||
background: #f5f5f5;
|
||||
color: #bbb;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* ========== Upload Section ========== */
|
||||
.upload-section {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 20px;
|
||||
margin-bottom: 16px;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
/* Select Area */
|
||||
.select-area {
|
||||
text-align: center;
|
||||
padding: 30px 20px;
|
||||
border: 2px dashed #d9d9d9;
|
||||
border-radius: 10px;
|
||||
transition: border-color 0.2s, background 0.2s;
|
||||
}
|
||||
|
||||
.select-area:hover {
|
||||
border-color: #4f6ef7;
|
||||
background: #f8f9ff;
|
||||
}
|
||||
|
||||
.select-icon {
|
||||
font-size: 40px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.select-area p {
|
||||
color: #666;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.select-area .hint {
|
||||
color: #aaa;
|
||||
font-size: 12px;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
/* ========== Buttons ========== */
|
||||
.btn {
|
||||
display: inline-block;
|
||||
padding: 10px 24px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 15px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
}
|
||||
|
||||
.btn:active {
|
||||
transform: scale(0.97);
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: #4f6ef7;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background: #3d5ce0;
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
background: #b0bfee;
|
||||
cursor: not-allowed;
|
||||
transform: none;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: #f0f2f5;
|
||||
color: #555;
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background: #e4e6eb;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: #ff4d4f;
|
||||
color: #fff;
|
||||
padding: 4px 12px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background: #e63e40;
|
||||
}
|
||||
|
||||
.btn-small {
|
||||
padding: 4px 12px;
|
||||
font-size: 12px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.btn-large {
|
||||
width: 100%;
|
||||
padding: 14px;
|
||||
font-size: 16px;
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
/* ========== Preview Section ========== */
|
||||
.preview-section {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.preview-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 12px;
|
||||
font-size: 14px;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.preview-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
|
||||
gap: 10px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
.preview-item {
|
||||
position: relative;
|
||||
aspect-ratio: 1;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
|
||||
.preview-item img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.preview-item .remove-btn {
|
||||
position: absolute;
|
||||
top: 4px;
|
||||
right: 4px;
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
font-size: 14px;
|
||||
line-height: 22px;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
opacity: 0;
|
||||
transition: opacity 0.2s;
|
||||
}
|
||||
|
||||
.preview-item:hover .remove-btn {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.preview-item .file-name {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
color: #fff;
|
||||
font-size: 10px;
|
||||
padding: 2px 4px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
/* ========== Progress Section ========== */
|
||||
.progress-section {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.progress-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 8px;
|
||||
font-size: 14px;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.progress-percent {
|
||||
font-weight: 600;
|
||||
color: #4f6ef7;
|
||||
}
|
||||
|
||||
.progress-bar {
|
||||
width: 100%;
|
||||
height: 8px;
|
||||
background: #eee;
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.progress-fill {
|
||||
height: 100%;
|
||||
width: 0%;
|
||||
background: linear-gradient(90deg, #4f6ef7, #7c8cf8);
|
||||
border-radius: 4px;
|
||||
transition: width 0.3s ease;
|
||||
}
|
||||
|
||||
.file-progress-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
max-height: 250px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.file-progress-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 8px 10px;
|
||||
background: #fafafa;
|
||||
border-radius: 8px;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.file-progress-item .status-icon {
|
||||
font-size: 16px;
|
||||
flex-shrink: 0;
|
||||
width: 20px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.file-progress-item .file-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.file-progress-item .file-info .name {
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.file-progress-item .file-info .detail {
|
||||
font-size: 11px;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.file-progress-item .mini-progress {
|
||||
width: 60px;
|
||||
height: 4px;
|
||||
background: #eee;
|
||||
border-radius: 2px;
|
||||
overflow: hidden;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.file-progress-item .mini-progress .mini-fill {
|
||||
height: 100%;
|
||||
background: #4f6ef7;
|
||||
border-radius: 2px;
|
||||
transition: width 0.2s;
|
||||
}
|
||||
|
||||
.file-progress-item.status-success .status-icon {
|
||||
color: #52c41a;
|
||||
}
|
||||
|
||||
.file-progress-item.status-failed .status-icon {
|
||||
color: #ff4d4f;
|
||||
}
|
||||
|
||||
.file-progress-item.status-failed {
|
||||
background: #fff2f0;
|
||||
}
|
||||
|
||||
/* ========== Result Section ========== */
|
||||
.result-section {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 20px;
|
||||
margin-bottom: 16px;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.result-header {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.result-header h2 {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: #1a1a2e;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.result-summary {
|
||||
font-size: 14px;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.result-summary .success-count {
|
||||
color: #52c41a;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.result-summary .fail-count {
|
||||
color: #ff4d4f;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.result-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
|
||||
gap: 10px;
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.result-item {
|
||||
position: relative;
|
||||
aspect-ratio: 1;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
border: 2px solid transparent;
|
||||
}
|
||||
|
||||
.result-item img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.result-item.success {
|
||||
border-color: #52c41a;
|
||||
}
|
||||
|
||||
.result-item.failed {
|
||||
border-color: #ff4d4f;
|
||||
}
|
||||
|
||||
.result-item .result-name {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
background: rgba(0, 0, 0, 0.6);
|
||||
color: #fff;
|
||||
font-size: 10px;
|
||||
padding: 4px 6px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.result-item .result-status {
|
||||
position: absolute;
|
||||
top: 4px;
|
||||
right: 4px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
/* ========== Responsive ========== */
|
||||
@media (max-width: 480px) {
|
||||
.container {
|
||||
padding: 12px 10px 30px;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.folder-section,
|
||||
.upload-section,
|
||||
.result-section {
|
||||
padding: 14px;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.select-area {
|
||||
padding: 20px 14px;
|
||||
}
|
||||
|
||||
.preview-grid {
|
||||
grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.result-grid {
|
||||
grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.btn-large {
|
||||
padding: 12px;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
/* 移动端始终显示删除按钮 */
|
||||
.preview-item .remove-btn {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 481px) and (max-width: 768px) {
|
||||
.preview-grid {
|
||||
grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
|
||||
}
|
||||
}
|
||||
|
||||
/* ========== Scrollbar ========== */
|
||||
.preview-grid::-webkit-scrollbar,
|
||||
.file-progress-list::-webkit-scrollbar,
|
||||
.result-grid::-webkit-scrollbar {
|
||||
width: 4px;
|
||||
}
|
||||
|
||||
.preview-grid::-webkit-scrollbar-thumb,
|
||||
.file-progress-list::-webkit-scrollbar-thumb,
|
||||
.result-grid::-webkit-scrollbar-thumb {
|
||||
background: #ddd;
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
/* ========== Loading animation ========== */
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.5; }
|
||||
}
|
||||
|
||||
.uploading {
|
||||
animation: pulse 1.5s ease-in-out infinite;
|
||||
}
|
||||
413
nas-uploader/static/js/app.js
Normal file
413
nas-uploader/static/js/app.js
Normal file
@@ -0,0 +1,413 @@
|
||||
// ========== 写死的选项数据 ==========
|
||||
|
||||
const ALL_CAMPS = [];
|
||||
for (let i = 13; i <= 26; i++) {
|
||||
ALL_CAMPS.push(`训练营${i}期`);
|
||||
}
|
||||
|
||||
const ALL_CLASSES = [];
|
||||
for (let i = 1; i <= 25; i++) {
|
||||
ALL_CLASSES.push(`${i}班`);
|
||||
}
|
||||
|
||||
const ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
||||
const MAX_FILE_SIZE_MB = 20;
|
||||
const MAX_FILES_COUNT = 100;
|
||||
|
||||
// ========== 状态管理 ==========
|
||||
|
||||
let state = {
|
||||
selectedCamp: '',
|
||||
selectedClass: '',
|
||||
selectedFiles: [], // { file, id, previewUrl }
|
||||
uploadResults: [], // { file, id, status, filename, reason, previewUrl }
|
||||
isUploading: false,
|
||||
};
|
||||
|
||||
// ========== DOM 元素 ==========
|
||||
|
||||
const $ = (sel) => document.querySelector(sel);
|
||||
const campSelect = $('#camp-select');
|
||||
const classSelect = $('#class-select');
|
||||
const fileInput = $('#file-input');
|
||||
const uploadSection = $('.upload-section');
|
||||
const selectArea = $('#select-area');
|
||||
const previewSection = $('#preview-section');
|
||||
const previewGrid = $('#preview-grid');
|
||||
const selectedCount = $('#selected-count');
|
||||
const btnUpload = $('#btn-upload');
|
||||
const btnClearAll = $('#btn-clear-all');
|
||||
const progressSection = $('#progress-section');
|
||||
const progressText = $('#progress-text');
|
||||
const progressPercent = $('#progress-percent');
|
||||
const progressFill = $('#progress-fill');
|
||||
const fileProgressList = $('#file-progress-list');
|
||||
const resultSection = $('#result-section');
|
||||
const resultSummary = $('#result-summary');
|
||||
const resultGrid = $('#result-grid');
|
||||
const btnRetry = $('#btn-retry');
|
||||
const btnNewUpload = $('#btn-new-upload');
|
||||
|
||||
// ========== 初始化 ==========
|
||||
|
||||
function init() {
|
||||
loadFolders();
|
||||
bindEvents();
|
||||
}
|
||||
|
||||
// ========== 文件夹加载(前端写死选项) ==========
|
||||
|
||||
function loadFolders() {
|
||||
campSelect.innerHTML = '<option value="">-- 请选择训练营 --</option>';
|
||||
ALL_CAMPS.forEach(camp => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = camp;
|
||||
opt.textContent = camp;
|
||||
campSelect.appendChild(opt);
|
||||
});
|
||||
}
|
||||
|
||||
function onCampChange() {
|
||||
const campName = campSelect.value;
|
||||
state.selectedCamp = campName;
|
||||
|
||||
classSelect.innerHTML = '';
|
||||
classSelect.disabled = true;
|
||||
state.selectedClass = '';
|
||||
|
||||
if (!campName) {
|
||||
classSelect.innerHTML = '<option value="">-- 请先选择训练营 --</option>';
|
||||
return;
|
||||
}
|
||||
|
||||
classSelect.disabled = false;
|
||||
classSelect.innerHTML = '<option value="">-- 请选择班级 --</option>';
|
||||
ALL_CLASSES.forEach(cls => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = cls;
|
||||
opt.textContent = cls;
|
||||
classSelect.appendChild(opt);
|
||||
});
|
||||
}
|
||||
|
||||
function onClassChange() {
|
||||
state.selectedClass = classSelect.value;
|
||||
}
|
||||
|
||||
// ========== 文件选择 ==========
|
||||
|
||||
function onFileSelect(e) {
|
||||
const files = Array.from(e.target.files);
|
||||
if (!files.length) return;
|
||||
|
||||
const remaining = MAX_FILES_COUNT - state.selectedFiles.length;
|
||||
if (remaining <= 0) {
|
||||
alert(`最多只能选择 ${MAX_FILES_COUNT} 张图片`);
|
||||
fileInput.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const filesToAdd = files.slice(0, remaining);
|
||||
let rejected = 0;
|
||||
|
||||
filesToAdd.forEach(file => {
|
||||
if (file.size > MAX_FILE_SIZE_MB * 1024 * 1024) {
|
||||
rejected++;
|
||||
return;
|
||||
}
|
||||
|
||||
const ext = file.name.split('.').pop().toLowerCase();
|
||||
if (!ALLOWED_EXTENSIONS.includes(ext)) {
|
||||
rejected++;
|
||||
return;
|
||||
}
|
||||
|
||||
const id = Date.now() + '_' + Math.random().toString(36).substr(2, 6);
|
||||
const previewUrl = URL.createObjectURL(file);
|
||||
state.selectedFiles.push({ file, id, previewUrl });
|
||||
});
|
||||
|
||||
if (rejected > 0) {
|
||||
alert(`已跳过 ${rejected} 个文件(格式不支持或超过 20MB)`);
|
||||
}
|
||||
|
||||
fileInput.value = '';
|
||||
renderPreview();
|
||||
}
|
||||
|
||||
function removeFile(id) {
|
||||
const idx = state.selectedFiles.findIndex(f => f.id === id);
|
||||
if (idx !== -1) {
|
||||
URL.revokeObjectURL(state.selectedFiles[idx].previewUrl);
|
||||
state.selectedFiles.splice(idx, 1);
|
||||
}
|
||||
renderPreview();
|
||||
}
|
||||
|
||||
function clearAllFiles() {
|
||||
state.selectedFiles.forEach(f => URL.revokeObjectURL(f.previewUrl));
|
||||
state.selectedFiles = [];
|
||||
renderPreview();
|
||||
}
|
||||
|
||||
function renderPreview() {
|
||||
const count = state.selectedFiles.length;
|
||||
|
||||
if (count === 0) {
|
||||
previewSection.style.display = 'none';
|
||||
selectArea.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
|
||||
selectArea.style.display = 'none';
|
||||
previewSection.style.display = 'block';
|
||||
selectedCount.textContent = count;
|
||||
|
||||
btnUpload.disabled = !state.selectedCamp || !state.selectedClass || state.isUploading;
|
||||
|
||||
previewGrid.innerHTML = '';
|
||||
state.selectedFiles.forEach(item => {
|
||||
const div = document.createElement('div');
|
||||
div.className = 'preview-item';
|
||||
div.innerHTML = `
|
||||
<img src="${item.previewUrl}" alt="${item.file.name}">
|
||||
<button class="remove-btn" onclick="removeFile('${item.id}')">×</button>
|
||||
<div class="file-name">${item.file.name}</div>
|
||||
`;
|
||||
previewGrid.appendChild(div);
|
||||
});
|
||||
}
|
||||
|
||||
// ========== 上传逻辑(真实API) ==========
|
||||
|
||||
function uploadFile(file, campName, className, onProgress) {
|
||||
return new Promise((resolve) => {
|
||||
const formData = new FormData();
|
||||
formData.append('folder', campName);
|
||||
formData.append('class', className);
|
||||
formData.append('files', file);
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/upload');
|
||||
|
||||
xhr.upload.onprogress = (e) => {
|
||||
if (e.lengthComputable) {
|
||||
onProgress(Math.round((e.loaded / e.total) * 100));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = () => {
|
||||
try {
|
||||
const data = JSON.parse(xhr.responseText);
|
||||
if (xhr.status === 200 && data.uploaded && data.uploaded.length > 0) {
|
||||
resolve({ type: 'success', filename: data.uploaded[0].filename });
|
||||
} else if (data.failed && data.failed.length > 0) {
|
||||
resolve({ type: 'failed', reason: data.failed[0].reason });
|
||||
} else {
|
||||
resolve({ type: 'failed', reason: data.error || '上传失败' });
|
||||
}
|
||||
} catch (e) {
|
||||
resolve({ type: 'failed', reason: '服务器响应异常' });
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => {
|
||||
resolve({ type: 'failed', reason: '网络错误' });
|
||||
};
|
||||
|
||||
xhr.ontimeout = () => {
|
||||
resolve({ type: 'failed', reason: '请求超时' });
|
||||
};
|
||||
|
||||
xhr.timeout = 120000; // 2分钟超时
|
||||
xhr.send(formData);
|
||||
});
|
||||
}
|
||||
|
||||
async function startUpload() {
|
||||
if (!state.selectedCamp || !state.selectedClass) {
|
||||
alert('请先选择训练营和班级');
|
||||
return;
|
||||
}
|
||||
if (state.selectedFiles.length === 0) {
|
||||
alert('请先选择图片');
|
||||
return;
|
||||
}
|
||||
|
||||
state.isUploading = true;
|
||||
btnUpload.disabled = true;
|
||||
btnUpload.textContent = '上传中...';
|
||||
btnUpload.classList.add('uploading');
|
||||
|
||||
// 切换到进度视图
|
||||
previewSection.style.display = 'none';
|
||||
resultSection.style.display = 'none';
|
||||
progressSection.style.display = 'block';
|
||||
|
||||
// 初始化进度列表
|
||||
fileProgressList.innerHTML = '';
|
||||
state.selectedFiles.forEach(item => {
|
||||
const div = document.createElement('div');
|
||||
div.className = 'file-progress-item';
|
||||
div.id = `progress-${item.id}`;
|
||||
div.innerHTML = `
|
||||
<span class="status-icon">⏳</span>
|
||||
<div class="file-info">
|
||||
<div class="name">${item.file.name}</div>
|
||||
<div class="detail">等待上传...</div>
|
||||
</div>
|
||||
<div class="mini-progress"><div class="mini-fill" style="width:0%"></div></div>
|
||||
`;
|
||||
fileProgressList.appendChild(div);
|
||||
});
|
||||
|
||||
// 逐张上传
|
||||
let completed = 0;
|
||||
const total = state.selectedFiles.length;
|
||||
state.uploadResults = [];
|
||||
|
||||
for (const item of state.selectedFiles) {
|
||||
const el = $(`#progress-${item.id}`);
|
||||
const icon = el.querySelector('.status-icon');
|
||||
const detail = el.querySelector('.detail');
|
||||
const miniFill = el.querySelector('.mini-fill');
|
||||
|
||||
// 上传中
|
||||
icon.textContent = '📤';
|
||||
detail.textContent = '上传中...';
|
||||
|
||||
// 调用真实API
|
||||
const result = await uploadFile(item.file, state.selectedCamp, state.selectedClass, (percent) => {
|
||||
miniFill.style.width = `${percent}%`;
|
||||
});
|
||||
|
||||
if (result.type === 'success') {
|
||||
icon.textContent = '✅';
|
||||
detail.textContent = result.filename;
|
||||
el.classList.add('status-success');
|
||||
miniFill.style.width = '100%';
|
||||
miniFill.style.background = '#52c41a';
|
||||
state.uploadResults.push({
|
||||
file: item.file,
|
||||
id: item.id,
|
||||
previewUrl: item.previewUrl,
|
||||
status: 'success',
|
||||
filename: result.filename,
|
||||
});
|
||||
} else if (result.type === 'failed') {
|
||||
icon.textContent = '❌';
|
||||
detail.textContent = `上传失败:${result.reason}`;
|
||||
el.classList.add('status-failed');
|
||||
miniFill.style.width = '100%';
|
||||
miniFill.style.background = '#ff4d4f';
|
||||
state.uploadResults.push({
|
||||
file: item.file,
|
||||
id: item.id,
|
||||
previewUrl: item.previewUrl,
|
||||
status: 'failed',
|
||||
filename: item.file.name,
|
||||
reason: result.reason,
|
||||
});
|
||||
}
|
||||
|
||||
completed++;
|
||||
const percent = Math.round((completed / total) * 100);
|
||||
progressText.textContent = `上传中... ${completed} / ${total}`;
|
||||
progressPercent.textContent = `${percent}%`;
|
||||
progressFill.style.width = `${percent}%`;
|
||||
}
|
||||
|
||||
// 上传完成
|
||||
state.isUploading = false;
|
||||
btnUpload.textContent = '开始上传';
|
||||
btnUpload.classList.remove('uploading');
|
||||
|
||||
setTimeout(() => {
|
||||
progressSection.style.display = 'none';
|
||||
uploadSection.style.display = 'none';
|
||||
showResults();
|
||||
}, 500);
|
||||
}
|
||||
|
||||
// ========== 结果展示 ==========
|
||||
|
||||
function showResults() {
|
||||
resultSection.style.display = 'block';
|
||||
|
||||
const successList = state.uploadResults.filter(r => r.status === 'success');
|
||||
const failedList = state.uploadResults.filter(r => r.status === 'failed');
|
||||
|
||||
let summaryHtml = `共上传 <strong>${state.uploadResults.length}</strong> 张图片,`;
|
||||
summaryHtml += `<span class="success-count">成功 ${successList.length} 张</span>`;
|
||||
if (failedList.length > 0) {
|
||||
summaryHtml += `,<span class="fail-count">失败 ${failedList.length} 张</span>`;
|
||||
}
|
||||
resultSummary.innerHTML = summaryHtml;
|
||||
|
||||
btnRetry.style.display = failedList.length > 0 ? 'inline-block' : 'none';
|
||||
|
||||
resultGrid.innerHTML = '';
|
||||
state.uploadResults.forEach(item => {
|
||||
const div = document.createElement('div');
|
||||
div.className = `result-item ${item.status}`;
|
||||
div.innerHTML = `
|
||||
<img src="${item.previewUrl}" alt="${item.filename}">
|
||||
<div class="result-name">${item.status === 'success' ? item.filename : item.filename}</div>
|
||||
<span class="result-status">${item.status === 'success' ? '✅' : '❌'}</span>
|
||||
`;
|
||||
resultGrid.appendChild(div);
|
||||
});
|
||||
}
|
||||
|
||||
function retryFailed() {
|
||||
const failedItems = state.uploadResults.filter(r => r.status === 'failed');
|
||||
state.selectedFiles = failedItems.map(item => ({
|
||||
file: item.file,
|
||||
id: item.id,
|
||||
previewUrl: item.previewUrl,
|
||||
}));
|
||||
state.uploadResults = [];
|
||||
|
||||
resultSection.style.display = 'none';
|
||||
progressSection.style.display = 'none';
|
||||
uploadSection.style.display = 'block';
|
||||
renderPreview();
|
||||
}
|
||||
|
||||
function newUpload() {
|
||||
state.selectedFiles.forEach(f => URL.revokeObjectURL(f.previewUrl));
|
||||
state.selectedFiles = [];
|
||||
state.uploadResults = [];
|
||||
state.isUploading = false;
|
||||
|
||||
resultSection.style.display = 'none';
|
||||
progressSection.style.display = 'none';
|
||||
uploadSection.style.display = 'block';
|
||||
previewSection.style.display = 'none';
|
||||
selectArea.style.display = 'block';
|
||||
btnUpload.disabled = true;
|
||||
btnUpload.textContent = '开始上传';
|
||||
}
|
||||
|
||||
// ========== 事件绑定 ==========
|
||||
|
||||
function bindEvents() {
|
||||
campSelect.addEventListener('change', onCampChange);
|
||||
classSelect.addEventListener('change', onClassChange);
|
||||
fileInput.addEventListener('change', onFileSelect);
|
||||
btnUpload.addEventListener('click', startUpload);
|
||||
btnClearAll.addEventListener('click', clearAllFiles);
|
||||
btnRetry.addEventListener('click', retryFailed);
|
||||
btnNewUpload.addEventListener('click', newUpload);
|
||||
|
||||
campSelect.addEventListener('change', () => {
|
||||
if (state.selectedFiles.length > 0) renderPreview();
|
||||
});
|
||||
classSelect.addEventListener('change', () => {
|
||||
if (state.selectedFiles.length > 0) renderPreview();
|
||||
});
|
||||
}
|
||||
|
||||
// ========== 启动 ==========
|
||||
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
Reference in New Issue
Block a user