index.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. const obj = {
  6. end: {
  7. pc: 'unity/HeNanMuseum-pc/ManorScene/index.html',
  8. app: 'unity/HeNanMuseum-mobile/ManorScene/index.html'
  9. },
  10. game1: {
  11. pc: 'unity/HeNanMuseum-pc/LinkingGame/index.html',
  12. app: 'unity/HeNanMuseum-mobile/LinkingGame/index.html'
  13. },
  14. game2: {
  15. pc: 'unity/HeNanMuseum-pc/PuzzleGame/index.html',
  16. app: 'unity/HeNanMuseum-mobile/PuzzleGame/index.html'
  17. }
  18. }
  19. function C2unityEnd() {
  20. const urlObj: any = useParams()
  21. const [url, setUrl] = useState('')
  22. useEffect(() => {
  23. const key = urlObj.key
  24. const objTemp = Reflect.get(obj, key)
  25. const url = Reflect.get(objTemp, isPc ? 'pc' : 'app')
  26. setUrl(otherUrl + url)
  27. }, [urlObj.key])
  28. useEffect(() => {
  29. if (url) {
  30. if (isPc) {
  31. const body = document.querySelector('body')
  32. const iframe = document.createElement('iframe')
  33. iframe.frameBorder = 'none'
  34. iframe.title = '漫游'
  35. iframe.id = 'myIframe'
  36. iframe.src = url
  37. body?.appendChild(iframe)
  38. }
  39. return () => {
  40. const iframeDom = document.querySelector('#myIframe')
  41. iframeDom?.remove()
  42. }
  43. }
  44. }, [url])
  45. return (
  46. <div className={styles.C2unityEnd}>
  47. {!isPc && url ? (
  48. <iframe className='C2unity' title='陶庄园' src={url} frameBorder='0'></iframe>
  49. ) : null}
  50. </div>
  51. )
  52. }
  53. const MemoC2unityEnd = React.memo(C2unityEnd)
  54. export default MemoC2unityEnd