微信小程序?qū)崿F(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)
更新時(shí)間:2021年08月13日 16:20:00 作者:半度℃溫?zé)?
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了微信小程序發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖

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="請(qǐng)輸入手機(jī)號(hào)"
placeholder-style="color:#C3C6C4;"/>
</view>
<view class="ver-code">
<view class="code">
<input type="number" bindinput="bindCodeInput" maxlength="6"
placeholder="請(qǐng)輸入驗(yàn)證碼"
placeholder-style="color:#C3C6C4;"/>
</view>
<view class="getCode" bindtap="getCode" wx:if="{{countDownNum == 60 || countDownNum == -1}}">
<button type="primary" plain="true">獲取驗(yàn)證碼</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文件
//獲取應(yīng)用實(shí)例
const app = getApp()
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
phone: null, // 手機(jī)號(hào)
code: null, // 手機(jī)驗(yàn)證碼
countDownNum: 60, // 倒計(jì)時(shí)初始值
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
},
// 輸入手機(jī)號(hào)
bindTelInput: function (e) {
this.setData({
phone: e.detail.value
})
},
// 輸入驗(yàn)證碼
bindCodeInput: function (e) {
this.setData({
code: e.detail.value
})
},
// 發(fā)送手機(jī)驗(yàn)證碼
getCode: function () {
if (!!this.data.phone) {
if (!!(/^1[34578]\d{9}$/.test(this.data.phone))) {
wx.showToast({
title: "發(fā)送成功",
icon: "none",
duration: 1500
})
this.countDown()
} else {
wx.showToast({
title: "請(qǐng)輸入正確的手機(jī)號(hào)",
icon: "none",
duration: 1500
})
}
} else {
wx.showToast({
title: "請(qǐng)輸入手機(jī)號(hào)",
icon: "none",
duration: 1500
})
}
},
/**
* 驗(yàn)證碼倒計(jì)時(shí)
*/
countDown: function () {
var _this = this
var countDownNum = _this.data.countDownNum // 獲取倒計(jì)時(shí)初始值
var timer = setInterval(function () {
countDownNum -= 1
_this.setData({
countDownNum: countDownNum
})
if (countDownNum <= -1) {
clearInterval(timer)
// 取消置頂?shù)膕etInterval函數(shù)將要執(zhí)行的代碼
_this.setData({
countDownNum: 60
})
}
}, 1000)
},
// 手機(jī)驗(yàn)證碼登錄
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: "請(qǐng)輸入驗(yàn)證碼",
icon: "none",
duration: 1500
})
}
} else {
wx.showToast({
title: "請(qǐng)輸入正確的手機(jī)號(hào)",
icon: "none",
duration: 1500
})
}
} else {
wx.showToast({
title: "請(qǐng)輸入手機(jī)號(hào)",
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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript FAQ函數(shù)(提問+回復(fù))
javascript FAQ函數(shù),當(dāng)點(diǎn)擊問題時(shí)顯示下面的回復(fù)內(nèi)容。2009-07-07

