vue实现手机验证功能

该博客详细介绍了如何在前端实现短信验证码功能,包括显示对话框、输入验证码、发送和重置验证码的倒计时逻辑,以及验证验证码的正确性。通过点击获取验证码按钮,触发发送短信请求,并启动倒计时。当验证码输入正确后,可以进行下一步操作。博客内容涉及到HTML、Vue.js和JavaScript等前端技术。
摘要由CSDN通过智能技术生成

效果预览:

点击后:

代码实现:

    <!-- 短信验证码 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      width="400px"
      center
      @close="close"
    >
      <el-form ref="formData_passcode" :model="formData_passcode" :rules="rules1">
        <div style="margin-bottom:10px">
          接收的手机号: {{ formData.entPhone.slice(0,3) + '****' + formData.entPhone.slice(-4) }}
        </div>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item prop="code">
              <el-input v-model="formData_passcode.code" maxlength="6" placeholder="请输入验证码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button size="mini" type="primary" :disabled="clockShow ? true : false" @click="getMessage">
                {{ clockShow ? clock + 's后重试' : '点击获取验证码' }}
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <!-- <span v-if="clockShow">{{ clock }} s内</span> -->
            </el-form-item>
          </el-col>
        </el-row>
        <div v-if="clockShow">{{ clockShow ? '验证码已发送,2分钟内有效' : '验证码已过期,请点击重新发送!' }}</div>
        <div style="text-align:right">
          <el-button size="mini" type="primary" @click="passcodeConfirm">确定</el-button>
        </div>
      </el-form>
    </el-dialog>

 

    // 获取短信验证码
    getMessage() {
      this.clockShow = true
      sendMessage(this.formData.entPhone).then(res => {
        // console.log(res)
        if (res.code === 0) {
          console.log('发送成功')
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
      this.setTime() // 开始倒计时
    },
    
    // 倒计时函数封装
    setTime() {
      if (this.clock === 0) {
        this.clock = 120
        this.clockShow = false
        return
      } else {
        this.clock--
      }
      setTimeout(() => { this.setTime() }, 1000)
    },
    // 验证通过
    passcodeConfirm() {
      if (this.formData_passcode.code) {
        this.formData_passcode.entPhone = this.formData.entPhone
        // 校验验证码输入是否正确
        getCompareCode(this.formData_passcode).then(res => {
          if (res.data) {
            this.$message({
              type: 'success',
              message: res.msg
            })
            this.submit() // 如果验证码输入正确,那么接下来要做什么操作?
            this.dialogFormVisible = false
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
        })
      } else {
        this.$message({
          type: 'error',
          message: '请输入验证码!'
        })
      }
    },

    // 事件操作
    submit() {
        
    }

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值