小程序模板:专业的小程序模板与静态模板分享平台
小程序
教程
搜索
当前位置 : 首页> 小程序教程> uniapp微信小程序验证码获取与倒计时功能实现

uniapp微信小程序验证码获取与倒计时功能实现

1. 数据定义与模板绑定



2. 倒计时逻辑实现

methods: {
  startCountdown() {
    this.countdown = 60
    this.isCounting = true
    
    const timer = setInterval(() => {
      if (this.countdown <= 0) {
        clearInterval(timer)
        this.isCounting = false
        return
      }
      this.countdown--
    }, 1000)
  }
}

3. 手机号验证与请求发送

async getCode() {
  // 正则校验
  const reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
  if (!reg.test(this.phone)) {
    uni.showToast({ title: '手机号格式错误', icon: 'none' })
    return
  }

  // 发送验证码请求
  try {
    const res = await uni.request({
      url: '/api/sendSms',
      method: 'POST',
      data: { phone: this.phone }
    })
    
    if (res.data.code === 200) {
      this.startCountdown()
    }
  } catch (err) {
    uni.showToast({ title: '发送失败', icon: 'none' })
  }
}

4. 完整参考案例






联系客服 意见反馈

签到成功!

已连续签到1天,签到3天将获得积分VIP1天

知道了