App.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { useEffect, useRef, useState } from 'react';
  2. import { eventBus } from './lib/EventBus';
  3. import { pmBridge } from './lib/PostMessageBridge';
  4. import { useSetAtom } from 'jotai'
  5. import { openModalAction } from './store';
  6. import { LoginIndex } from './components/login';
  7. import { RealNameIndex } from './components/real-name';
  8. import { PaymentIndex } from './components/payment';
  9. import { report } from './components/report';
  10. import { getH5GameLinkApi } from './api';
  11. import { getURLparams } from './utils';
  12. import { init } from './components/init';
  13. import { nativeInit } from './components/init/native-init';
  14. import SlideBar from './components/slide-bar';
  15. function App() {
  16. const iframeRef = useRef<HTMLIFrameElement>(null);
  17. const [iframeUrl, setIframeUrl] = useState<string>('');
  18. const openModal = useSetAtom(openModalAction);
  19. // const modalState = useAtomValue(modalStackAtom);
  20. const getGameUrl = async () => {
  21. const res = await getH5GameLinkApi({game_id: getURLparams('game_id') || ''})
  22. setIframeUrl(`${res.game_url}?game_id=${getURLparams('game_id') || ''}&time=${Math.round(new Date().getTime()/1000)}`)
  23. }
  24. // 初始化通信桥
  25. useEffect(() => {
  26. if (iframeRef.current) {
  27. pmBridge.init(iframeRef.current);
  28. }
  29. return () => pmBridge.destroy();
  30. }, []);
  31. // EventBus 事件订阅
  32. useEffect(() => {
  33. // 处理登录请求
  34. const handleLoginReq = (payload: any) => {
  35. openModal({name:'login',item:{isOpen:true,requestId:payload.requestId,data:payload.data}})
  36. console.log("登录")
  37. };
  38. // 处理支付请求
  39. const handlePayReq = (payload: any) => {
  40. openModal({name:'payment',item:{isOpen:true,requestId:payload.requestId,data:payload.data}})
  41. };
  42. // 处理上报请求
  43. const handleReportReq = (payload: any) => {
  44. report(payload)
  45. };
  46. // 处理初始化请求
  47. const handleInitReq = (payload: any) => {
  48. // 微端是真
  49. if(getURLparams('is_native') === 'true'){
  50. nativeInit(payload)
  51. }else{
  52. init(payload)
  53. }
  54. }
  55. eventBus.on('LOGIN_REQUEST', handleLoginReq);
  56. eventBus.on('PAY_REQUEST', handlePayReq);
  57. eventBus.on('REPORT_REQUEST', handleReportReq);
  58. eventBus.on('INIT_REQUEST', handleInitReq);
  59. return () => {
  60. eventBus.off('LOGIN_REQUEST', handleLoginReq);
  61. eventBus.off('PAY_REQUEST', handlePayReq);
  62. eventBus.off('REPORT_REQUEST', handleReportReq);
  63. eventBus.off('INIT_REQUEST', handleInitReq);
  64. };
  65. }, []);
  66. // 请求记载游戏链接
  67. useEffect(() => {
  68. getGameUrl()
  69. }, []);
  70. return (
  71. <div className="w-full h-screen relative bg-white font-sans overflow-hidden">
  72. {/* Iframe 游戏容器 */}
  73. <iframe
  74. ref={iframeRef}
  75. src={iframeUrl}
  76. title="Game Container"
  77. className="w-full h-full border-none"
  78. sandbox="allow-scripts allow-same-origin allow-forms"
  79. />
  80. {/* 渲染弹窗 */}
  81. <LoginIndex />
  82. <RealNameIndex />
  83. <PaymentIndex />
  84. <SlideBar />
  85. </div>
  86. );
  87. }
  88. export default App;