更新 file-share-frontend/script.js

This commit is contained in:
2025-05-27 13:23:19 +08:00
parent 8727785289
commit b6bac2389f

View File

@@ -1,88 +1,88 @@
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
const uploadForm = document.getElementById('uploadForm'); const uploadForm = document.getElementById('uploadForm');
const downloadForm = document.getElementById('downloadForm'); const downloadForm = document.getElementById('downloadForm');
const uploadResult = document.getElementById('uploadResult'); const uploadResult = document.getElementById('uploadResult');
const fileLink = document.getElementById('fileLink'); const fileLink = document.getElementById('fileLink');
const copyLinkBtn = document.getElementById('copyLinkBtn'); const copyLinkBtn = document.getElementById('copyLinkBtn');
// 设置最小过期时间为当前时间 // 设置最小过期时间为当前时间
const expiresInput = document.getElementById('expiresInput'); const expiresInput = document.getElementById('expiresInput');
const now = new Date(); const now = new Date();
const localDatetime = new Date(now.getTime() - now.getTimezoneOffset() * 60000) const localDatetime = new Date(now.getTime() - now.getTimezoneOffset() * 60000)
.toISOString() .toISOString()
.slice(0, 16); .slice(0, 16);
expiresInput.min = localDatetime; expiresInput.min = localDatetime;
// 文件上传处理 // 文件上传处理
uploadForm.addEventListener('submit', async function(e) { uploadForm.addEventListener('submit', async function(e) {
e.preventDefault(); e.preventDefault();
const formData = new FormData(uploadForm); const formData = new FormData(uploadForm);
const fileInput = document.getElementById('fileInput'); const fileInput = document.getElementById('fileInput');
if (!fileInput.files[0]) { if (!fileInput.files[0]) {
alert('请选择要上传的文件'); alert('请选择要上传的文件');
return; return;
} }
try { try {
const response = await fetch('https://share.ssplus.cn/upload', { const response = await fetch('<你的后端地址>/upload', {
method: 'POST', method: 'POST',
body: formData body: formData
}); });
const result = await response.json(); const result = await response.json();
if (result.success) { if (result.success) {
// 显示上传结果 // 显示上传结果
fileLink.href = result.url; fileLink.href = result.url;
fileLink.textContent = result.url; fileLink.textContent = result.url;
uploadResult.classList.remove('hidden'); uploadResult.classList.remove('hidden');
// 清空表单 // 清空表单
uploadForm.reset(); uploadForm.reset();
} else { } else {
alert(`上传失败: ${result.error || '未知错误'}`); alert(`上传失败: ${result.error || '未知错误'}`);
} }
} catch (error) { } catch (error) {
alert(`上传出错: ${error.message}`); alert(`上传出错: ${error.message}`);
} }
}); });
// 复制链接功能 // 复制链接功能
copyLinkBtn.addEventListener('click', function() { copyLinkBtn.addEventListener('click', function() {
navigator.clipboard.writeText(fileLink.href) navigator.clipboard.writeText(fileLink.href)
.then(() => { .then(() => {
const originalText = copyLinkBtn.textContent; const originalText = copyLinkBtn.textContent;
copyLinkBtn.textContent = '已复制!'; copyLinkBtn.textContent = '已复制!';
setTimeout(() => { setTimeout(() => {
copyLinkBtn.textContent = originalText; copyLinkBtn.textContent = originalText;
}, 2000); }, 2000);
}) })
.catch(err => { .catch(err => {
alert('复制失败: ' + err); alert('复制失败: ' + err);
}); });
}); });
// 文件下载处理 // 文件下载处理
downloadForm.addEventListener('submit', function(e) { downloadForm.addEventListener('submit', function(e) {
e.preventDefault(); e.preventDefault();
const fileId = document.getElementById('fileIdInput').value.trim(); const fileId = document.getElementById('fileIdInput').value.trim();
const password = document.getElementById('downloadPasswordInput').value; const password = document.getElementById('downloadPasswordInput').value;
if (!fileId) { if (!fileId) {
alert('请输入文件ID'); alert('请输入文件ID');
return; return;
} }
// 构建下载URL // 构建下载URL
let downloadUrl = `https://share.ssplus.cn/file/${fileId}`; let downloadUrl = `<你的后端地址>/file/${fileId}`;
if (password) { if (password) {
downloadUrl += `?password=${encodeURIComponent(password)}`; downloadUrl += `?password=${encodeURIComponent(password)}`;
} }
// 打开下载链接 // 打开下载链接
window.open(downloadUrl, '_blank'); window.open(downloadUrl, '_blank');
}); });
}); });