index.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { useEffect, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import { isPc, otherUrl } from '@/utils/http'
  4. import { useParams } from 'react-router-dom'
  5. import history from '@/utils/history'
  6. const obj = {
  7. end: {
  8. pc: 'unity/HeNanMuseum-pc/ManorScene/index.html',
  9. app: 'unity/HeNanMuseum-mobile/ManorScene/index.html'
  10. },
  11. game1: {
  12. pc: 'unity/HeNanMuseum-pc/LinkingGame/index.html',
  13. app: 'unity/HeNanMuseum-mobile/LinkingGame/index.html'
  14. },
  15. game2: {
  16. pc: 'unity/HeNanMuseum-pc/PuzzleGame/index.html',
  17. app: 'unity/HeNanMuseum-mobile/PuzzleGame/index.html'
  18. },
  19. game3: {
  20. pc: 'unity/HeNanMuseum-pc/ThrowGame/index.html',
  21. app: 'unity/HeNanMuseum-mobile/ThrowGame/index.html'
  22. }
  23. }
  24. function C2unityEnd() {
  25. const urlObj: any = useParams()
  26. const [url, setUrl] = useState('')
  27. useEffect(() => {
  28. const key = urlObj.key
  29. console.log(123, key)
  30. const objTemp = Reflect.get(obj, key)
  31. const res = Reflect.get(objTemp, isPc ? 'pc' : 'app')
  32. setUrl(`${otherUrl + res}?T=${Date.now()}`)
  33. }, [urlObj.key])
  34. useEffect(() => {
  35. if (url) {
  36. if (isPc) {
  37. const body = document.querySelector('body')
  38. const iframe = document.createElement('iframe')
  39. iframe.frameBorder = 'none'
  40. iframe.title = '漫游'
  41. iframe.id = 'myIframe'
  42. iframe.src = url
  43. body?.appendChild(iframe)
  44. }
  45. return () => {
  46. const iframeDom = document.querySelector('#myIframe')
  47. iframeDom?.remove()
  48. }
  49. }
  50. }, [url])
  51. useEffect(() => {
  52. window.unityBack = () => {
  53. console.log('从unity点击返回')
  54. // 退出
  55. if (window.location.href.includes('r=ren')) history.replace('/more?r=ren')
  56. else if (window.location.href.includes('v=v2')) history.replace('/visit2?v=v2')
  57. else history.go(-1)
  58. }
  59. }, [])
  60. return (
  61. <div className={styles.C2unityEnd}>
  62. {!isPc && url ? (
  63. <iframe className='C2unity' title='陶庄园' src={url} frameBorder='0'></iframe>
  64. ) : null}
  65. </div>
  66. )
  67. }
  68. const MemoC2unityEnd = React.memo(C2unityEnd)
  69. export default MemoC2unityEnd