Skip to content

InputCode 输入框验证码

💡 组件名:my-input-code

输入框验证码组件,用于获取邮箱验证码和短信验证码

局部导入

ts
import { defineAsyncComponent } from 'vue'
ts
const MyInputCode = defineAsyncComponent(() => import('/@/components/my-input-code/index.vue'))

邮箱验证码

ts
const state = reactive({
  form: {
    email: '',
    code: '',
    codeId: '',
  },
})

//验证邮箱
const validateEmail = (callback: Function) => {
  formRef.value.validateField('email', (isValid: boolean) => {
    if (!isValid) {
      emailRef.value?.focus()
      return
    }
    callback?.()
  })
}

//发送验证码
const onSend = (codeId: string) => {
  state.form.codeId = codeId
}
vue
<MyInputCode v-model="form.code" :email="form.email" :validate="validateEmail" @send="onSend" />

短信验证码

ts
const state = reactive({
  form: {
    mobile: '',
    code: '',
    codeId: '',
  },
})

//验证手机号
const validateMobile = (callback: Function) => {
  formRef.value.validateField('mobile', (isValid: boolean) => {
    if (!isValid) {
      phoneRef.value?.focus()
      return
    }
    callback?.()
  })
}

//发送验证码
const onSend = (codeId: string) => {
  state.form.codeId = codeId
}
vue
 <MyInputCode v-model="form.code" :mobile="form.mobile" :validate="validateMobile" @send="onSend" />

Props

参数说明类型默认值可选值
maxlength最大长度Number6-
seconds获取验证码后倒计时,单位秒Number60-
maxlength最大长度Number6-
startText倒计时开始文本String获取验证码-
changeText倒计时进行文本Strings秒后重发-
endText倒计时结束文本String重新发送验证码-
mobile手机号String--
email邮箱String--
validate验证方法Function--