微信小程序發(fā)送短信驗證碼完整實例
更新時間:2019年01月07日 09:40:03 作者:laozuo
這篇文章主要介紹了微信小程序發(fā)送短信驗證碼完整實例,實現(xiàn)發(fā)送短信驗證碼,帶60秒倒計時功能,無需服務(wù)器端,非常具有實用價值,需要的朋友可以參考下
微信小程序注冊完整實例,發(fā)送短信驗證碼,帶60秒倒計時功能,無需服務(wù)器端。效果圖:

代碼:
index.wxml
<!--index.wxml-->
<view class="container">
<view class='row'>
<input placeholder='請輸入姓名' bindinput='bindNameInput'/>
</view>
<view class='row'>
<input placeholder='請輸入手機號' bindinput='bindPhoneInput'/>
</view>
<view class='row'>
<input placeholder='請輸驗證碼' bindinput='bindCodeInput' style='width:70%;'/>
<button class='codeBtn' bindtap='getCode' hidden='{{hidden}}' disabled='{{btnDisabled}}'>{{btnValue}}</button>
</view>
<view>
<button class='save' bindtap='save' >保存</button>
</view>
</view>
index.js
//index.js
var zhenzisms = require('../../utils/zhenzisms.js');
//獲取應(yīng)用實例
const app = getApp();
Page({
data: {
hidden: true,
btnValue:'',
btnDisabled:false,
name: '',
phone: '',
code: '',
second: 60
},
onLoad: function () {
},
//姓名輸入
bindNameInput(e) {
this.setData({
name: e.detail.value
})
},
//手機號輸入
bindPhoneInput(e) {
console.log(e.detail.value);
var val = e.detail.value;
this.setData({
phone: val
})
if(val != ''){
this.setData({
hidden: false,
btnValue: '獲取驗證碼'
})
}else{
this.setData({
hidden: true
})
}
},
//驗證碼輸入
bindCodeInput(e) {
this.setData({
code: e.detail.value
})
},
//獲取短信驗證碼
getCode(e) {
console.log('獲取驗證碼');
var that = this;
zhenzisms.client.init('https://sms_developer.zhenzikj.com', 'appId', 'appSecret');
zhenzisms.client.send(function (res) {
if(res.data.code == 0){
that.timer();
return ;
}
wx.showToast({
title: res.data.data,
icon: 'none',
duration: 2000
})
}, '15801636347', '驗證碼為:3322');
},
timer: function () {
let promise = new Promise((resolve, reject) => {
let setTimer = setInterval(
() => {
var second = this.data.second - 1;
this.setData({
second: second,
btnValue: second+'秒',
btnDisabled: true
})
if (this.data.second <= 0) {
this.setData({
second: 60,
btnValue: '獲取驗證碼',
btnDisabled: false
})
resolve(setTimer)
}
}
, 1000)
})
promise.then((setTimer) => {
clearInterval(setTimer)
})
},
//保存
save(e) {
console.log('姓名: ' + this.data.name);
console.log('手機號: ' + this.data.phone);
console.log('驗證碼: ' + this.data.code);
//省略提交過程
}
})
index.wxss
/**index.wxss**/
page{
height: 100%;
width: 100%;
background: linear-gradient(#5681d7, #486ec3);
display: flex;
flex-direction: column;
}
.container{
display: flex;
flex-direction: column;
justify-content: space-around;
width: 90%;
margin: 50rpx auto;
}
.row{
position: relative;
height: 80rpx;
width: 100%;
border-radius: 10rpx;
background: #fff;
margin-bottom: 20rpx;
padding-left: 20rpx;
box-sizing: border-box;
}
.row input{
width: 100%;
height:100%;
}
.codeBtn{
position: absolute;
right: 0;
top: 0;
color: #bbb;
width: 30%;
font-size: 26rpx;
height: 80rpx;
line-height: 80rpx;
}
.subBtn{
width: 200rpx;
height: 80rpx;
background: #fff;
color: #000;
border-radius: 50rpx;
line-height: 80rpx;
}
完整下載: 下載
詳情參考: http://smsow.zhenzikj.com/doc/sdk.html
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
經(jīng)典面試題之JavaScript?for循環(huán)(var?let)
如果你也在面試找工作,那么也一定遇到過這道for循環(huán)打印結(jié)果的題,下面我們來探討下,對經(jīng)典面試題之js?for循環(huán)相關(guān)知識感興趣的朋友跟隨小編一起看看吧2023-10-10
Javascript正則控制文本框只能輸入整數(shù)或浮點數(shù)
這篇文章主要介紹Javascript正則如何控制文本框只能輸入整數(shù)或浮點數(shù),需要的朋友可以參考下2014-09-09
JavaScript實現(xiàn)可拖拽的拖動層Div實例
這篇文章主要介紹了JavaScript實現(xiàn)可拖拽的拖動層Div的方法,拖拽頁面中的div塊可實現(xiàn)div塊按照拖動軌跡移動的效果,涉及javascript鼠標(biāo)事件、頁面元素樣式結(jié)合事件函數(shù)動態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2015-08-08
淺析為什么a="abc" 不等于 a=new String("abc")
這篇文章主要介紹了為什么a="abc" 不等于 a=new String("abc"),需要的朋友可以參考下2017-10-10

