App.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import '@/assets/styles/base.css'
  2. // 关于路由
  3. import React, { useCallback, useEffect, useRef } from 'react'
  4. import { Router, Route, Switch } from 'react-router-dom'
  5. import history from './utils/history'
  6. import SpinLoding from './components/SpinLoding'
  7. import NotFound from '@/components/NotFound'
  8. import store from './store'
  9. import { baseURL, myData } from './utils/http'
  10. // import Vconsole from 'vconsole'
  11. // new Vconsole()
  12. // import { isLoc, myData } from './utils/http'
  13. const A1home = React.lazy(() => import('./pages/A1home'))
  14. const A1_1base = React.lazy(() => import('./pages/A1_1base'))
  15. const A2visit = React.lazy(() => import('./pages/A2visit'))
  16. const A3banquet = React.lazy(() => import('./pages/A3banquet'))
  17. const A4dance = React.lazy(() => import('./pages/A4dance'))
  18. const A5chef = React.lazy(() => import('./pages/A5chef'))
  19. const A6plow = React.lazy(() => import('./pages/A6plow'))
  20. const A7end = React.lazy(() => import('./pages/A7end'))
  21. const B1more = React.lazy(() => import('./pages/B1more'))
  22. // 云起图仙居 等热点集合页面
  23. const B2hots = React.lazy(() => import('./pages/B2hots'))
  24. // 云气图
  25. const B3yun = React.lazy(() => import('./pages/B3yun'))
  26. // 仙居
  27. const B4xian = React.lazy(() => import('./pages/B4xian'))
  28. // unity页面
  29. const C1unity = React.lazy(() => import('./pages/C1unity'))
  30. const C2unityEnd = React.lazy(() => import('./pages/C2unityEnd'))
  31. const Text = React.lazy(() => import('./pages/Text'))
  32. declare global {
  33. //设置全局属性
  34. interface Window {
  35. //window对象属性
  36. isHH: boolean //加入对象
  37. unityOpenHot: (index: number) => void
  38. unityBack: () => void
  39. }
  40. }
  41. let tempW = document.documentElement.clientWidth
  42. let tempH = document.documentElement.clientHeight
  43. let tempMax = tempW >= tempH ? tempW : tempH
  44. let tempMin = tempW >= tempH ? tempH : tempW
  45. const pageBi = Math.round(Number((tempMax / tempMin).toFixed(2)))
  46. // 设计图按照 844 X 390 来开发
  47. const planSize = {
  48. width: 844,
  49. height: Math.round(Number((844 / pageBi).toFixed(0)))
  50. }
  51. export default function App() {
  52. useEffect(() => {
  53. // 打包环境 刷新页面从 首页 开始
  54. // if (!isLoc && myData.isLdong) {
  55. // if (window.location.hash !== '#/') {
  56. // window.location.href = window.location.origin
  57. // }
  58. // }
  59. }, [])
  60. // 根元素
  61. const rootRef = useRef<any>(null)
  62. const pageFullChangeFu = useCallback(() => {
  63. let width = document.documentElement.clientWidth
  64. let height = document.documentElement.clientHeight
  65. let isHHTemp = false
  66. if (width >= height) {
  67. //横屏
  68. isHHTemp = true
  69. const sizeW = width / planSize.width
  70. let sizeH = height / planSize.height
  71. let moveX = (planSize.width - width) / 2
  72. let moveY = (planSize.height - height) / 2
  73. if (width >= planSize.width) moveX = 0
  74. rootRef.current.style.left = '0'
  75. rootRef.current.style.transform = `translate(${-moveX}px,${-moveY}px) scale(${sizeW},${sizeH}) rotate(0deg)`
  76. rootRef.current.style.transformOrigin = 'center'
  77. store.dispatch({
  78. type: 'layout/style',
  79. payload: {
  80. width: planSize.width,
  81. moveX: -moveX,
  82. moveY: -moveY,
  83. sizeW,
  84. sizeH
  85. }
  86. })
  87. } else {
  88. // 竖屏
  89. isHHTemp = false
  90. const temp = width
  91. width = height
  92. height = temp
  93. const sizeW = width / planSize.width
  94. let sizeH = height / planSize.height
  95. rootRef.current.style.left = '100%'
  96. rootRef.current.style.transform = `rotate(90deg) scale(${sizeW},${sizeH})`
  97. rootRef.current.style.transformOrigin = 'left top'
  98. }
  99. // 横竖屏变化的时候 刷新页面
  100. // if (window.isHH !== isHHTemp) {
  101. // window.location.reload()
  102. // }
  103. store.dispatch({ type: 'layout/isHH', payload: isHHTemp })
  104. }, [])
  105. const audioPlayFu = useCallback(() => {
  106. // 播放背景音乐
  107. const audioDom: HTMLAudioElement = document.querySelector('#bgMp3')!
  108. if (audioDom) {
  109. if (audioDom.paused) audioDom.play()
  110. else rootRef.current.removeEventListener('click', audioPlayFu)
  111. }
  112. }, [])
  113. useEffect(() => {
  114. rootRef.current = document.querySelector('#root')
  115. rootRef.current.style.width = planSize.width + 'px'
  116. rootRef.current.style.height = planSize.height + 'px'
  117. pageFullChangeFu()
  118. window.addEventListener('resize', pageFullChangeFu, false)
  119. // 全局点击都播放背景音乐
  120. if (myData.isLdong) rootRef.current.addEventListener('click', audioPlayFu)
  121. }, [audioPlayFu, pageFullChangeFu])
  122. return (
  123. <>
  124. {/* 关于路由 */}
  125. <Router history={history}>
  126. <React.Suspense fallback={<SpinLoding />}>
  127. <Switch>
  128. <Route path='/' component={A1home} exact />
  129. <Route path='/base' component={A1_1base} exact />
  130. <Route path='/visit' component={A2visit} exact />
  131. <Route path='/banquet' component={A3banquet} exact />
  132. <Route path='/dance' component={A4dance} exact />
  133. <Route path='/chef' component={A5chef} exact />
  134. <Route path='/plow' component={A6plow} exact />
  135. <Route path='/end' component={A7end} exact />
  136. <Route path='/more' component={B1more} exact />
  137. <Route path='/hots' component={B2hots} exact />
  138. <Route path='/yun' component={B3yun} exact />
  139. <Route path='/xian' component={B4xian} exact />
  140. <Route path='/unity/:id' component={C1unity} exact />
  141. <Route path='/unend/:key' component={C2unityEnd} exact />
  142. <Route path='/text' component={Text} exact />
  143. <Route path='*' component={NotFound} />
  144. </Switch>
  145. </React.Suspense>
  146. </Router>
  147. {/* 背景音乐 */}
  148. <audio id='bgMp3' loop style={{ display: 'none' }} src={baseURL + 'home/bg.mp3'}></audio>
  149. </>
  150. )
  151. }