You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.3 KiB
110 lines
3.3 KiB
import { ref, computed, onMounted, watch } from 'vue'
|
|
import { useRouter } from 'vue-router'
|
|
import { getBackTime } from '@/http/api/base'
|
|
import { useLogout } from '@/composables/useLogout'
|
|
import { isAndroid } from '@/utils/utils'
|
|
|
|
export const useHandleScreen = (callback: () => void) => {
|
|
const MIN_TIME = 0
|
|
const MAX_TIME = 5
|
|
const CHECK_TIME = 1000
|
|
const DELAY_CHECK_TIME = 400
|
|
const _isAndroid = isAndroid()
|
|
|
|
const router = useRouter()
|
|
const { logout, resetClickNumber, setLogout, addTotalClick } = useLogout()
|
|
|
|
const totalTime = ref<[number, number]>([60, 60]) //总时间
|
|
const toIndexTime = ref(60) //回首页的时间
|
|
const toWallpaperTime = ref(60) //回屏保的时间
|
|
const isWallpaper = ref(false) //回首页是否已经跑完
|
|
const showCountDownDialog = ref(false)
|
|
const title = computed(() => (isWallpaper.value ? '即将进入屏幕保护' : '即将进入首页'))
|
|
|
|
const toIndexInterval = ref()
|
|
const toWallpaperInterval = ref()
|
|
const delayCheckRoutePathTimer = ref()
|
|
|
|
function sleepToIndex() {
|
|
isWallpaper.value = false
|
|
return new Promise<void>(resolve => {
|
|
toIndexInterval.value = setInterval(() => {
|
|
toIndexTime.value--
|
|
if (toIndexTime.value <= MIN_TIME) {
|
|
clearInterval(toIndexInterval.value)
|
|
toIndexTime.value = totalTime.value[0]
|
|
resolve()
|
|
}
|
|
}, CHECK_TIME)
|
|
})
|
|
}
|
|
function sleepToWallpaper() {
|
|
isWallpaper.value = true
|
|
return new Promise<void>(resolve => {
|
|
toWallpaperInterval.value = setInterval(() => {
|
|
toWallpaperTime.value--
|
|
if (toWallpaperTime.value <= MIN_TIME) {
|
|
clearInterval(toWallpaperInterval.value)
|
|
toWallpaperTime.value = totalTime.value[1]
|
|
isWallpaper.value = false
|
|
resolve()
|
|
}
|
|
}, CHECK_TIME)
|
|
})
|
|
}
|
|
|
|
async function checkHandleScreen(e: TouchEvent) {
|
|
!_isAndroid && addTotalClick(e)
|
|
toIndexTime.value = totalTime.value[0]
|
|
toWallpaperTime.value = totalTime.value[1]
|
|
|
|
clearInterval(toIndexInterval.value)
|
|
clearInterval(toWallpaperInterval.value)
|
|
clearTimeout(delayCheckRoutePathTimer.value)
|
|
|
|
delayCheckRoutePathTimer.value = setTimeout(async () => {
|
|
if (router.currentRoute.value.fullPath !== '/') {
|
|
await sleepToIndex()
|
|
callback()
|
|
}
|
|
|
|
//没有屏保的话不允许弹出屏保弹框 TODO:后期由容器通知
|
|
if (toWallpaperTime.value === -1) {
|
|
return
|
|
}
|
|
await sleepToWallpaper()
|
|
callback()
|
|
}, DELAY_CHECK_TIME)
|
|
}
|
|
|
|
//监听时间 大于等于0且小于等于5时显示弹框
|
|
watch([toIndexTime, toWallpaperTime], ([indexTime, wallpaperTime]) => {
|
|
if ((indexTime >= MIN_TIME && indexTime <= MAX_TIME) || (wallpaperTime >= MIN_TIME && wallpaperTime <= MAX_TIME)) {
|
|
showCountDownDialog.value = true
|
|
} else {
|
|
showCountDownDialog.value = false
|
|
}
|
|
})
|
|
|
|
onMounted(() => {
|
|
//获取返回首页和进入屏保的具体时间
|
|
getBackTime().then(({ data }) => {
|
|
const noWallpaper = data[1]
|
|
totalTime.value = noWallpaper ? [data[0], -1] : data
|
|
toIndexTime.value = data[0]
|
|
toWallpaperTime.value = noWallpaper ? -1 : data[1]
|
|
})
|
|
})
|
|
return {
|
|
isWallpaper,
|
|
showCountDownDialog,
|
|
title,
|
|
totalTime,
|
|
toIndexTime,
|
|
toWallpaperTime,
|
|
logout,
|
|
checkHandleScreen,
|
|
resetClickNumber,
|
|
setLogout
|
|
}
|
|
}
|
|
|