App.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // GA
  2. import ReactGA from 'react-ga4';
  3. // utils
  4. import {lazy, Suspense} from 'react';
  5. // styles
  6. import ThemeStyles from '@styles/theme';
  7. import './style.scss';
  8. // libs styles
  9. import 'react-toastify/dist/ReactToastify.min.css';
  10. import 'react-grid-layout/css/styles.css';
  11. import 'swiper/css';
  12. import 'swiper/css/effect-fade';
  13. import 'swiper/css/pagination';
  14. // fonts
  15. import '@fonts/icomoon/icomoon.woff';
  16. // import '@fonts/swisscbt/SWISSC-BT.ttf'
  17. // contexts
  18. import {SidebarProvider, useSidebar} from '@contexts/sidebarContext';
  19. import {ThemeProvider} from 'styled-components';
  20. // hooks
  21. import {useThemeProvider} from '@contexts/themeContext';
  22. import {useEffect, useRef} from 'react';
  23. import {useWindowSize} from 'react-use';
  24. import useAuthRoute from '@hooks/useAuthRoute';
  25. // utils
  26. import {StyleSheetManager} from 'styled-components';
  27. import {ThemeProvider as MuiThemeProvider, createTheme} from '@mui/material/styles';
  28. import {getUrlParam, localStorageSet, preventDefault} from '@utils/helpers';
  29. import rtlPlugin from 'stylis-plugin-rtl';
  30. import {CacheProvider} from '@emotion/react';
  31. import createCache from '@emotion/cache';
  32. // components
  33. import {BrowserRouter, HashRouter, Route, Routes, useLocation} from 'react-router-dom';
  34. import {ToastContainer} from 'react-toastify';
  35. import Layout from "@layout/Layout";
  36. import {getBFP, getFetchStatus, getUserID, getUserInfo} from '@features/mainSlice';
  37. import {useDispatch, useSelector} from "react-redux";
  38. import {REQ_STATUS} from "@api/config";
  39. import {reqCustomService, reqGetRoutes, reqTurnplateIndex, reqWithdrawBaseInfo} from "@features/api";
  40. import LoadingScreen from "@components/LoadingScreen";
  41. import {reqPayConfig} from "@features/paySlice";
  42. import {connectHall, disconnectHall} from "@features/actions/hallClient";
  43. import * as toast from "react-toastify";
  44. import {useBlocker} from "react-router";
  45. import FontFaceObserver from 'fontfaceobserver';
  46. import {getTurnplate} from "@features/turnplateSlice";
  47. import {LOCAL_WHEEL_INVITE_CODE} from "@constants/localkey";
  48. const App = () => {
  49. const dispatch = useDispatch();
  50. const fetchStatus=useSelector(getFetchStatus);
  51. const userInfo=useSelector(getUserInfo);
  52. const userID=useSelector(getUserID);
  53. const turnplate=useSelector(getTurnplate);
  54. const appRef = useRef(null);
  55. const {theme, direction,setIsShowWheel} = useThemeProvider();
  56. const {pathname, search, hash} = useLocation();
  57. const gaKey = process.env.REACT_APP_PUBLIC_GA;
  58. gaKey && ReactGA.initialize(gaKey);
  59. // auto RTL support for Material-UI components and styled-components
  60. const plugins = direction === 'rtl' ? [rtlPlugin] : [];
  61. const muiTheme = createTheme({
  62. direction: direction,
  63. });
  64. const cacheRtl = createCache({
  65. key: 'css-rtl',
  66. stylisPlugins: plugins,
  67. });
  68. const startReqRoutes=()=>{
  69. //先请求指纹id
  70. dispatch(getBFP()).then((result)=>{
  71. dispatch(reqGetRoutes());
  72. });
  73. }
  74. const loadFont=()=>{
  75. // let swisscbt=new FontFaceObserver("SWISSC-BT");
  76. // swisscbt.load().then(() => {
  77. //
  78. // })
  79. new FontFaceObserver("SWISSC-BT").load();
  80. new FontFaceObserver("Roboto").load();
  81. new FontFaceObserver("Roboto-1").load();
  82. }
  83. //检查邀请码
  84. const checkInviteCode=()=>{
  85. let name="act";
  86. let code=getUrlParam(name);
  87. if (code!=null){
  88. localStorageSet(LOCAL_WHEEL_INVITE_CODE,code)
  89. }
  90. else {
  91. localStorageSet(LOCAL_WHEEL_INVITE_CODE,"")
  92. }
  93. }
  94. //初始化
  95. useEffect(() => {
  96. // scroll to top on route change
  97. loadFont();
  98. //请求routes
  99. startReqRoutes();
  100. appRef.current && appRef.current.scrollTo(0, 0);
  101. preventDefault();
  102. checkInviteCode();
  103. }, []);
  104. useEffect(()=>{
  105. if (userID!=0){
  106. dispatch(connectHall());
  107. //withdraw
  108. dispatch(reqWithdrawBaseInfo());
  109. //turnplate
  110. // dispatch(reqTurnplateIndex());
  111. //rest code
  112. localStorageSet(LOCAL_WHEEL_INVITE_CODE,"")
  113. }
  114. else
  115. {
  116. dispatch(disconnectHall());
  117. }
  118. //pay
  119. reqPayConfig(dispatch,userInfo);
  120. //customer
  121. dispatch(reqCustomService());
  122. dispatch(reqTurnplateIndex());
  123. },[userID])
  124. //转盘
  125. useEffect(()=>{
  126. if(turnplate){
  127. let times=turnplate.times;
  128. if (times>0){
  129. if (pathname==="/"){
  130. if(hash==null||hash===""){
  131. setIsShowWheel(true)
  132. }
  133. }
  134. }
  135. }
  136. },[turnplate])
  137. const renderLayout=()=>{
  138. return (
  139. <>
  140. {
  141. fetchStatus===REQ_STATUS.IDLE?<Layout />:(<LoadingScreen />)
  142. }
  143. </>
  144. )
  145. }
  146. // return (
  147. // <CacheProvider value={cacheRtl}>
  148. // <MuiThemeProvider theme={muiTheme}>
  149. // <SidebarProvider>
  150. // <ThemeProvider theme={{theme: theme}}>
  151. // <ThemeStyles/>
  152. // <ToastContainer theme={theme} autoClose={2500}
  153. // position={direction === 'ltr' ? 'top-right' : 'top-left'}/>
  154. // <StyleSheetManager stylisPlugins={plugins}>
  155. // {
  156. // renderLayout()
  157. // }
  158. // </StyleSheetManager>
  159. // </ThemeProvider>
  160. // </SidebarProvider>
  161. // </MuiThemeProvider>
  162. // </CacheProvider>
  163. // );
  164. return (
  165. <CacheProvider value={cacheRtl}>
  166. <MuiThemeProvider theme={muiTheme}>
  167. <ThemeProvider theme={{theme: theme}}>
  168. <SidebarProvider>
  169. <ThemeStyles/>
  170. <ToastContainer theme={theme} autoClose={2500}
  171. position={direction === 'ltr' ? 'top-right' : 'top-left'}/>
  172. <StyleSheetManager stylisPlugins={plugins}>
  173. {
  174. renderLayout()
  175. }
  176. </StyleSheetManager>
  177. </SidebarProvider>
  178. </ThemeProvider>
  179. </MuiThemeProvider>
  180. </CacheProvider>
  181. );
  182. }
  183. export default App