index.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <script setup lang="ts">
  2. import type { PropType } from 'vue';
  3. import { onMounted, ref } from 'vue';
  4. import { useVModel } from '@vueuse/core';
  5. const props = defineProps({
  6. api: {
  7. type: Function as PropType<(params: any) => Promise<unknown>>,
  8. default: null,
  9. },
  10. uuidField: {
  11. type: String,
  12. default: 'uuid',
  13. },
  14. base64Field: {
  15. type: String,
  16. default: 'base64',
  17. },
  18. prefix: {
  19. type: String,
  20. default: 'data:image/png;base64,',
  21. },
  22. value: {
  23. type: String,
  24. default: '',
  25. },
  26. params: {
  27. type: Object,
  28. default: () => {
  29. return {};
  30. },
  31. },
  32. immediate: {
  33. type: Boolean,
  34. default: true,
  35. },
  36. });
  37. const emit = defineEmits(['update:modelValue']);
  38. const modelValue = useVModel(props, 'value', emit, {
  39. defaultValue: props.value,
  40. passive: true,
  41. });
  42. const base64 = ref<string>('');
  43. const fetch = () => {
  44. props.api?.(props.params).then((res: any) => {
  45. modelValue.value = res[props.uuidField];
  46. base64.value = `${props.prefix}${res[props.base64Field]}`;
  47. emit('update:modelValue', res[props.uuidField]);
  48. });
  49. };
  50. onMounted(() => {
  51. if (props.immediate) {
  52. fetch();
  53. }
  54. });
  55. defineExpose({
  56. resume: fetch,
  57. });
  58. </script>
  59. <template>
  60. <div class="m-captcha">
  61. <img :src="base64" @click="fetch" />
  62. </div>
  63. </template>
  64. <style lang="less" scoped></style>