From 5d8491ceb64ca170c4d9c8dbcaf7ccbc5f5c7766 Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Tue, 8 Oct 2024 23:02:55 +0800 Subject: [PATCH] add code preset --- src/api.ts | 26 ++++++++++++++-- src/composables/code.ts | 26 ++++++++++------ src/desktop/Query.vue | 69 +++++++++++++++++++++++++++++++++++++++++ src/desktop/index.vue | 27 ++++++++++++---- 4 files changed, 131 insertions(+), 17 deletions(-) create mode 100644 src/desktop/Query.vue diff --git a/src/api.ts b/src/api.ts index 6ea423e..f3e5934 100644 --- a/src/api.ts +++ b/src/api.ts @@ -24,7 +24,9 @@ function decode(bytes?: string) { ) } -const http = axios.create({ baseURL: `${protocol}://judge0api.xuyue.cc` }) +const judge = axios.create({ baseURL: `${protocol}://judge0api.xuyue.cc` }) +const api = axios.create({ baseURL: `${protocol}://codeapi.xuyue.cc` }) +// const api = axios.create({ baseURL: `http://localhost:8080` }) export async function submit(code: Code, input: string) { const encodedCode = encode(code.value) @@ -42,7 +44,7 @@ export async function submit(code: Code, input: string) { redirect_stderr_to_stdout: true, compiler_options: compilerOptions, } - const response = await http.post("/submissions", payload, { + const response = await judge.post("/submissions", payload, { params: { base64_encoded: true, wait: true }, }) const data = response.data @@ -54,3 +56,23 @@ export async function submit(code: Code, input: string) { } } } + +export async function listCode() { + const res = await api.get("/") + return res.data +} + +export async function getCodeByQuery(query: string) { + const res = await api.get("/" + query) + return res.data +} + +export async function createCode(data: { code: string; query: string }) { + const res = await api.post("/", data) + return res.data +} + +export async function removeCode(id: number) { + const res = await api.delete(`/${id}`) + console.log(res.data) +} diff --git a/src/composables/code.ts b/src/composables/code.ts index e70615e..a295903 100644 --- a/src/composables/code.ts +++ b/src/composables/code.ts @@ -2,7 +2,7 @@ import { useStorage } from "@vueuse/core" import copyTextToClipboard from "copy-text-to-clipboard" import queryString from "query-string" import { reactive, ref, watch } from "vue" -import { submit } from "../api" +import { getCodeByQuery, submit } from "../api" import { sources } from "../templates" import { Cache, Code, LANGUAGE, Status } from "../types" import { atou, utoa } from "../utils" @@ -56,7 +56,7 @@ watch(input, (value: string) => { cache.input.value = value }) -export function init() { +export async function init() { code.language = cache.language.value code.value = cache.code[code.language].value input.value = cache.input.value @@ -65,13 +65,21 @@ export function init() { const parsed = queryString.parse(location.search) const base64 = parsed.share as string - if (!base64) return - try { - const data = JSON.parse(atou(base64)) - code.language = data.lang - code.value = data.code - input.value = data.input - } catch (err) {} + if (base64) { + try { + const data = JSON.parse(atou(base64)) + code.language = data.lang + code.value = data.code + input.value = data.input + } catch (err) {} + } + const preset = parsed.query as string + if (preset) { + try { + const result = await getCodeByQuery(preset) + code.value = result.data.code + } catch (err) {} + } } export function clearInput() { diff --git a/src/desktop/Query.vue b/src/desktop/Query.vue new file mode 100644 index 0000000..87a91c8 --- /dev/null +++ b/src/desktop/Query.vue @@ -0,0 +1,69 @@ + + diff --git a/src/desktop/index.vue b/src/desktop/index.vue index 2530dbc..702c90c 100644 --- a/src/desktop/index.vue +++ b/src/desktop/index.vue @@ -2,7 +2,7 @@
+ + +