微信小程序获取验证码
JS 部分
/**
* 页面的初始数据
*/
data: {
getmsg: "获取验证码",
sendmsg: "sendmsg",
mobile: "",
},
/**
* 获取短信验证码
*/
sendmessg: function (e) {
var that = this;
var mobile = that.data.mobile;
if (mobile) {
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
if (!myreg.test(mobile)) {
wx.showToast({
title: '手机号有误!',
icon: "none",
duration: 1000
});
return false;
} else {
var timer = 1;
if (timer == 1) {
timer = 0;
var time = 60;
that.setData({
sendmsg: "Sendafter",
})
var inter = setInterval(function () {
that.setData({
getmsg: time + "s后重新发送",
})
time--
if (time < 0) {
timer = 1
clearInterval(inter);
that.setData({
sendmsg: "sendmsg",
getmsg: "获取验证码",
})
}
}, 1000)
}
//调用获取短信息接口
that.bindright(e);
}
} else {
wx.showToast({
title: '手机号码不能为空',
icon: "none",
duration: 1000
});
}
},
WXML 部分 :
<button class="submit" bindtap="{{sendmsg == 'Sendafter'?'':'sendmessg'}}" >
{{getmsg}}
</button>