activity-count-down.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <script setup lang="ts">
  2. import dayjs from 'dayjs'
  3. const props = defineProps<{ startAt?: string | number; endAt?: string | number }>()
  4. const emits = defineEmits<{ end: [] }>()
  5. const status = computed(() => {
  6. // 如果当前时间小于开始时间返回等待中,如果当前时间大于等于开始时间小于等于结束时间返回进行中,当前时间大于结束时间返回已结束
  7. const now = new Date()
  8. if (dayjs(now).isBefore(dayjs(props.startAt))) {
  9. return 'waiting'
  10. } else if (dayjs(now).isAfter(dayjs(props.startAt)) && dayjs(now).isBefore(dayjs(props.endAt))) {
  11. return 'running'
  12. } else {
  13. return 'overdue'
  14. }
  15. })
  16. const time = ref(
  17. dayjs({ waiting: props.startAt, running: props.endAt }[status.value]).diff(
  18. new Date(),
  19. 'millisecond',
  20. ),
  21. )
  22. </script>
  23. <template>
  24. <div>
  25. <wd-count-down :time="time" @finish="emits('end')">
  26. <template #default="{ current }">
  27. <div v-if="time" class="flex items-center gap-1.25 text-black/40 text-sm">
  28. <div>距{{ { waiting: '报名开始', running: '报名结束' }[status] }}还有</div>
  29. <div
  30. v-if="current.days"
  31. class="w-4 h-4 bg-black/90 rounded text-white text-2.5 flex items-center justify-center"
  32. >
  33. {{ current.days }}
  34. </div>
  35. <span v-if="current.days" class="custom-count-down-colon">天</span>
  36. <div
  37. class="w-4 h-4 bg-black/90 rounded text-white text-2.5 flex items-center justify-center"
  38. >
  39. {{ current.hours }}
  40. </div>
  41. <span class="custom-count-down-colon">时</span>
  42. <div
  43. class="w-4 h-4 bg-black/90 rounded text-white text-2.5 flex items-center justify-center"
  44. >
  45. {{ current.minutes }}
  46. </div>
  47. <span class="custom-count-down-colon">分</span>
  48. <div
  49. v-if="!current.days"
  50. class="w-4 h-4 bg-black/90 rounded text-white text-2.5 flex items-center justify-center"
  51. >
  52. {{ current.seconds }}
  53. </div>
  54. <span v-if="!current.days" class="custom-count-down-colon">秒</span>
  55. </div>
  56. </template>
  57. </wd-count-down>
  58. </div>
  59. </template>