This commit is contained in:
2026-06-12 14:27:19 +08:00
commit d8dccfc04c
12 changed files with 1796 additions and 0 deletions

View File

@@ -0,0 +1,742 @@
/* ── 基础变量 ───────────────────────────────────────────── */
:root {
--bg: #faf9f7;
--bg-card: #ffffff;
--bg-hover: #f5f4f2;
--ink: #1a1816;
--muted: #7a756e;
--border: #e8e5e0;
--border-focus: #c4a882;
--accent: #b45309;
--accent-hover: #92400e;
--accent-light: #fef3c7;
--success: #0f766e;
--success-bg: #f0fdfa;
--error: #dc2626;
--error-bg: #fef2f2;
--radius: 10px;
--radius-sm: 6px;
--shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
--shadow-lg: 0 10px 40px rgba(0,0,0,0.06), 0 2px 10px rgba(0,0,0,0.04);
--font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
--font-mono: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", monospace;
--transition: 0.2s ease;
}
/* ── 重置 ───────────────────────────────────────────────── */
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
font-family: var(--font);
color: var(--ink);
background: var(--bg);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem 1rem;
line-height: 1.6;
}
/* ── 容器 ───────────────────────────────────────────────── */
.container {
width: 100%;
max-width: 520px;
}
/* ── 头部 ───────────────────────────────────────────────── */
.header {
text-align: center;
margin-bottom: 2rem;
}
.header-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 56px;
height: 56px;
border-radius: 14px;
background: linear-gradient(135deg, var(--accent), #d97706);
color: white;
margin-bottom: 1rem;
box-shadow: 0 4px 14px rgba(180, 83, 9, 0.25);
}
.header h1 {
font-size: 1.5rem;
font-weight: 700;
letter-spacing: -0.02em;
color: var(--ink);
margin-bottom: 0.25rem;
}
.subtitle {
font-size: 0.9rem;
color: var(--muted);
}
/* ── 表单卡片 ───────────────────────────────────────────── */
.form {
background: var(--bg-card);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 1.75rem;
box-shadow: var(--shadow);
}
.form-group {
margin-bottom: 1.25rem;
}
.form-group:last-of-type {
margin-bottom: 1rem;
}
.form-group label {
display: flex;
align-items: center;
gap: 6px;
font-size: 0.85rem;
font-weight: 600;
color: var(--ink);
margin-bottom: 0.5rem;
}
.form-group label svg {
color: var(--muted);
}
.form-hint {
display: block;
font-size: 0.75rem;
color: var(--muted);
margin-top: 0.35rem;
}
/* ── 输入框 ─────────────────────────────────────────────── */
input[type="text"] {
width: 100%;
padding: 0.65rem 0.9rem;
font-size: 0.9rem;
font-family: var(--font);
color: var(--ink);
background: var(--bg);
border: 1.5px solid var(--border);
border-radius: var(--radius-sm);
outline: none;
transition: border-color var(--transition), box-shadow var(--transition);
}
input[type="text"]::placeholder {
color: #b5b0a8;
}
input[type="text"]:focus {
border-color: var(--border-focus);
box-shadow: 0 0 0 3px rgba(196, 168, 130, 0.15);
}
input[type="text"].input-error {
border-color: var(--error);
box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1);
}
/* ── 验证码行 ───────────────────────────────────────────── */
.captcha-row {
display: flex;
gap: 10px;
align-items: stretch;
}
.captcha-row input {
flex: 1;
min-width: 0;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.captcha-img {
height: 42px;
width: 140px;
border-radius: var(--radius-sm);
border: 1.5px solid var(--border);
cursor: pointer;
object-fit: cover;
transition: opacity var(--transition);
flex-shrink: 0;
}
.captcha-img:hover {
opacity: 0.85;
}
.btn-icon {
display: flex;
align-items: center;
justify-content: center;
width: 42px;
height: 42px;
border: 1.5px solid var(--border);
border-radius: var(--radius-sm);
background: var(--bg);
color: var(--muted);
cursor: pointer;
transition: all var(--transition);
flex-shrink: 0;
}
.btn-icon:hover {
border-color: var(--border-focus);
color: var(--accent);
background: var(--accent-light);
}
/* ── 拖拽区 ─────────────────────────────────────────────── */
.dropzone {
border: 2px dashed var(--border);
border-radius: var(--radius);
padding: 2rem 1.5rem;
text-align: center;
cursor: pointer;
transition: all var(--transition);
background: var(--bg);
}
.dropzone:hover,
.dropzone.dragover {
border-color: var(--accent);
background: var(--accent-light);
}
.dropzone.dragover {
transform: scale(1.01);
}
.dropzone-content {
pointer-events: none;
}
.dropzone-icon {
color: var(--muted);
margin-bottom: 0.5rem;
transition: color var(--transition);
}
.dropzone:hover .dropzone-icon,
.dropzone.dragover .dropzone-icon {
color: var(--accent);
}
.dropzone-text {
font-size: 0.9rem;
color: var(--muted);
margin-bottom: 0.35rem;
}
.dropzone-link {
color: var(--accent);
font-weight: 600;
text-decoration: underline;
text-decoration-thickness: 1.5px;
text-underline-offset: 2px;
}
.dropzone-hint {
font-size: 0.75rem;
color: #b5b0a8;
line-height: 1.5;
}
/* ── 文件列表 ───────────────────────────────────────────── */
.file-list {
margin-bottom: 1rem;
}
.file-item {
display: flex;
align-items: center;
gap: 10px;
padding: 0.6rem 0.85rem;
background: var(--bg);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
margin-bottom: 6px;
animation: slideIn 0.25s ease;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(-8px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.file-item-icon {
flex-shrink: 0;
color: var(--muted);
}
.file-item-info {
flex: 1;
min-width: 0;
}
.file-item-name {
font-size: 0.85rem;
font-weight: 500;
color: var(--ink);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.file-item-meta {
font-size: 0.72rem;
color: var(--muted);
margin-top: 1px;
}
.file-item-status {
flex-shrink: 0;
font-size: 0.78rem;
font-weight: 600;
}
.file-item-status.status-waiting {
color: var(--muted);
}
.file-item-status.status-uploading {
color: var(--accent);
}
.file-item-status.status-done {
color: var(--success);
}
.file-item-status.status-error {
color: var(--error);
}
.file-item-remove {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
border: none;
border-radius: 50%;
background: transparent;
color: var(--muted);
cursor: pointer;
transition: all var(--transition);
}
.file-item-remove:hover {
background: var(--error-bg);
color: var(--error);
}
/* 进度条 */
.progress-bar {
width: 100%;
height: 3px;
background: var(--border);
border-radius: 2px;
margin-top: 6px;
overflow: hidden;
}
.progress-bar-fill {
height: 100%;
background: linear-gradient(90deg, var(--accent), #d97706);
border-radius: 2px;
transition: width 0.3s ease;
width: 0%;
}
/* ── 按钮 ───────────────────────────────────────────────── */
.form-actions {
display: flex;
gap: 10px;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 0.7rem 1.5rem;
font-size: 0.9rem;
font-weight: 600;
font-family: var(--font);
border: none;
border-radius: var(--radius-sm);
cursor: pointer;
transition: all var(--transition);
white-space: nowrap;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-primary {
flex: 1;
background: linear-gradient(135deg, var(--accent), #d97706);
color: white;
box-shadow: 0 2px 8px rgba(180, 83, 9, 0.2);
}
.btn-primary:hover:not(:disabled) {
box-shadow: 0 4px 14px rgba(180, 83, 9, 0.35);
transform: translateY(-1px);
}
.btn-primary:active:not(:disabled) {
transform: translateY(0);
}
.btn-secondary {
background: var(--bg);
color: var(--muted);
border: 1.5px solid var(--border);
}
.btn-secondary:hover {
border-color: var(--border-focus);
color: var(--ink);
background: var(--bg-hover);
}
/* ── 结果面板 ───────────────────────────────────────────── */
.result-panel {
margin-top: 1.25rem;
animation: slideIn 0.3s ease;
}
.result-card {
background: var(--bg-card);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 1.25rem;
box-shadow: var(--shadow);
}
.result-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 0.75rem;
}
.result-icon {
display: flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
border-radius: 50%;
flex-shrink: 0;
}
.result-icon.icon-success {
background: var(--success-bg);
color: var(--success);
}
.result-icon.icon-error {
background: var(--error-bg);
color: var(--error);
}
.result-title {
font-size: 1rem;
font-weight: 700;
}
.result-title.text-success {
color: var(--success);
}
.result-title.text-error {
color: var(--error);
}
.result-detail {
font-size: 0.85rem;
color: var(--muted);
line-height: 1.7;
}
.result-detail strong {
color: var(--ink);
font-weight: 600;
}
.result-files {
margin-top: 0.75rem;
padding-top: 0.75rem;
border-top: 1px solid var(--border);
}
.result-file-item {
display: flex;
align-items: center;
gap: 8px;
padding: 0.35rem 0;
font-size: 0.82rem;
}
.result-file-item svg {
flex-shrink: 0;
}
.result-file-item.file-success {
color: var(--success);
}
.result-file-item.file-error {
color: var(--error);
}
/* ── 弹窗 ───────────────────────────────────────────────── */
.modal-overlay {
display: none;
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.4);
backdrop-filter: blur(4px);
z-index: 1000;
align-items: center;
justify-content: center;
padding: 1rem;
}
.modal-overlay.active {
display: flex;
animation: fadeIn 0.2s ease;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.modal {
background: var(--bg-card);
border-radius: 14px;
width: 100%;
max-width: 380px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15), 0 4px 20px rgba(0, 0, 0, 0.08);
animation: modalSlideUp 0.25s ease;
}
@keyframes modalSlideUp {
from {
opacity: 0;
transform: translateY(20px) scale(0.97);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
.modal-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1.1rem 1.25rem 0;
}
.modal-header h3 {
font-size: 1.05rem;
font-weight: 700;
color: var(--ink);
}
.modal-close {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border: none;
border-radius: 8px;
background: transparent;
color: var(--muted);
cursor: pointer;
transition: all var(--transition);
}
.modal-close:hover {
background: var(--bg-hover);
color: var(--ink);
}
.modal-body {
padding: 1rem 1.25rem;
}
.modal-desc {
font-size: 0.85rem;
color: var(--muted);
margin-bottom: 1rem;
}
.modal-captcha-row {
display: flex;
gap: 10px;
align-items: center;
margin-bottom: 0.75rem;
}
.modal-captcha-img {
height: 46px;
flex: 1;
border-radius: var(--radius-sm);
border: 1.5px solid var(--border);
cursor: pointer;
object-fit: contain;
background: var(--bg);
transition: opacity var(--transition);
}
.modal-captcha-img:hover {
opacity: 0.85;
}
.modal-captcha-input {
width: 100%;
padding: 0.7rem 0.9rem;
font-size: 1.1rem;
font-family: var(--font);
color: var(--ink);
background: var(--bg);
border: 1.5px solid var(--border);
border-radius: var(--radius-sm);
outline: none;
letter-spacing: 0.2em;
text-transform: uppercase;
text-align: center;
transition: border-color var(--transition), box-shadow var(--transition);
}
.modal-captcha-input::placeholder {
letter-spacing: normal;
text-transform: none;
font-size: 0.85rem;
color: #b5b0a8;
}
.modal-captcha-input:focus {
border-color: var(--border-focus);
box-shadow: 0 0 0 3px rgba(196, 168, 130, 0.15);
}
.modal-error {
font-size: 0.8rem;
color: var(--error);
min-height: 1.2rem;
margin-top: 0.4rem;
}
.modal-footer {
display: flex;
gap: 10px;
padding: 0 1.25rem 1.25rem;
}
.modal-footer .btn {
flex: 1;
padding: 0.65rem 1rem;
}
/* ── 页脚 ───────────────────────────────────────────────── */
.footer {
margin-top: 2rem;
text-align: center;
font-size: 0.78rem;
color: #b5b0a8;
}
/* ── 响应式 ─────────────────────────────────────────────── */
@media (max-width: 560px) {
body {
padding: 1rem 0.75rem;
}
.header {
margin-bottom: 1rem;
}
.header-icon {
width: 40px;
height: 40px;
border-radius: 10px;
margin-bottom: 0.5rem;
}
.header-icon svg {
width: 22px;
height: 22px;
}
.header h1 {
font-size: 1.2rem;
}
.subtitle {
font-size: 0.8rem;
}
.form {
padding: 1.25rem;
}
.dropzone {
padding: 1.5rem 1rem;
}
.btn-icon {
width: 38px;
height: 38px;
}
.form-actions {
flex-direction: column;
}
}
/* ── 工具类 ─────────────────────────────────────────────── */
.hidden {
display: none !important;
}
.shake {
animation: shake 0.4s ease;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
20%, 60% { transform: translateX(-6px); }
40%, 80% { transform: translateX(6px); }
}

View File

@@ -0,0 +1,493 @@
/**
* 文件上传网站 - 前端交互逻辑
*/
(function () {
'use strict';
// ── 常量 ────────────────────────────────────────────────
const MAX_FILE_SIZE = 20 * 1024 * 1024; // 20MB
const MAX_FILES = 9;
const ALLOWED_EXTENSIONS = ['.txt', '.doc', '.docx', '.pdf', '.rtf', '.odt', '.html', '.csv', '.xls', '.xlsx'];
// ── DOM 元素 ────────────────────────────────────────────
const $name = document.getElementById('name');
const $dropzone = document.getElementById('dropzone');
const $fileInput = document.getElementById('fileInput');
const $fileList = document.getElementById('fileList');
const $uploadBtn = document.getElementById('uploadBtn');
const $clearBtn = document.getElementById('clearBtn');
const $uploadForm = document.getElementById('uploadForm');
const $resultPanel = document.getElementById('resultPanel');
// 弹窗相关
const $modal = document.getElementById('captchaModal');
const $modalClose = document.getElementById('modalClose');
const $modalCancel = document.getElementById('modalCancel');
const $modalConfirm = document.getElementById('modalConfirm');
const $captchaImg = document.getElementById('captchaImg');
const $refreshCaptcha = document.getElementById('refreshCaptcha');
const $captchaInput = document.getElementById('captchaInput');
const $captchaError = document.getElementById('captchaError');
// ── 状态 ────────────────────────────────────────────────
let selectedFiles = []; // { file, id, status, error }
let fileIdCounter = 0;
let isUploading = false;
// ── 工具函数 ────────────────────────────────────────────
function formatSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
}
function getFileExt(filename) {
const idx = filename.lastIndexOf('.');
return idx >= 0 ? filename.substring(idx).toLowerCase() : '';
}
function validateFile(file) {
const ext = getFileExt(file.name);
if (!ALLOWED_EXTENSIONS.includes(ext)) {
return '不支持的文件格式';
}
if (file.size > MAX_FILE_SIZE) {
return '文件超过 20MB 限制';
}
if (file.size === 0) {
return '文件为空';
}
return null;
}
// ── 弹窗控制 ────────────────────────────────────────────
function openModal() {
$captchaInput.value = '';
$captchaError.textContent = '';
refreshCaptcha();
$modal.classList.add('active');
setTimeout(() => $captchaInput.focus(), 200);
}
function closeModal() {
$modal.classList.remove('active');
}
function refreshCaptcha() {
$captchaImg.src = '/api/captcha?' + Date.now();
$captchaInput.value = '';
$captchaError.textContent = '';
}
// 弹窗事件绑定
$modalClose.addEventListener('click', closeModal);
$modalCancel.addEventListener('click', closeModal);
$captchaImg.addEventListener('click', refreshCaptcha);
$refreshCaptcha.addEventListener('click', refreshCaptcha);
// 点击遮罩层关闭
$modal.addEventListener('click', function (e) {
if (e.target === $modal) closeModal();
});
// ESC 关闭弹窗
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && $modal.classList.contains('active')) {
closeModal();
}
});
// 弹窗内回车 = 确认
$captchaInput.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
e.preventDefault();
$modalConfirm.click();
}
});
// 确认上传按钮
$modalConfirm.addEventListener('click', function () {
const captchaVal = $captchaInput.value.trim();
if (!captchaVal) {
$captchaError.textContent = '请输入验证码';
shakeElement($captchaInput);
return;
}
const nameVal = $name.value.trim();
const validFiles = selectedFiles.filter(f => f.status !== 'error');
closeModal();
startUpload(nameVal, captchaVal, validFiles);
});
// ── 文件选择(点击) ────────────────────────────────────
$dropzone.addEventListener('click', function () {
if (!isUploading) {
$fileInput.click();
}
});
$fileInput.addEventListener('change', function () {
addFiles(Array.from($fileInput.files));
$fileInput.value = ''; // 清空以便重复选择同名文件
});
// ── 文件拖拽 ────────────────────────────────────────────
$dropzone.addEventListener('dragover', function (e) {
e.preventDefault();
if (!isUploading) {
$dropzone.classList.add('dragover');
}
});
$dropzone.addEventListener('dragleave', function (e) {
e.preventDefault();
$dropzone.classList.remove('dragover');
});
$dropzone.addEventListener('drop', function (e) {
e.preventDefault();
$dropzone.classList.remove('dragover');
if (!isUploading) {
addFiles(Array.from(e.dataTransfer.files));
}
});
// ── 添加文件到列表 ──────────────────────────────────────
function addFiles(files) {
if (isUploading) return;
for (const file of files) {
if (selectedFiles.length >= MAX_FILES) {
showToast(`单次最多上传 ${MAX_FILES} 个文件`);
break;
}
const error = validateFile(file);
const item = {
file: file,
id: ++fileIdCounter,
status: error ? 'error' : 'waiting',
error: error,
progress: 0
};
selectedFiles.push(item);
}
renderFileList();
updateButtonState();
}
// ── 渲染文件列表 ────────────────────────────────────────
function renderFileList() {
if (selectedFiles.length === 0) {
$fileList.innerHTML = '';
return;
}
$fileList.innerHTML = selectedFiles.map(item => {
const statusText = {
waiting: '等待中',
uploading: `${item.progress}%`,
done: '已完成',
error: item.error || '失败'
}[item.status];
const statusClass = {
waiting: 'status-waiting',
uploading: 'status-uploading',
done: 'status-done',
error: 'status-error'
}[item.status];
const iconSvg = item.status === 'done'
? '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#0f766e" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>'
: item.status === 'error'
? '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#dc2626" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>'
: '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#7a756e" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>';
const progressBar = item.status === 'uploading'
? `<div class="progress-bar"><div class="progress-bar-fill" style="width:${item.progress}%"></div></div>`
: '';
const removeBtn = !isUploading
? `<button type="button" class="file-item-remove" data-id="${item.id}" title="移除">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
</button>`
: '';
return `
<div class="file-item" data-id="${item.id}">
<span class="file-item-icon">${iconSvg}</span>
<div class="file-item-info">
<div class="file-item-name" title="${item.file.name}">${item.file.name}</div>
<div class="file-item-meta">${formatSize(item.file.size)}</div>
${progressBar}
</div>
<span class="file-item-status ${statusClass}">${statusText}</span>
${removeBtn}
</div>
`;
}).join('');
// 绑定移除按钮
$fileList.querySelectorAll('.file-item-remove').forEach(btn => {
btn.addEventListener('click', function (e) {
e.stopPropagation();
const id = parseInt(this.dataset.id);
selectedFiles = selectedFiles.filter(f => f.id !== id);
renderFileList();
updateButtonState();
});
});
}
// ── 按钮状态 ────────────────────────────────────────────
function updateButtonState() {
const nameValid = $name.value.trim().length >= 2;
const hasFiles = selectedFiles.some(f => f.status !== 'error');
$uploadBtn.disabled = !nameValid || !hasFiles || isUploading;
}
$name.addEventListener('input', updateButtonState);
// ── 清空列表 ────────────────────────────────────────────
$clearBtn.addEventListener('click', function () {
if (isUploading) return;
selectedFiles = [];
renderFileList();
updateButtonState();
$resultPanel.innerHTML = '';
});
// ── 点击"开始上传" → 打开验证码弹窗 ─────────────────────
$uploadForm.addEventListener('submit', function (e) {
e.preventDefault();
if (isUploading) return;
// 前端校验姓名
const nameVal = $name.value.trim();
if (nameVal.length < 2) {
shakeElement($name);
return;
}
// 校验文件
const validFiles = selectedFiles.filter(f => f.status !== 'error');
if (validFiles.length === 0) {
showToast('没有可上传的文件');
return;
}
// 打开验证码弹窗
openModal();
});
// ── 执行上传 ────────────────────────────────────────────
function startUpload(name, captcha, files) {
isUploading = true;
$uploadBtn.disabled = true;
$clearBtn.disabled = true;
$resultPanel.innerHTML = '';
// 重置文件状态
files.forEach(f => {
f.status = 'uploading';
f.progress = 0;
});
renderFileList();
const formData = new FormData();
formData.append('name', name);
formData.append('captcha', captcha);
files.forEach(f => formData.append('files', f.file));
const xhr = new XMLHttpRequest();
// 进度事件
xhr.upload.addEventListener('progress', function (e) {
if (e.lengthComputable) {
const pct = Math.round((e.loaded / e.total) * 100);
files.forEach(f => {
f.progress = pct;
});
renderFileList();
}
});
// 完成事件
xhr.addEventListener('load', function () {
isUploading = false;
$clearBtn.disabled = false;
if (xhr.status === 200) {
const data = JSON.parse(xhr.responseText);
handleUploadSuccess(data, files);
} else {
let errMsg = '上传失败,请重试';
try {
const errData = JSON.parse(xhr.responseText);
errMsg = errData.error || errMsg;
} catch (_) { /* ignore */ }
// 如果是验证码错误,重新弹窗让用户输入
if (errMsg.includes('验证码')) {
files.forEach(f => {
f.status = 'waiting';
f.progress = 0;
});
renderFileList();
updateButtonState();
showToast(errMsg);
// 延迟一下再弹窗,让用户看到 toast
setTimeout(openModal, 600);
} else {
handleUploadError(errMsg, files);
}
}
updateButtonState();
});
xhr.addEventListener('error', function () {
isUploading = false;
$clearBtn.disabled = false;
handleUploadError('网络错误,请检查连接后重试', files);
updateButtonState();
});
xhr.open('POST', '/api/upload');
xhr.send(formData);
}
// ── 上传成功处理 ────────────────────────────────────────
function handleUploadSuccess(data, files) {
// 更新每个文件的状态
data.results.forEach((result, idx) => {
if (idx < files.length) {
files[idx].status = result.success ? 'done' : 'error';
files[idx].error = result.success ? null : result.error;
files[idx].progress = 100;
}
});
renderFileList();
// 显示结果面板
const hasSuccess = data.success_count > 0;
const hasFail = data.fail_count > 0;
let resultHtml = `
<div class="result-card">
<div class="result-header">
<div class="result-icon ${hasSuccess ? 'icon-success' : 'icon-error'}">
${hasSuccess
? '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>'
: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>'
}
</div>
<span class="result-title ${hasSuccess ? 'text-success' : 'text-error'}">
${hasSuccess && !hasFail ? '上传成功' : hasSuccess ? '部分上传成功' : '上传失败'}
</span>
</div>
<div class="result-detail">
${hasSuccess ? `<p>已上传 <strong>${data.success_count}</strong> 个文件至 <strong>${data.storage_path}</strong></p>` : ''}
${hasFail ? `<p>${data.fail_count} 个文件上传失败</p>` : ''}
${hasSuccess ? `<p>该目录累计文件数:<strong>${data.total_files}</strong></p>` : ''}
</div>
`;
// 文件详情
if (data.results.length > 0) {
resultHtml += '<div class="result-files">';
data.results.forEach(r => {
if (r.success) {
resultHtml += `
<div class="result-file-item file-success">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>
<span>${r.filename}${r.storage_filename}</span>
</div>`;
} else {
resultHtml += `
<div class="result-file-item file-error">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
<span>${r.filename}${r.error}</span>
</div>`;
}
});
resultHtml += '</div>';
}
resultHtml += '</div>';
$resultPanel.innerHTML = resultHtml;
}
// ── 上传失败处理 ────────────────────────────────────────
function handleUploadError(message, files) {
files.forEach(f => {
f.status = 'error';
f.error = message;
});
renderFileList();
$resultPanel.innerHTML = `
<div class="result-card">
<div class="result-header">
<div class="result-icon icon-error">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
</div>
<span class="result-title text-error">上传失败</span>
</div>
<div class="result-detail">
<p>${message}</p>
</div>
</div>
`;
}
// ── 抖动效果 ────────────────────────────────────────────
function shakeElement(el) {
el.classList.add('shake', 'input-error');
setTimeout(() => {
el.classList.remove('shake');
}, 400);
setTimeout(() => {
el.classList.remove('input-error');
}, 2000);
}
// ── Toast 提示 ──────────────────────────────────────────
function showToast(message) {
const existing = document.querySelector('.toast');
if (existing) existing.remove();
const toast = document.createElement('div');
toast.className = 'toast';
toast.textContent = message;
toast.style.cssText = `
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background: #1a1816;
color: white;
padding: 0.6rem 1.2rem;
border-radius: 8px;
font-size: 0.85rem;
font-weight: 500;
z-index: 1100;
box-shadow: 0 4px 20px rgba(0,0,0,0.15);
`;
document.body.appendChild(toast);
setTimeout(() => {
toast.style.opacity = '0';
toast.style.transition = 'opacity 0.3s';
setTimeout(() => toast.remove(), 300);
}, 2500);
}
// ── 初始化 ──────────────────────────────────────────────
updateButtonState();
})();