index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <div class="h-[30px] leading-[28px]">
  3. <el-button type="primary" link class="!text-[12px]" :disabled="!sendSms.canGetCode.value" @click="handleClick">{{ sendSms.text.value }}</el-button>
  4. </div>
  5. <el-dialog v-model="captchaDialog" :title="t('captchaTitle')" width="350px" :append-to-body="true" :align-center="true">
  6. <el-form :model="formData" ref="formRef" :rules="formRules">
  7. <el-form-item prop="captcha_code" style="margin-bottom: 0;">
  8. <el-input v-model="formData.captcha_code" :placeholder="t('captchaPlaceholder')">
  9. <template #suffix>
  10. <div class="py-[5px] leading-none">
  11. <el-image :src="captcha.image.value" class="h-[30px] cursor-pointer" @click="captcha.refresh()"></el-image>
  12. </div>
  13. </template>
  14. </el-input>
  15. </el-form-item>
  16. </el-form>
  17. <template #footer>
  18. <span class="dialog-footer">
  19. <el-button @click="captchaDialog = false">{{ t('cancel') }}</el-button>
  20. <el-button type="primary" :loading="loading" @click="confirm">{{ t('confirm') }}</el-button>
  21. </span>
  22. </template>
  23. </el-dialog>
  24. </template>
  25. <script lang="ts" setup>
  26. const prop = defineProps({
  27. mobile: String,
  28. type: {
  29. type: String,
  30. default: ''
  31. },
  32. modelValue: {
  33. type: String,
  34. default: ''
  35. }
  36. })
  37. const emits = defineEmits(['update:modelValue', 'click'])
  38. const value = computed({
  39. get() {
  40. return prop.modelValue
  41. },
  42. set(value) {
  43. emits('update:modelValue', value)
  44. }
  45. })
  46. const loading = ref(false)
  47. const formData = reactive<requestMobileParam>({
  48. mobile: '',
  49. captcha_code: '',
  50. captcha_key: '',
  51. type: prop.type
  52. })
  53. const formRules = reactive({
  54. captcha_code: {
  55. required: true,
  56. message: t('captchaPlaceholder'),
  57. trigger: ['blur']
  58. }
  59. })
  60. const formRef = ref<AnyObject | null>(null)
  61. const captchaDialog = ref(false)
  62. const captcha = useCaptcha(formData)
  63. captcha.refresh()
  64. const sendSms = useSendSms()
  65. const send = () => {
  66. formData.mobile = prop.mobile
  67. if (sendSms.canGetCode.value) {
  68. captchaDialog.value = true
  69. }
  70. }
  71. const confirm = async () => {
  72. await formRef.value?.validate(async (valid, fields) => {
  73. if (valid) {
  74. loading.value = true
  75. const sendRes = await sendSms.send(formData)
  76. if (sendRes) {
  77. value.value = sendRes
  78. captchaDialog.value = false
  79. captcha.refresh()
  80. formData.captcha_code = ''
  81. loading.value = false
  82. } else if (sendRes === false) {
  83. captcha.refresh()
  84. loading.value = false
  85. }
  86. }
  87. })
  88. }
  89. const handleClick = () => {
  90. emits('click')
  91. }
  92. defineExpose({
  93. send
  94. })
  95. </script>
  96. <style lang="scss" scoped></style>