import verification from '@/utils/test'; export default { data(){ return { timeConfig:{ default:'获取验证码', format:'还剩', afterFormat:'s', time:60 }, timeFormat:'' } }, methods:{ // 获取验证码 triggerTimeDate(item,data){ if (this.timeFormat) return; let resultData = undefined; for (let i=0,count=data.length;i{ data.type = item.codeType; this.$request({ url:'login/send_sms', data, message:2, failMessage:true }).catch(()=>{ // 如果发生错误触发 this.endTimeDate = 0 }); return this.triggerTimeType(); }); } }, // 触发倒计时 triggerTimeType(){ clearTimeout(this._time_format); if (this.endTimeDate === undefined) { this.endTimeDate = this.getTimeDate() + this.timeConfig.time; } let diff = this.endTimeDate - this.getTimeDate(); if (diff <= 0) { this.timeFormat = ''; this.endTimeDate = undefined; } else { this.timeFormat = this.timeConfig.format + diff + this.timeConfig.afterFormat; this._time_format = setTimeout(()=> this.triggerTimeType(),1000); } }, // 获取时间 getTimeDate(){ return Math.ceil(new Date().getTime() / 1000); } }, beforeUnmount() { clearTimeout(this._time_format); } }