From ac806544c55cd4af4a3cbc741e991db266b46a88 Mon Sep 17 00:00:00 2001 From: Timi Date: Fri, 7 Nov 2025 18:21:23 +0800 Subject: [PATCH] support captchaId --- src/components/captcha/index.vue | 36 ++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/components/captcha/index.vue b/src/components/captcha/index.vue index 6473c7d..74a0277 100644 --- a/src/components/captcha/index.vue +++ b/src/components/captcha/index.vue @@ -26,14 +26,42 @@ const props = withDefaults(defineProps<{ const { width, height, from, api } = toRefs(props); const src = ref(""); -function update() { - src.value = `${api.value}?from=${from.value}&width=${width.value}&height=${height.value}&r=${Toolkit.random(0, 999999)}`; +const captchaId = ref(""); + +const emit = defineEmits<{ + "update:captchaId": [id: string], +}>(); + +async function update() { + if (src.value && src.value.startsWith("blob:")) { + URL.revokeObjectURL(src.value); + } + const response = await fetch(`${api.value}?from=${from.value}&width=${width.value}&height=${height.value}&r=${Toolkit.random(0, 999999)}`); + if (!response.ok) { + throw new Error(`fetch captcha error: ${response.status}`); + } + captchaId.value = response.headers.get("X-Captcha-ID") || ""; + src.value = URL.createObjectURL(await response.blob()); + emit("update:captchaId", captchaId.value); } onMounted(update); -defineExpose({ - update +watch([width, height], () => { + update(); }); + +onUnmounted(() => { + if (src.value && src.value.startsWith("blob:")) { + URL.revokeObjectURL(src.value); + } +}); + +defineExpose({ + update, + getCaptchaId: () => captchaId.value, + refresh: update +}); +