zhou zhou
4 小时以前 fec285d150b377d004e47f0973d298b92fe4c711
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { useTimeoutFn, useIntervalFn, useDateFormat } from '@vueuse/core'
import { storeToRefs } from 'pinia'
import { computed } from 'vue'
import { useSettingStore } from '@/store/modules/setting'
import { mittBus } from '@/utils/sys'
import { festivalConfigList } from '@/config/modules/festival'
const FESTIVAL_CONFIG = {
  /** 初始延迟(毫秒) */
  INITIAL_DELAY: 300,
  /** 烟花播放间隔(毫秒) */
  FIREWORK_INTERVAL: 1e3,
  /** 文本显示延迟(毫秒) */
  TEXT_DELAY: 2e3,
  /** 默认烟花播放次数 */
  DEFAULT_FIREWORKS_COUNT: 3
}
function useCeremony() {
  const settingStore = useSettingStore()
  const { holidayFireworksLoaded, isShowFireworks } = storeToRefs(settingStore)
  let fireworksInterval = null
  const isDateInRange = (currentDate, festivalDate, festivalEndDate) => {
    if (!festivalEndDate) {
      return currentDate === festivalDate
    }
    const current = new Date(currentDate)
    const start = new Date(festivalDate)
    const end = new Date(festivalEndDate)
    return current >= start && current <= end
  }
  const currentFestivalData = computed(() => {
    const currentDate = useDateFormat(/* @__PURE__ */ new Date(), 'YYYY-MM-DD').value
    return festivalConfigList.find((item) => isDateInRange(currentDate, item.date, item.endDate))
  })
  const updateFestivalDate = () => {
    settingStore.setFestivalDate(currentFestivalData.value?.date || '')
  }
  const triggerFirework = () => {
    mittBus.emit('triggerFireworks', currentFestivalData.value?.image)
  }
  const showFestivalText = () => {
    settingStore.setholidayFireworksLoaded(true)
    useTimeoutFn(() => {
      settingStore.setShowFestivalText(true)
      updateFestivalDate()
    }, FESTIVAL_CONFIG.TEXT_DELAY)
  }
  const startFireworksLoop = () => {
    let playedCount = 0
    const count = currentFestivalData.value?.count ?? FESTIVAL_CONFIG.DEFAULT_FIREWORKS_COUNT
    const { pause } = useIntervalFn(() => {
      triggerFirework()
      playedCount++
      if (playedCount >= count) {
        pause()
        showFestivalText()
      }
    }, FESTIVAL_CONFIG.FIREWORK_INTERVAL)
    fireworksInterval = { pause }
  }
  const openFestival = () => {
    if (!currentFestivalData.value || !isShowFireworks.value) {
      return
    }
    const { start } = useTimeoutFn(startFireworksLoop, FESTIVAL_CONFIG.INITIAL_DELAY)
    start()
  }
  const cleanup = () => {
    if (fireworksInterval) {
      fireworksInterval.pause()
      fireworksInterval = null
    }
    settingStore.setShowFestivalText(false)
    updateFestivalDate()
  }
  return {
    openFestival,
    cleanup,
    holidayFireworksLoaded,
    currentFestivalData,
    isShowFireworks
  }
}
export { useCeremony }