微信小程序獲取驗證碼60秒倒計時功能
更新時間:2023年04月24日 10:27:36 作者:switch
這篇文章主要介紹了微信小程序獲取驗證碼60秒倒計時模板,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
效果圖
index.wxml
<view class="Info"> <view class="Num"> <view>6位數(shù)字驗證碼</view> <view class="verification_time"> <button bindtap='MosendSms' disabled='{{MoDisabled}}' class="Box_hid" style='color:{{MoColor}}' type="button">{{MoCodeMsg}}</button> </view> </view> <view class='verification'> <block wx:for="{{Length}}" wx:key="item"> <input class='frame' value="{{Showboxval.length>=index+1?Showboxval[index]:''}}" disabled catchtap='InputTap' /> </block> </view> <view class="error" wx:if="{{error}}">驗證碼輸入錯誤</view> <input name="password" password="{{true}}" class='ipt' maxlength="{{Length}}" focus="{{isFocus}}" bindinput="FocuInput" /> </view>
index.js
data: { code: "", //后端驗證碼 Jurisdiction: true, //是否有權(quán)限 error: false, //錯誤提示 Length: 6, //輸入框個數(shù) isFocus: true, //聚焦 Showboxval: "", //輸入的內(nèi)容 MoDisabled: false, //驗證碼是否可點擊 MoCodeMsg: '獲取驗證碼', //文案 MoMsgWait: 60, //時間秒 MoColor: 'rgba(40, 200, 122, 1)', //默認驗證碼字體顏色 },
/** * 生命周期函數(shù)--監(jiān)聽頁面加載 正常第一次進來先調(diào)用一次 */ onLoad(options) { this.MosendSms() // 60秒后重新獲取驗證碼 },
// 60秒后驗證碼 sendSbinms() { wx.showToast({ title: '短信驗證碼發(fā)送成功,請注意查收', icon: 'none' }) this.setData({ MoCodeMsg: this.data.MoMsgWait + " s", MoColor: 'rgba(40, 200, 122, 1)', MoMsgWait: this.data.MoMsgWait - 1, MoDisabled: true }); let inter = setInterval(function () { this.setData({ MoCodeMsg: this.data.MoMsgWait + " s", MoColor: 'rgba(40, 200, 122, 1)', MoMsgWait: this.data.MoMsgWait - 1, MoDisabled: true }); if (this.data.MoMsgWait < 0) { clearInterval(inter) this.setData({ MoCodeMsg: "重新獲取", MoMsgWait: 60, MoDisabled: false, MoColor: 'rgba(40, 200, 122, 1)' }); } //注意后面的bind綁定,最關(guān)鍵。不然又是未定義,無法使用外圍的變量; }.bind(this), 1000); },
// 點擊獲取驗證碼 MosendSms() { if (this.data.Jurisdiction) { this.sendSbinms() // 60秒后重新獲取驗證碼 this.obtain(); //后端接口 獲取驗證碼 } else { wx.showToast({ title: this.data.massage ? this.data.massage : '手機號未注冊', icon: 'error', duration: 3000 }) } },
obtain() { let params = { phone: this.data.rstProduct, type: 1 } appletValidateCode(params).then((res) => { this.setData({ code: res.data.data, }); }).catch((res) => {}) },
//驗證碼輸入框 FocuInput(e) { let that = this; let inputValue = e.detail.value; that.setData({ Showboxval: inputValue, }) if (inputValue.length === 6) { if (inputValue == this.data.code) { this.setData({ error: false, }); } else { this.setData({ error: true, }); } } },
//驗證碼輸入框點擊 InputTap() { let that = this; that.setData({ isFocus: true, }) },
index.wxss
.Info { padding: 138rpx 32rpx 0 32rpx; } .verification { display: flex; align-items: center; justify-content: space-between; margin-top: 30rpx; font-size: 32rpx; } .Num { display: flex; align-items: center; justify-content: space-between; color: rgba(0, 0, 0, 0.65); font-size: 30rpx; font-family: PingFang SC-Regular, PingFang SC; font-weight: 400; } .frame { width: 80rpx; height: 80rpx; border-radius: 2px; border: 2rpx solid #DEDEDE; display: flex; justify-content: center; align-items: center; text-align: center; font-size: 32rpx; font-family: PingFang SC-Medium, PingFang SC; font-weight: 500; color: rgba(0, 0, 0, 0.65); } .ipt { width: 0; height: 0; } .Box_hid { font-size: 30rpx; background: #fff !important; text-align: left; color: rgba(40, 200, 122, 1) !important; padding-right: 0 !important; font-family: PingFang SC-Regular, PingFang SC; font-weight: 400 !important; } .Box_hid::after { border: none; } .error { color: #F24236; margin-top: 8rpx; font-size: 28rpx; font-family: PingFang SC-Regular, PingFang SC; font-weight: 400; }
到此這篇關(guān)于微信小程序獲取驗證碼60秒倒計時模板的文章就介紹到這了,更多相關(guān)微信小程序60秒倒計時內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
bootstrapTable+ajax加載數(shù)據(jù) refresh更新數(shù)據(jù)
這篇文章主要為大家詳細介紹了bootstrapTable+ajax加載數(shù)據(jù),以及refresh更新數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08Google 爬蟲如何抓取 JavaScript 的內(nèi)容
我們測試了谷歌爬蟲是如何抓取 JavaScript,下面就是我們從中學(xué)習到的知識,需要的朋友可以參考下2017-04-04