<view class="login-input-box">
<input type="text" v-model="checknum"
class="uni-input common-input forget-input"
placeholder="请输入验证码" />
<view class="forget u-f-ajc login-font-color yanzhengma"
@tap="getCheckNum">
<view class="u-f-ajc">{{!codetime?'获取验证码':codetime+' s'}}</view>
</view>
</view>

1.codetime


data() {
return {
status:false,//false代表账号密码登录,true代表手机验证码登录
disabled:true,
loading:false,
username:"",
password:"",
phone:"",
checknum:"",
codetime:0,
}
},

2.短信发送成功处理倒计时,
进入的时候需要判断倒计时是否结束。


// 获取验证码
async getCheckNum(){
if(this.codetime > 0)return;
// 验证手机号合法性
if(!this.isPhone()){
return uni.showToast({ title: '请输入正确的手机号码', icon:"none" });
}
// 请求服务器,发送验证码
let [err,res] = await this.$http.post('/user/sendcode',{
phone:this.phone
});
// 请求失败处理
this.$http.errorCheck(err,res);
if (res.data.errorCode === 30001) return;
// 发送成功,开启倒计时
this.codetime=60;
let timer=setInterval(()=>{
this.codetime--;
if(this.codetime < 1){
clearInterval(timer);
this.codetime=0;
}
},1000);
},