index.vue 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <div ref="el" />
  3. <FireWorkListVue ref="firesRef" :center="center" :raduis="raduis" />
  4. <MusicOnce ref="bingoRef" :src="bingoMusic" />
  5. <MusicOnce ref="failRef" :src="failMusic" />
  6. <MusicOnce ref="successRef" :src="successMusic" />
  7. </template>
  8. <script setup>
  9. import { onBeforeUnmount, onMounted, ref } from 'vue'
  10. import { useRouter } from 'vue-router'
  11. import FireWorkListVue from '@/components/FireWorkList.vue'
  12. import game from './game.js'
  13. import { gameOver, gameStart } from '@/utils/api.js'
  14. // // 修复一个 two-js bug ,当页面返回的时候, 不能正常渲染
  15. // // 所以这边判断如果第二次进入页面就强制刷新
  16. // const loadSign = localStorage.getItem('loadSign');
  17. // if (loadSign) {
  18. // localStorage.removeItem('loadSign');
  19. // window.location.href = window.location.href;
  20. // }
  21. const el = ref()
  22. const destroyRef = ref()
  23. const firesRef = ref()
  24. const router = useRouter()
  25. const raduis = ref(150)
  26. const path = window.location.origin + window.location.pathname
  27. const bingoMusic = `${path}music/bingo.mp3`
  28. const failMusic = `${path}music/fail.mp3`
  29. const successMusic = `${path}music/bingo.mp3`
  30. const bingoRef = ref()
  31. const failRef = ref()
  32. const successRef = ref()
  33. const center = {
  34. x: document.body.offsetWidth / 2,
  35. y: document.body.offsetHeight / 2
  36. }
  37. const gameInit = () => {
  38. destroyRef.value = game({
  39. el: el.value,
  40. center,
  41. onError: () => {
  42. failRef.value.play()
  43. const t = setTimeout(() => {
  44. localStorage.setItem('loadSign', '1')
  45. router.push('/OverPage?type=lose')
  46. clearTimeout(t)
  47. }, 1000)
  48. },
  49. onSuccess: () => {
  50. gameOver()
  51. successRef.value.play()
  52. firesRef.value.toggle()
  53. // alert('你真牛逼')
  54. const t = setTimeout(() => {
  55. localStorage.setItem('loadSign', '1')
  56. router.push('/OverPage?type=win')
  57. clearTimeout(t)
  58. }, 2000)
  59. },
  60. onBingo: () => {
  61. bingoRef.value.play()
  62. }
  63. })
  64. raduis.value = destroyRef.value.raduis
  65. }
  66. onMounted(() => {
  67. gameStart()
  68. gameInit()
  69. })
  70. onBeforeUnmount(() => {
  71. if (destroyRef.value) {
  72. destroyRef.value.destroy()
  73. }
  74. })
  75. </script>
  76. <style>
  77. html,
  78. body,
  79. #app {
  80. height: 100%;
  81. margin: 0;
  82. padding: 0;
  83. }
  84. </style>