1、HTML
<button bindtap='deliver' wx:if="{{ verifySend }}" hover-class='cl35'>获取验证码</button>
<button class='cl99' hover-class='cl35' wx:else>重新获取({{ countDown }})</button>
2、JS
// 手机号
mobile: '',
// 验证码
verify: '',
// 是否获取验证码
verifySend: true,
// 倒计时
countDown: 60,
// 获取验证码
deliver() {
let that = this
let {
mobile,
settime
} = that.data
let myreg = /^1[0-9]{10}$/
if (!mobile) {
$toast('请输入手机号码')
} else if (!myreg.test(mobile)) {
$toast('手机号格式输入错误')
} else {
let getCodeParams = { mobile}
// 发生验证码
$get(`/login/getVerificationCode`, getCodeParams, true).then(res => {
$toast('短信验证码已发送,请注意查收', 'none', 2000)
let time = 59
settime = setInterval(() => {
that.setData({
countDown: time--
})
if (that.data.countDown == 0) {
clearInterval(settime)
that.setData({
verifySend: true,
settime,
countDown: 60
})
}
}, 1000)
that.setData({
settime,
verifySend: false
})
}).catch(err => {
let { code, msg } = err
if(code == $WARNING){
$toast(msg,'none',2000)
}
})
}
},