2025-09-13 23:01:14 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="flex">
|
2026-04-30 23:48:48 +08:00
|
|
|
<a-input :value="valueRef" :placeholder="t('certd.captcha.inputImageCode')" autocomplete="off" @update:value="onChange">
|
2025-09-13 23:01:14 +08:00
|
|
|
<template #prefix>
|
|
|
|
|
<fs-icon icon="ion:image-outline"></fs-icon>
|
|
|
|
|
</template>
|
|
|
|
|
</a-input>
|
2026-04-30 23:48:48 +08:00
|
|
|
<div class="input-right pointer" :title="t('certd.captcha.refresh')">
|
2025-09-13 23:01:14 +08:00
|
|
|
<img class="image-code" :src="imageCodeSrc" @click="resetImageCode" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
<script setup lang="ts">
|
2026-01-29 17:21:39 +08:00
|
|
|
import { ref, watch } from "vue";
|
2026-04-30 23:48:48 +08:00
|
|
|
import { useI18n } from "vue-i18n";
|
2025-09-13 23:01:14 +08:00
|
|
|
|
|
|
|
|
const props = defineProps<{
|
2025-09-24 00:06:00 +08:00
|
|
|
modelValue: any;
|
2025-09-13 23:01:14 +08:00
|
|
|
captchaGet?: () => Promise<any>;
|
|
|
|
|
}>();
|
|
|
|
|
defineOptions({
|
|
|
|
|
name: "ImageCaptcha",
|
|
|
|
|
});
|
2026-04-30 23:48:48 +08:00
|
|
|
const { t } = useI18n();
|
2025-09-13 23:01:14 +08:00
|
|
|
const emit = defineEmits(["update:modelValue", "change"]);
|
|
|
|
|
|
|
|
|
|
const valueRef = ref("");
|
|
|
|
|
const randomStrRef = ref();
|
|
|
|
|
const imageCodeSrc = ref();
|
|
|
|
|
async function resetImageCode() {
|
|
|
|
|
const res = await props.captchaGet();
|
|
|
|
|
randomStrRef.value = res.randomStr;
|
|
|
|
|
valueRef.value = "";
|
|
|
|
|
emitChange(null);
|
|
|
|
|
imageCodeSrc.value = "data:image/svg+xml," + encodeURIComponent(res.imageData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCaptchaForm() {
|
|
|
|
|
return {
|
|
|
|
|
imageCode: valueRef.value,
|
|
|
|
|
randomStr: randomStrRef.value,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
defineExpose({
|
|
|
|
|
resetImageCode,
|
|
|
|
|
getCaptchaForm,
|
2025-09-24 00:06:00 +08:00
|
|
|
reset: resetImageCode,
|
2025-09-13 23:01:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
resetImageCode();
|
|
|
|
|
|
|
|
|
|
function onChange(value: string) {
|
|
|
|
|
valueRef.value = value;
|
|
|
|
|
const form = getCaptchaForm();
|
|
|
|
|
emitChange(form);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-24 00:06:00 +08:00
|
|
|
watch(
|
|
|
|
|
() => {
|
|
|
|
|
return props.modelValue;
|
|
|
|
|
},
|
|
|
|
|
value => {
|
|
|
|
|
if (value == null) {
|
2025-09-24 00:55:31 +08:00
|
|
|
resetImageCode();
|
2025-09-24 00:06:00 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function emitChange(value: any) {
|
2025-09-13 23:01:14 +08:00
|
|
|
emit("update:modelValue", value);
|
|
|
|
|
emit("change", value);
|
|
|
|
|
}
|
|
|
|
|
</script>
|