From 3d501e8b6951e35ac8afdb677af1b68cb5b423eb Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Tue, 25 Aug 2026 14:03:51 -0600 Subject: [PATCH] =?UTF-8?q?fix(=E8=AF=95=E8=BF=90=E8=A1=8C):=20=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E5=9B=BE/SQL=20=E4=BC=9A=E5=BE=80=20Judge0=20?= =?UTF-8?q?=E5=8F=91=E5=87=BA=20language=5Fid:=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Judge0 的语言 id 表里只有 C/C++/Java/Golang/JavaScript/Python2/Python3, 而 code.language 的类型包含 Flowchart 和 SQL —— 前者不是可执行代码, 后者由本站自己的 SQL 沙箱判,都走不到 Judge0。之前直接索引拿到 undefined 就发出去了。 改成 Partial>,取不到就提前返回提示。 Co-Authored-By: Claude Opus 5 --- apps/web/src/utils/judge.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/apps/web/src/utils/judge.ts b/apps/web/src/utils/judge.ts index 6015d21..614ad85 100644 --- a/apps/web/src/utils/judge.ts +++ b/apps/web/src/utils/judge.ts @@ -1,20 +1,27 @@ import axios from "axios" import { decode, encode } from "./functions" -import { Code } from "./types" +import type { Code, LANGUAGE } from "./types" const http = axios.create({ baseURL: import.meta.env.PUBLIC_JUDGE0_URL }) +// Judge0 的语言 id。Flowchart 和 SQL 不在其中 —— 前者根本不是可执行代码, +// 后者由本站自己的 SQL 沙箱判,都走不到 Judge0。 +const JUDGE0_LANGUAGE_ID: Partial> = { + C: 50, + "C++": 54, + Java: 62, + Golang: 60, + JavaScript: 63, + Python2: 70, + Python3: 71, +} + export async function createTestSubmission(code: Code, input: string) { const encodedCode = encode(code.value) - const id = { - C: 50, - "C++": 54, - Java: 62, - Golang: 60, - JavaScript: 63, - Python2: 70, - Python3: 71, - }[code.language] + const id = JUDGE0_LANGUAGE_ID[code.language] + if (id === undefined) { + return { status: null, output: `${code.language} 不支持在线试运行` } + } let compilerOptions = "" if (id === 50) compilerOptions = "-lm" // 解决 GCC 的链接问题 const payload = {