hn-hlw-app/components/consult-countdown.vue
2026-07-27 11:26:39 +08:00

78 lines
1.7 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="countdown-container">
<p class="countdown-text">
问诊进行中本次问诊剩余时间 {{ formattedTime }}
</p>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, computed, watch } from "vue";
const props = defineProps({
expireTime: {
type: Number,
required: true,
},
});
const emit = defineEmits(["countdown-finished"]);
const timeLeft = ref(calculateTimeLeft());
let intervalId = null;
function calculateTimeLeft() {
const now = Date.now();
const timeDifference = props.expireTime - now;
return timeDifference > 0 ? Math.floor(timeDifference / 1000) : 0;
}
const formattedTime = computed(() => {
const hours = Math.floor(timeLeft.value / 3600);
const minutes = Math.floor((timeLeft.value % 3600) / 60);
const seconds = timeLeft.value % 60;
return `${hours}${minutes < 10 ? "0" : ""}${minutes}${
seconds < 10 ? "0" : ""
}${seconds}`;
});
const updateCountdown = () => {
if (timeLeft.value > 0) {
timeLeft.value -= 1;
} else {
clearInterval(intervalId);
emit("countdown-finished");
}
};
onMounted(() => {
intervalId = setInterval(updateCountdown, 1000);
});
onUnmounted(() => {
clearInterval(intervalId);
});
watch(
() => props.expireTime,
(newExpireTime) => {
timeLeft.value = calculateTimeLeft();
clearInterval(intervalId);
intervalId = setInterval(updateCountdown, 1000);
}
);
</script>
<style scoped>
.countdown-container {
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0; /* 灰色背景 */
padding: 10px;
border-radius: 5px;
}
.countdown-text {
color: #ff0000; /* 红色文字 */
font-size: 16px;
}
</style>