import { Injectable } from '@kaokei/use-vue-service'; import { DaoService } from './dao.service'; @Injectable() export class VCodeService { public countdownStatus = 'initial'; // initial-初始状态 pending-请求发送验证码中 countdown-倒计时中 public countdownTime = 0; constructor(public daoService: DaoService) {} public sendVerifiedCode(phone: string) { if (this.countdownStatus === 'initial') { this.countdownStatus = 'pending'; this.daoService .sendVerifiedCode(phone) .then(() => { this.countdownTime = 60; this.countdownStatus = 'countdown'; this.ticktok(); }) .catch(err => { this.countdownStatus = 'initial'; throw err; }); } else { return Promise.resolve(); } } public ticktok() { if (this.countdownTime > 0 && this.countdownTime <= 60) { this.countdownTime--; setTimeout(() => { this.ticktok(); }, 1000); } else { this.countdownTime = 0; this.countdownStatus = 'initial'; } } }