2025-09-13 23:01:14 +08:00
|
|
|
<template>
|
2025-09-24 00:06:00 +08:00
|
|
|
<component :is="captchaComponent" v-if="settingStore.inited" ref="captchaRef" :model-value="modelValue" class="captcha_input" :captcha-get="getCaptcha" @change="onChange" />
|
2025-09-13 23:01:14 +08:00
|
|
|
</template>
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ref, computed, defineAsyncComponent } from "vue";
|
|
|
|
|
import { useSettingStore } from "/@/store/settings";
|
|
|
|
|
import { nanoid } from "nanoid";
|
|
|
|
|
import { request } from "/@/api/service";
|
|
|
|
|
|
2025-09-24 00:06:00 +08:00
|
|
|
const props = defineProps({
|
|
|
|
|
modelValue: {
|
|
|
|
|
type: Object,
|
|
|
|
|
default: () => ({}),
|
|
|
|
|
},
|
2025-09-27 01:19:32 +08:00
|
|
|
type: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: "image",
|
|
|
|
|
},
|
|
|
|
|
addonId: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 0,
|
|
|
|
|
},
|
2025-09-24 00:06:00 +08:00
|
|
|
});
|
2025-09-13 23:01:14 +08:00
|
|
|
const captchaRef = ref(null);
|
|
|
|
|
const settingStore = useSettingStore();
|
|
|
|
|
|
|
|
|
|
const emits = defineEmits(["update:modelValue", "change"]);
|
|
|
|
|
const captchaImpls = import.meta.glob("./captchas/*.vue");
|
|
|
|
|
|
|
|
|
|
const captchaAddonId = computed(() => {
|
|
|
|
|
return settingStore.sysPublic.captchaAddonId ?? 0;
|
|
|
|
|
});
|
|
|
|
|
const captchaComponent = computed(() => {
|
2025-09-27 01:19:32 +08:00
|
|
|
let type: any = props.type ?? "image";
|
2025-09-13 23:01:14 +08:00
|
|
|
if (settingStore.sysPublic.captchaAddonId && settingStore.sysPublic.captchaType) {
|
|
|
|
|
type = settingStore.sysPublic.captchaType;
|
|
|
|
|
}
|
|
|
|
|
const componentName = `${type}_captcha`;
|
|
|
|
|
return defineAsyncComponent(captchaImpls[`./captchas/${componentName}.vue`]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function getCaptcha(): Promise<any> {
|
|
|
|
|
const randomStr = nanoid(10);
|
|
|
|
|
return await request({
|
|
|
|
|
url: `/basic/code/captcha/get?randomStr=${randomStr}`,
|
|
|
|
|
method: "post",
|
|
|
|
|
data: {
|
|
|
|
|
captchaAddonId: captchaAddonId.value,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-24 00:06:00 +08:00
|
|
|
function onChange(data: any) {
|
2025-09-13 23:01:14 +08:00
|
|
|
emits("update:modelValue", data);
|
|
|
|
|
emits("change", data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getCaptchaForm() {
|
2025-10-28 15:30:31 +08:00
|
|
|
return await captchaRef.value?.getCaptchaForm();
|
2025-09-13 23:01:14 +08:00
|
|
|
}
|
2025-09-24 00:06:00 +08:00
|
|
|
async function reset() {
|
2025-10-28 15:30:31 +08:00
|
|
|
await captchaRef.value?.reset();
|
2025-09-24 00:06:00 +08:00
|
|
|
}
|
2025-09-13 23:01:14 +08:00
|
|
|
defineExpose({
|
|
|
|
|
getCaptchaForm,
|
2025-09-24 00:06:00 +08:00
|
|
|
reset,
|
2025-09-13 23:01:14 +08:00
|
|
|
});
|
|
|
|
|
</script>
|