From 4bf9a770c22faea46b55a4c05b3ae1a72c57a911 Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Tue, 12 May 2026 11:24:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/ApplyFormView.vue | 55 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/views/ApplyFormView.vue b/src/views/ApplyFormView.vue index 33f7133..11e8621 100644 --- a/src/views/ApplyFormView.vue +++ b/src/views/ApplyFormView.vue @@ -951,6 +951,52 @@ async function saveDraftToServer() { return true } +/** Laravel ValidationException:errors 为字段 → 字符串数组 */ +function firstMessagesFromLaravelErrors(errors: unknown): string[] { + if (errors == null || typeof errors !== 'object' || Array.isArray(errors)) return [] + const out: string[] = [] + for (const v of Object.values(errors as Record)) { + if (Array.isArray(v)) { + for (const s of v) { + if (typeof s === 'string' && s.trim()) out.push(s.trim()) + } + } else if (typeof v === 'string' && v.trim()) { + out.push(v.trim()) + } + } + return out +} + +function isGenericLaravelInvalidMessage(message: string): boolean { + const m = message.trim() + return ( + m === 'The given data was invalid.' || + m === 'The given data was invalid' || + m === '给定的数据无效。' || + m === '给定的数据无效' + ) +} + +/** 按接口返回组装选手可见的提交失败说明(优先业务 errors / message,再按状态码兜底) */ +function submitFailureUserMessage(status: number, body: Record): string { + const fromFields = firstMessagesFromLaravelErrors(body.errors) + if (fromFields.length > 0) return fromFields[0] + + const rawMsg = body.message + if (typeof rawMsg === 'string' && rawMsg.trim()) { + const msg = rawMsg.trim() + if (!isGenericLaravelInvalidMessage(msg)) return msg + } + + if (status === 404) return '赛事不存在或未发布,请返回重新进入报名页。' + if (status === 403) return '当前账号无权限提交,请确认登录状态或联系管理员。' + if (status === 409) return '报名状态已变更或发生冲突,请刷新页面后重试。' + if (status === 429) return '请求过于频繁,请稍后再试。' + if (status >= 500) return '服务暂时不可用,请稍后重试。' + if (status === 422) return '提交失败,请填写完整信息。' + return '报名未能提交,请检查网络或稍后重试。' +} + async function submitApplicationToServer() { const r = await fetch(`${apiBase()}/api/applications/current/submit${competitionQuery()}`, { method: 'POST', @@ -962,8 +1008,13 @@ async function submitApplicationToServer() { return false } if (!r.ok) { - void r.json().catch(() => ({})) - showNotice('报名未能提交,请检查网络或稍后重试。', '提交失败', 'warning') + let body: Record = {} + try { + body = (await r.json()) as Record + } catch { + /* 非 JSON */ + } + showNotice(submitFailureUserMessage(r.status, body), '提交失败', 'warning') return false } const d = (await r.json()) as Parameters[0]