获取手机验证码倒计时

wxml

<view class="phoneContainer">
  <input type="number" maxlength="11" disabled="{{disabled}}" placeholder="手机号" value="{{phoneNumber}}" class="phoneNumber" bindinput="getPhone"/>
</view>
<view class="codeContainer">
  <input type="number" placeholder="验证码" bindinput="getPhoneCode" class="code" />
  <button class="getCode" bindtap="getCode"  disabled='{{isDis}}' style="background:{{bg==true?'#ccc':'linear-gradient(to right,#7D93FF, #6667FF)'}}">
  	{{tip=='new'?'获取验证码':tip=='send'?'已发送'+seconds:'重新发送'}}
  </button>
</view>

js

data: {
    btnDisabled:false,
    seconds:60,
    tip:'new',
    phoneNumber:'',
    phoneCode:'',
    binding:true,//已绑定手机号
    disabled:false,//能否编辑手机号
    uid:'',
    isDis:false//倒计时
  },


/* 获取手机号 */
  getPhone(e){
    this.setData({
      phoneNumber: e.detail.value
    })
  },
 /* 获取验证码 */
  getPhoneCode(e){
    this.setData({
      phoneCode: e.detail.value
    })
  },
  /* 点击获取验证码 */
  getCode: function(e){
    var userinfo = wx.getStorageSync('wxinfo');
    var uid = userinfo.uid;
    let reg= /^1[0-9]{10}/g;
    let that = this;
    var phoneNumber = that.data.phoneNumber;
    if (reg.test(phoneNumber) ){
      wx.request({
        url:  '/api/getCode/getCode', //仅为示例,并非真实的接口地址
        data: {
          phone: phoneNumber,
          uid: uid,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          if (res.data.code == 100) {
              var timer = null;
              var seconds = that.data.seconds;
              that.setData({
                btnDisabled: true,
                bg: true,
                tip: 'send',
                isDis:true
              })
            timer = setInterval(function () {
              seconds--;
              if (seconds < 0) {
                clearInterval(timer)
                seconds = 60;
                that.setData({
                  seconds: seconds,
                  tip: 'agin',
                  bg: false,
                  btnDisabled: false,
                  isDis:false
                })
              }
              that.setData({
                seconds: seconds
              })
            }, 1000)
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else{
      wx.showToast({
        title: '请输入正确的手机号',
        icon:'none',
        duration: 2000
      })
    }
    
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值