From 3045c354f427c443aa481c5c7fed7d61210d6060 Mon Sep 17 00:00:00 2001 From: yuyx <237899745@qq.com> Date: Tue, 25 Nov 2025 10:46:10 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E4=BC=98=E5=8C=96=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E4=BD=93=E9=AA=8C=EF=BC=9A=E4=B8=8A=E4=BC=A0=E5=89=8D?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:原来文件上传完成后才提示超过大小限制,浪费用户时间 修复: - 后端:添加 /api/config 公开接口返回上传大小限制 - 前端:页面加载时获取配置 - 前端:uploadFile 函数开始时检查文件大小,超限立即提示 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- backend/server.js | 11 +++++++++++ frontend/app.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/backend/server.js b/backend/server.js index 40462e1..dc321d2 100644 --- a/backend/server.js +++ b/backend/server.js @@ -972,6 +972,17 @@ app.get('/api/health', (req, res) => { res.json({ success: true, message: 'Server is running' }); }); +// 获取公开的系统配置(不需要登录) +app.get('/api/config', (req, res) => { + const maxUploadSize = parseInt(SettingsDB.get('max_upload_size') || '10737418240'); + res.json({ + success: true, + config: { + max_upload_size: maxUploadSize + } + }); +}); + // 生成验证码API app.get('/api/captcha', captchaRateLimitMiddleware, (req, res) => { try { diff --git a/frontend/app.js b/frontend/app.js index bbd2b6b..6e82c8a 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -152,6 +152,9 @@ createApp({ toasts: [], toastIdCounter: 0, + // 上传限制(字节),默认10GB + maxUploadSize: 10737418240, + // 提示信息 errorMessage: '', successMessage: '', @@ -798,6 +801,19 @@ handleDragLeave(e) { this.stopProfileSync(); }, + // 获取公开的系统配置(上传限制等) + async loadPublicConfig() { + try { + const response = await axios.get(`${this.apiBase}/api/config`); + if (response.data.success) { + this.maxUploadSize = response.data.config.max_upload_size || 10737418240; + } + } catch (error) { + console.error('获取系统配置失败:', error); + // 使用默认值 + } + }, + // 检查本地存储的登录状态 async checkLoginStatus() { const token = localStorage.getItem('token'); @@ -1370,6 +1386,18 @@ handleDragLeave(e) { }, async uploadFile(file) { + // 文件大小限制预检查(在上传前检查,避免用户等待上传完才发现超限) + if (file.size > this.maxUploadSize) { + const fileSizeMB = Math.round(file.size / (1024 * 1024)); + const maxSizeMB = Math.round(this.maxUploadSize / (1024 * 1024)); + this.showToast( + 'error', + '文件超过上传限制', + `文件大小 ${fileSizeMB}MB 超过系统限制 ${maxSizeMB}MB,请选择更小的文件` + ); + return; + } + // 本地存储配额预检查 if (this.storageType === 'local') { const estimatedUsage = this.localUsed + file.size; @@ -2313,6 +2341,8 @@ handleDragLeave(e) { // 检查URL参数 this.checkUrlParams(); + // 获取系统配置(上传限制等) + this.loadPublicConfig(); // 检查登录状态 this.checkLoginStatus(); },