短信验证码60倒计时

export default {
  data () {
    return {
      btnColor: '#ea5504',
      content: '发送验证码', // 按钮里显示的内容
      totalTime: 90, // 记录具体倒计时间 s
      typeBtn: 'warning',
      sendBtnDisabled: false, // 发送短信按钮状态
      captchaId: '', // 图形验证码验证id
      verifyFlag: false,
      imgCode: '',
      active: 0,
      activeType: '',
      formData: {
        userName: '',
        telephone: '',
        SMSVerCode: '',
        VerifyCode: ''
      },
      nameType: false,
      phoneType: false,
      phoneLicit: false,
      codeType: false,
      codeVerifyType: false
    }
  },
  directives: {
    fours: {
      inserted (el, bindings, vnode) { // 指令元素插入到页面时执行
        el.changeValue = function (e) {
          if (el.value.trim()) {
            switch (bindings.value) {
              case 'name':
                vnode.context['nameType'] = false
                break
              case 'phone':
                vnode.context['phoneType'] = false
                vnode.context['phoneLicit'] = !/^1\d{10}$/.test(el.value.trim())
                break
              case 'code' :
                vnode.context['codeType'] = false
                break
              case 'codeVerify' :
                vnode.context['codeVerifyType'] = false
                break
            }
          } else {
            vnode.context[`${bindings.value}Type`] = true
          }
        }
        el.onblur = function () {
          el.changeValue()
        }
        el.onfocus = function () {
          // 对比名称一样加边框颜色
          vnode.context['activeType'] = bindings.value
        }
        el.oninput = el.changeValue
      }
    }
  },
  methods: {
    // 计算class类名
    classObject (value) {
      return {
        active: this.activeType === value,
        bdColor: value === 'phone' && (this.phoneLicit || this[`${value}Type`])
      }
    },
    // 清除数据
    reset (value) {
      this.formData[value] = ''
    },
    // 发送验证码
    async sendCode () {
      let res = await getReportCode({
        captchaId: this.captchaId,
        captchaValue: this.formData.VerifyCode,
        clientId: 'h5',
        clientName: 'h5_mobile',
        mobile: this.formData.telephone
      })
      if (res.code === '30000') {
        this.verifyFlag = true
        this.createVerifyVode()
      }
      if (res.code === '30001') {
        this.$notify({
          message: res.message,
          color: '#ff9f0a',
          background: '#fff8ea'
        })
        this.createVerifyVode()
      }
      if (res.code === 1) {
        this.sendBtnDisabled = true
        this.typeBtn = 'info'
        this.btnColor = '#d9d9d9'
        this.content = this.totalTime + 's后重新获取'
        let timer = setInterval(() => {
          this.totalTime--
          this.content = this.totalTime + 's后重新获取'
          if (this.totalTime < 0) {
            clearInterval(timer)
            this.content = '发送验证码'
            this.totalTime = 90
            this.sendBtnDisabled = false
            this.typeBtn = 'warning'
            this.btnColor = '#ea5504'
          }
        }, 1000)
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值