获取验证码之前,先检测有没有手机号
之前做的时候用的vue, 里面存在的变量大都是取义的,相信各位童鞋都会懂的
getCode: function () {
let self = this;
// if(!this.getCode) return ;
// this.getCode = false;
//验证手机号
let telReg = /^1(3|4|5|6|7|8|9)\d{9}$/;
let phone = self.form.phone;
if('' == phone) { tip('请输入手机号码'); return ;} //tip为一个提示消息函数(做的时候在移动端)
if(!telReg.test(phone)) { tip('请输入正确手机号'); return ;} //正则验证
let time = self.form.countdown;
if(self.form.countdown != 60){ tip('验证码已发送,请稍等...'); return ;}
//发送验证码
axios({
method:"POST",
url: this.path+'sms/'+phone,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
transformRequest: [function(data) {
let ret = '';
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
}
return ret
}]
}).then(data => {
// this.getCode = true;
if(data.data.code == 100000){
tip('验证码已发送...');
###############################################
//敲黑板!!!(这个里面就是倒计时的效果啦) 成功回调,倒计时,为一个自执行函数
(function settime(){
if(time == 0 || self.form.code != ''){
self.form.codeText = '获取验证码';
self.form.countdown = 60;
return ;
}else {
self.form.codeText = time + ' 秒';
self.form.countdown = time;
time-- ;
setTimeout(function(){
settime();
},1000);
}
})()
######################################
//其余为失败回调
}else if(data.data.code == 100006){
tip(data.data.result);
}}).catch(err => console.log(err));
},
demo地址,戳这里~~~
来源:CSDN
作者:yagmdream
链接:https://blog.csdn.net/sinat_40257787/article/details/79242056