App.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, useNavigate} from 'react-router-dom';
  34. import {ToastContainer} from 'react-toastify';
  35. import Layout from "@layout/Layout";
  36. import {getBFP, getFetchStatus, getHallServerUrl, getUserID, getUserInfo, isLogined} from '@features/mainSlice';
  37. import {useDispatch, useSelector} from "react-redux";
  38. import {REQ_STATUS} from "@api/config";
  39. import {
  40. reqAgentCheck, reqCheckinCheck,
  41. reqCustomService, reqFirstPayConfig, reqGameActivity, reqGameInfo,
  42. reqGetRoutes,
  43. reqMailCheck,
  44. reqRedpackCheck, reqRedpackConfig,
  45. reqTurnplateIndex,
  46. reqWithdrawBaseInfo
  47. } from "@features/api";
  48. import LoadingScreen from "@components/LoadingScreen";
  49. import DropCoins from "@components/DropCoins";
  50. import {reqPayConfig} from "@features/paySlice";
  51. import {connectHall, disconnectHall} from "@features/actions/hallClient";
  52. import {toast} from "react-toastify";
  53. import {useBlocker} from "react-router";
  54. import FontFaceObserver from 'fontfaceobserver';
  55. import {getTurnplate} from "@features/turnplateSlice";
  56. import {LOCAL_ACTIVITY_C, LOCAL_WHEEL_INVITE_CODE} from "@constants/localkey";
  57. import LoadingFull from "@components/Loading/LoadingFull";
  58. import {ClickAwayListener} from "@mui/base/ClickAwayListener";
  59. import useGlobalClick from "@hooks/useGlobalClick";
  60. import SoundHelp from "@utils/SoundManger";
  61. import usePreviousRoute from "@hooks/usePreviousRoute";
  62. import {ROUTE_PATH} from "@constants/routes";
  63. import {onChangeUser, onChangeUserID} from "@features/actions/mainAction";
  64. const App = () => {
  65. const dispatch = useDispatch();
  66. const fetchStatus=useSelector(getFetchStatus);
  67. const userInfo=useSelector(getUserInfo);
  68. const userID=useSelector(getUserID);
  69. const turnplate=useSelector(getTurnplate);
  70. const appRef = useRef(null);
  71. const {theme, direction,setIsShowWheel} = useThemeProvider();
  72. const {pathname, search, hash} = useLocation();
  73. const gaKey = process.env.REACT_APP_PUBLIC_GA;
  74. gaKey && ReactGA.initialize(gaKey);
  75. const navigate = useNavigate();
  76. // auto RTL support for Material-UI components and styled-components
  77. const plugins = direction === 'rtl' ? [rtlPlugin] : [];
  78. const hallServerUrl=useSelector(getHallServerUrl);
  79. useGlobalClick();
  80. const prevLocation=usePreviousRoute();
  81. const muiTheme = createTheme({
  82. direction: direction,
  83. });
  84. const cacheRtl = createCache({
  85. key: 'css-rtl',
  86. stylisPlugins: plugins,
  87. });
  88. const startReqRoutes=()=>{
  89. //先请求指纹id
  90. dispatch(getBFP()).then((result)=>{
  91. dispatch(reqGetRoutes());
  92. });
  93. }
  94. const loadFont=()=>{
  95. // let swisscbt=new FontFaceObserver("SWISSC-BT");
  96. // swisscbt.load().then(() => {
  97. //
  98. // })
  99. new FontFaceObserver("SWISSC-BT").load();
  100. new FontFaceObserver("Roboto").load();
  101. new FontFaceObserver("Roboto-1").load();
  102. }
  103. //检查邀请码
  104. const checkInviteCode=()=>{
  105. let name="act";
  106. let code=getUrlParam(name);
  107. if (code!=null){
  108. localStorageSet(LOCAL_WHEEL_INVITE_CODE,code)
  109. }
  110. else {
  111. localStorageSet(LOCAL_WHEEL_INVITE_CODE,"")
  112. }
  113. let cname="c";
  114. let ccode=getUrlParam(cname);
  115. if (ccode!=null){
  116. localStorageSet(LOCAL_ACTIVITY_C,ccode);
  117. }
  118. // else{
  119. // localStorageSet(LOCAL_ACTIVITY_C,"");
  120. // }
  121. }
  122. const reProgressierInstallBanner=()=>{
  123. if (window.NewProgressierInstallBanner!=null)return;
  124. let ProgressierInstallBanner=window.ProgressierInstallBanner;
  125. window.NewProgressierInstallBanner=ProgressierInstallBanner;
  126. window.ProgressierInstallBanner=function (e, t) {
  127. this.remove = async function() {
  128. };
  129. this.render = async function() {
  130. };
  131. this.make = function() {
  132. };
  133. this.init = async function() {
  134. }
  135. }
  136. }
  137. const reqInitData=()=>{
  138. //未初始化
  139. if (userID===-1)return;
  140. if (userID>0){
  141. dispatch(connectHall(hallServerUrl));
  142. localStorageSet(LOCAL_WHEEL_INVITE_CODE,"");
  143. localStorageSet(LOCAL_ACTIVITY_C,"");
  144. //Mail红点屏蔽
  145. dispatch(reqMailCheck());
  146. }
  147. else
  148. {
  149. dispatch(disconnectHall());
  150. }
  151. //pay
  152. // reqPayConfig(dispatch,userInfo);
  153. // dispatch(reqFirstPayConfig(userID))
  154. //customer
  155. // dispatch(reqCustomService());
  156. //turnplate
  157. dispatch(reqTurnplateIndex());
  158. //redpack
  159. // dispatch(reqRedpackCheck());
  160. // dispatch(reqRedpackConfig());
  161. //agent
  162. // dispatch(reqAgentCheck());
  163. //activity
  164. // dispatch(reqGameActivity());
  165. dispatch(onChangeUser(userInfo));
  166. }
  167. const appInitData=()=>{
  168. // scroll to top on route change
  169. loadFont();
  170. //请求routes
  171. startReqRoutes();
  172. checkInviteCode();
  173. appRef.current && appRef.current.scrollTo(0, 0);
  174. preventDefault();
  175. reProgressierInstallBanner();
  176. SoundHelp.loadAllAudioFile();
  177. // preventTouchMove();
  178. }
  179. const checkOpenCheckin=()=>{
  180. if (userID>0){
  181. if (pathname==="/"){
  182. if(hash==null||hash===""){
  183. dispatch(reqCheckinCheck((isCanCheckIn)=>{
  184. if(isCanCheckIn===1)navigate(ROUTE_PATH.CHECK_IN);
  185. }));
  186. return true;
  187. }
  188. }
  189. }
  190. return false;
  191. }
  192. const onCloseHallWheel=()=>{
  193. checkOpenCheckin();
  194. }
  195. //初始化
  196. useEffect(() => {
  197. appInitData();
  198. return ()=>{
  199. }
  200. }, []);
  201. useEffect(()=>{
  202. reqInitData();
  203. },[userID])
  204. //转盘
  205. useEffect(()=>{
  206. if(turnplate){
  207. let times=turnplate.times;
  208. if (times>0){
  209. if (pathname==="/"){
  210. if(hash==null||hash===""){
  211. setIsShowWheel(true);
  212. return;
  213. }
  214. }
  215. }
  216. }
  217. checkOpenCheckin();
  218. },[turnplate]);
  219. useEffect(()=>{
  220. if (prevLocation){
  221. let pathname=prevLocation.pathname;
  222. if (pathname){
  223. if (pathname.startsWith(ROUTE_PATH.GAME)){
  224. //更新金币
  225. dispatch(reqGameInfo());
  226. }
  227. }
  228. }
  229. },[prevLocation])
  230. const renderLayout=()=>{
  231. return (
  232. <>
  233. {
  234. fetchStatus===REQ_STATUS.IDLE?<Layout onCloseHallWheel={onCloseHallWheel} />:(<LoadingFull />)
  235. }
  236. </>
  237. )
  238. }
  239. return (
  240. <CacheProvider value={cacheRtl}>
  241. <MuiThemeProvider theme={muiTheme}>
  242. <ThemeProvider theme={{theme: theme}}>
  243. <SidebarProvider>
  244. <ThemeStyles/>
  245. <ToastContainer theme={theme} autoClose={2500}
  246. position={direction === 'ltr' ? 'top-right' : 'top-left'}/>
  247. <StyleSheetManager stylisPlugins={plugins}>
  248. {
  249. renderLayout()
  250. }
  251. </StyleSheetManager>
  252. </SidebarProvider>
  253. </ThemeProvider>
  254. </MuiThemeProvider>
  255. </CacheProvider>
  256. );
  257. }
  258. export default App