微信小程序實現發送短信驗證碼倒計時

本文實例為大傢分享瞭微信小程序發送短信驗證碼倒計時的具體代碼,供大傢參考,具體內容如下

效果圖

WXML文件

<view class="container">
  <view class="userinfo">
    <image class="userinfo-avatar" src="../../images/timg.jpg" mode="cover"></image>
    <text class="userinfo-nickname">什麼</text>
  </view>
  <view class="wrap">
    <view class="tel">
      <input type="number" bindinput="bindTelInput" maxlength="11" 
         placeholder="請輸入手機號"
         placeholder-style="color:#C3C6C4;"/>
    </view>
    <view class="ver-code">
      <view class="code">
        <input type="number" bindinput="bindCodeInput" maxlength="6"
               placeholder="請輸入驗證碼"
               placeholder-style="color:#C3C6C4;"/>
      </view>
      <view class="getCode" bindtap="getCode" wx:if="{{countDownNum == 60 || countDownNum == -1}}">
        <button type="primary" plain="true">獲取驗證碼</button>
      </view>
      <view class="getCode" wx:else>
        <button type="primary" plain="true">{{countDownNum}}s後重新獲取</button>
      </view>
    </view>
  </view>
  <view class="btn-login" bindtap="login">登錄</view>
</view>

JS文件

//獲取應用實例
const app = getApp()

Page({

  /**
   * 頁面的初始數據
   */
  data: {
    phone: null, // 手機號
    code: null, // 手機驗證碼
    countDownNum: 60, // 倒計時初始值
  },

  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函數--監聽頁面顯示
   */
  onShow: function () {
    
  },
  
  /**
   * 生命周期函數--監聽頁面初次渲染完成
   */
  onReady: function () {

  },
  
  /**
   * 用戶點擊右上角分享
   */
  onShareAppMessage: function () {

  },
  // 輸入手機號
  bindTelInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 輸入驗證碼
  bindCodeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 發送手機驗證碼
  getCode: function () {
    if (!!this.data.phone) {
      if (!!(/^1[34578]\d{9}$/.test(this.data.phone))) {
        wx.showToast({
          title: "發送成功",
          icon: "none",
          duration: 1500
        })
        this.countDown()
      } else {
        wx.showToast({
          title: "請輸入正確的手機號",
          icon: "none",
          duration: 1500
        })
      }
    } else {
      wx.showToast({
        title: "請輸入手機號",
        icon: "none",
        duration: 1500
      })
    }
  },
  /**
   * 驗證碼倒計時
   */
  countDown: function () {
    var _this = this
    var countDownNum = _this.data.countDownNum // 獲取倒計時初始值
    var timer = setInterval(function () {
      countDownNum -= 1
      _this.setData({
        countDownNum: countDownNum
      })
      if (countDownNum <= -1) {
        clearInterval(timer)
        // 取消置頂的setInterval函數將要執行的代碼
        _this.setData({
          countDownNum: 60
        })
      }
    }, 1000)
  },
  // 手機驗證碼登錄
  login: function () {
    if (this.data.phone) {
      if (!!(/^1[34578]\d{9}$/.test(this.data.phone))) {
        if (this.data.code) {
          wx.showToast({
            title: "登錄成功",
            icon: "none",
            duration: 1500
          })
        } else {
          wx.showToast({
            title: "請輸入驗證碼",
            icon: "none",
            duration: 1500
          })
        }
      } else {
        wx.showToast({
          title: "請輸入正確的手機號",
          icon: "none",
          duration: 1500
        })
      }
    } else {
      wx.showToast({
        title: "請輸入手機號",
        icon: "none",
        duration: 1500
      })
    }
  }
})

WXSS文件

.userinfo {
  height: 240rpx;
  margin: 40rpx auto 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo .userinfo-avatar {
  width: 140rpx;
  height: 140rpx;
  margin: 20rpx;
  border-radius: 50%;
  border: 1rpx solid #dad5d5;
}

.userinfo .userinfo-nickname {
  color: #aaa;
}

.wrap {
  width: 630rpx;
  font-size: 32rpx;
  margin: 80rpx auto 120rpx;
}

.wrap .tel {
  width: 100%;
  height: 68rpx;
  border-bottom: 1rpx solid #DDE3EC;
  margin-bottom: 60rpx;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.wrap .ver-code {
  width: 100%;
  height: 68rpx;
  border-bottom: 1rpx solid #DDE3EC;
  display: flex;
  justify-content: space-between;
}

.wrap .ver-code .code {

}

.wrap .ver-code .getCode {
  min-width: 190rpx;
  height: 40rpx;
}

.wrap .ver-code .getCode button {
  width: 100%;
  height: 100%;
  font-size: 28rpx;
  font-weight: normal;
  line-height: 40rpx;
  background: #fff;
  color: #ffaa7f;
  border: none;
  padding: 0;
  margin: 0;
}

.btn-login {
  width: 588rpx;
  height: 88rpx;
  background: #ffaa7f;
  border-radius: 10rpx;
  text-align: center;
  line-height: 88rpx;
  font-size: 36rpx;
  font-weight: 500;
  color: #fff;
  margin: 0 auto;
}

.clickClass {
  background: #ea986c;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: