微信小程序注册完整实例,发送短信验证码,带60秒倒计时功能,无需服务器端。效果图:
代码:
index.wxml
<!--index.wxml-->
<view class="container">
'row'>
<input placeholder='请输入姓名' bindinput='bindNameInput'/>
</view>
'请输入手机号' 'bindPhoneInput'/>
</view>
'请输验证码' 'bindCodeInput' style='width:70%;'/>
<button 'codeBtn' bindtap='getCode' hidden='{{hidden}}' disabled='{{btndisabled}}'>{{btnValue}}</button>
<view>
'save' 'save' >保存</view>
</view>
index.js
);
//获取应用实例
const app = getApp();
Page({
data: {
hidden: true,btnValue:'',btndisabled:false,name: Box-sizing: border-Box; color: rgb(221,phone: Box-sizing: border-Box; color: rgb(221,code: Box-sizing: border-Box; color: rgb(221,second: 60
},onLoad: function () {
},//姓名输入
bindNameInput(e) {
this.setData({
name: e.detail.value
})
},136);">//手机号输入
bindPhoneInput(e) {
console.log(e.detail.value);
var val = e.detail.value;
this.setData({
phone: val
})
if(val != ''){
this.setData({
hidden: teral" style="Box-sizing: border-Box; color: rgb(0,btnValue: '获取验证码'
})
}else{
this.setData({
hidden: true
})
}
},136);">//验证码输入
bindCodeInput(e) {
this.setData({
code: e.detail.value
})
},136);">//获取短信验证码
getCode(e) {
console.log('获取验证码');
var that = this;
zhenzisms.client.init('https://sms_developer.zhenzikj.com','appId',68);">'appSecret');
zhenzisms.client.send(function (res) {
if(res.data.code == 0){
that.timer();
return ;
}
wx.showToast({
title: res.data.data,icon: 'none',duration: 2000
})
},68);">'15801636347',68);">'验证码为: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,68);">'获取验证码',128);">false
})
resolve(setTimer)
}
},1000)
})
promise.then((setTimer) => {
clearInterval(setTimer)
})
},136);">//保存
save(e) {
console.log('姓名: ' + this.data.name);
console.log('手机号: ' + this.data.phone);
console.log('验证码: ' + this.data.code);
//省略提交过程
}
})
index.wxss
完整下载: 下载