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.
93 lines
2.3 KiB
93 lines
2.3 KiB
import { ref, watch, onUnmounted } from 'vue'
|
|
import { storeToRefs } from 'pinia'
|
|
import { useRootStore } from '@/stores/root'
|
|
|
|
export const useHandleScreen = (callback: () => void) => {
|
|
const MIN_TIME = 0
|
|
const MAX_TIME = 5
|
|
const CHECK_TIME = 1000
|
|
const DELAY_CHECK_TIME = 400
|
|
const title = '即将进入屏幕保护'
|
|
|
|
const store = useRootStore()
|
|
const { nativeMethods, backTime } = storeToRefs(store)
|
|
|
|
const toWallpaperTime = ref(backTime.value[1]) //回屏保的时间
|
|
const isWallpaper = ref(false) //回首页是否已经跑完
|
|
const showCountDownDialog = ref(false)
|
|
let toWallpaperInterval: any
|
|
let delayCheckRoutePathTimer: any
|
|
|
|
function sleepToWallpaper() {
|
|
isWallpaper.value = true
|
|
return new Promise<void>(resolve => {
|
|
toWallpaperInterval = setInterval(() => {
|
|
toWallpaperTime.value--
|
|
if (toWallpaperTime.value <= MIN_TIME) {
|
|
clearInterval(toWallpaperInterval)
|
|
toWallpaperTime.value = backTime.value[1]
|
|
isWallpaper.value = false
|
|
resolve()
|
|
}
|
|
}, CHECK_TIME)
|
|
})
|
|
}
|
|
|
|
function clearTimers() {
|
|
clearInterval(toWallpaperInterval)
|
|
clearTimeout(delayCheckRoutePathTimer)
|
|
}
|
|
|
|
//跳转屏保挂载到全局 原生app使用
|
|
window.leaveScreenSave = () => {
|
|
checkHandleScreen()
|
|
}
|
|
|
|
function checkHandleScreen() {
|
|
toWallpaperTime.value = backTime.value[1]
|
|
|
|
clearTimers()
|
|
|
|
delayCheckRoutePathTimer = setTimeout(async () => {
|
|
try {
|
|
//没有屏保
|
|
if (backTime.value[1] < 0) {
|
|
return
|
|
}
|
|
await sleepToWallpaper()
|
|
callback()
|
|
nativeMethods.value?.goScreenSave()
|
|
} catch (error) {
|
|
clearTimers()
|
|
}
|
|
}, DELAY_CHECK_TIME)
|
|
}
|
|
|
|
//监听时间 大于等于0且小于等于5时显示弹框
|
|
const stopHandler = watch(toWallpaperTime, wallpaperTime => {
|
|
showCountDownDialog.value = wallpaperTime >= MIN_TIME && wallpaperTime <= MAX_TIME
|
|
})
|
|
|
|
const stopCheckHandleScreenHandler = watch(
|
|
backTime,
|
|
() => {
|
|
checkHandleScreen()
|
|
},
|
|
{ immediate: true }
|
|
)
|
|
|
|
onUnmounted(() => {
|
|
clearTimers()
|
|
stopHandler()
|
|
stopCheckHandleScreenHandler()
|
|
toWallpaperInterval = null
|
|
delayCheckRoutePathTimer = null
|
|
})
|
|
|
|
return {
|
|
checkHandleScreen,
|
|
showCountDownDialog,
|
|
title,
|
|
toWallpaperTime
|
|
}
|
|
}
|
|
|