activity-as-of.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 currentDate = ref(new Date())
  6. const interval = ref()
  7. const status = computed(() => {
  8. if (dayjs(currentDate.value).isBefore(dayjs(props.startAt))) {
  9. return 'waiting'
  10. } else if (
  11. dayjs(currentDate.value).isAfter(dayjs(props.startAt)) &&
  12. dayjs(currentDate.value).isBefore(dayjs(props.endAt))
  13. ) {
  14. return 'running'
  15. } else {
  16. return 'overdue'
  17. }
  18. })
  19. const time = computed(() =>
  20. dayjs({ waiting: props.startAt, running: props.endAt }[status.value]).diff(
  21. new Date(),
  22. 'millisecond',
  23. ),
  24. )
  25. onMounted(() => {
  26. interval.value = setInterval(() => {
  27. currentDate.value = new Date()
  28. }, 1000)
  29. })
  30. onUnmounted(() => {
  31. clearInterval(interval.value)
  32. })
  33. </script>
  34. <template>
  35. <div>
  36. <wd-count-down v-if="status!=='running'" :time="time" @finish="emits('end')">
  37. <template #default="{ current }">
  38. <div
  39. class="flex items-center text-white text-xs font-normal font-['PingFang_SC'] leading-normal whitespace-nowrap"
  40. >
  41. <div v-if="current.days" class="text-[#ef4343]">
  42. {{ current.days }}
  43. </div>
  44. <span v-if="current.days">天</span>
  45. <div v-if="current.hours && !current.days" class="text-[#ef4343]">
  46. {{ current.hours }}
  47. </div>
  48. <span v-if="current.hours && !current.days">小时</span>
  49. <div v-if="current.minutes && !current.hours && !current.days" class="text-[#ef4343]">
  50. {{ current.minutes }}
  51. </div>
  52. <span v-if="current.minutes && !current.hours && !current.days">分钟</span>
  53. <div
  54. v-if="current.seconds && !current.minutes && !current.hours && !current.days"
  55. class="text-[#ef4343]"
  56. >
  57. {{ current.seconds }}
  58. </div>
  59. <span v-if="current.seconds && !current.minutes && !current.hours && !current.days">
  60. </span>
  61. <span v-if="current.days || current.hours || current.minutes || current.seconds">
  62. 后 报名{{ { waiting: '开始', running: '截止' }[status] }}
  63. </span>
  64. <span v-else>加载中...</span>
  65. </div>
  66. </template>
  67. </wd-count-down>
  68. </div>
  69. </template>