App.js 9.3 KB

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