import { computed, onUnmounted, ref } from 'vue' import dayjs from 'dayjs' import duration from 'dayjs/plugin/duration' import { onUnload } from '@dcloudio/uni-app' import { toDouble } from '@/utils/common' import { useServiceStore } from '@/store/modules/service' dayjs.extend(duration) export interface INextNode { countDownInSeconds: string firstAidId: number nextStepCode: string nextStepName: string nextStepType: string // 0 仅显示 1 弹窗询问是否进入下个环节 } export function useNextNode() { const serviceStore = useServiceStore() const nextNode = ref(null) const currentFirstAidId = computed(() => serviceStore.currentFirstAidId) let timer: number async function getNextNode() { if (!currentFirstAidId.value) { uni.$u.toast('没有急救id信息') return } try { nextNode.value = await uni.$u.api.getNextNode(currentFirstAidId.value) } catch (error) { nextNode.value = null uni.$u.alertError(error) } } // 距下一个节点的剩余时间 const leftTime = computed(() => { const res = { hours: '00', minutes: '00', seconds: '' } if (!nextNode.value?.countDownInSeconds) return res const totalSeconds = Number(nextNode.value.countDownInSeconds) res.seconds = toDouble(dayjs.duration(totalSeconds).seconds()) res.minutes = toDouble(dayjs.duration(totalSeconds).minutes()) res.hours = toDouble(dayjs.duration(totalSeconds).hours()) return res }) // 循环查下个节点 function intervalGetNextCode() { getNextNode() timer && clearInterval(timer) timer = setInterval(() => { getNextNode() }, 1000) } onUnmounted(() => { console.log('on unmounted') timer && clearInterval(timer) timer = 0 }) onUnload(() => { console.log('on unload') timer && clearInterval(timer) timer = 0 }) return { nextNode, getNextNode, intervalGetNextCode, leftTime } }