123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { ref, Ref } from 'vue'
- interface CountdownTimerOptions {
- delay?: number
- cacheKey?: string
- }
- /**
- * 倒计时
- * - Ref countdown、isCounting
- * - Fn start、stop
- */
- class CountdownTimer {
- delay: number
- cacheKey: string
- isCounting: Ref<boolean>
- countdown: Ref<number>
- countdownTimer: any
- constructor({ delay, cacheKey }: CountdownTimerOptions = {}) {
- this.delay = delay || 59
- this.cacheKey = cacheKey || 'sms-count-timer'
- this.isCounting = ref(false)
- this.countdown = ref(0)
- this.countdownTimer = null
- this.recoverCacheCountdown()
- }
- /**
- * 对外提供调用
- */
- getInstance() {
- return {
- countdown: this.countdown,
- isCounting: this.isCounting,
- start: this.startCountdown.bind(this),
- stop: this.stopTimer.bind(this)
- }
- }
- /**
- * 开始倒计时
- */
- startCountdown() {
- if (!this.isCounting.value) {
- if (this.cacheKey) {
- localStorage.setItem(this.cacheKey, Date.now().toString())
- }
- this.countdown.value = this.delay
- this.startTimer()
- }
- }
- /**
- * 恢复缓存倒计时
- */
- recoverCacheCountdown() {
- if (this.cacheKey && localStorage.getItem(this.cacheKey)) {
- const cacheDelay = this.calcCountdown(
- parseInt(localStorage.getItem(this.cacheKey))
- )
- if (cacheDelay) {
- this.countdown.value = cacheDelay
- this.startTimer()
- }
- }
- }
- /**
- * 计算倒计时,如果超过60秒则不再计算
- * @param start - 需要与当前时间对比的开始时间,ms
- */
- calcCountdown(start) {
- const startTime = parseInt(start) || Date.now()
- const diffTime = Math.ceil((Date.now() - startTime) / 1000)
- if (diffTime > 0 && diffTime <= this.delay) {
- return this.delay - diffTime
- }
- return 0
- }
- /**
- * 开始倒计时
- */
- startTimer() {
- this.isCounting.value = true
- this.countdownTimer = setInterval(() => {
- this.countdown.value--
- if (this.countdown.value <= 0) {
- this.isCounting.value = false
- clearInterval(this.countdownTimer)
- if (this.cacheKey) {
- localStorage.removeItem(this.cacheKey)
- }
- }
- }, 1000)
- }
- /**
- * 停止倒计时
- */
- stopTimer() {
- clearInterval(this.countdownTimer)
- }
- }
- export default CountdownTimer
|