App.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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, localStorageGet, 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, getOpenPay, getPromoteInstall,
  40. getUserID,
  41. getUserInfo,
  42. isLogined,
  43. updateCurrGame, updateOpenPay
  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_VISTOR_ID, 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 {ROUTE_PATH} from "@constants/routes";
  70. import {onChangeUser, onChangeUserID} from "@features/actions/mainAction";
  71. import {isInAppBrowser, isShowInstallBox, openUrlUseChrome} from "@utils/UAUtils";
  72. import WInstallBox from "@widgets/WInstallBox";
  73. const App = () => {
  74. const dispatch = useDispatch();
  75. const fetchStatus=useSelector(getFetchStatus);
  76. const userInfo=useSelector(getUserInfo);
  77. const userID=useSelector(getUserID);
  78. const turnplate=useSelector(getTurnplate);
  79. const appRef = useRef(null);
  80. const {theme, direction,setIsShowWheel,preLocation,gameID} = useThemeProvider();
  81. const {pathname, search, hash} = useLocation();
  82. const gaKey = process.env.REACT_APP_PUBLIC_GA;
  83. gaKey && ReactGA.initialize(gaKey);
  84. const navigate = useNavigate();
  85. // auto RTL support for Material-UI components and styled-components
  86. const plugins = direction === 'rtl' ? [rtlPlugin] : [];
  87. const hallServerUrl=useSelector(getHallServerUrl);
  88. useGlobalClick();
  89. // const {preLocation,gameID}=usePreviousRoute();
  90. const serverKick=useSelector(getKickState);
  91. const promoteInstall=useSelector(getPromoteInstall);
  92. const openPay=useSelector(getOpenPay);
  93. const muiTheme = createTheme({
  94. direction: direction,
  95. });
  96. const cacheRtl = createCache({
  97. key: 'css-rtl',
  98. stylisPlugins: plugins,
  99. });
  100. const startReqRoutes=()=>{
  101. //先请求指纹id
  102. dispatch(getBFP()).then((result)=>{
  103. dispatch(reqGetRoutes());
  104. });
  105. }
  106. const loadFont=()=>{
  107. // let swisscbt=new FontFaceObserver("SWISSC-BT");
  108. // swisscbt.load().then(() => {
  109. //
  110. // })
  111. new FontFaceObserver("SWISSC-BT").load();
  112. new FontFaceObserver("Roboto").load();
  113. new FontFaceObserver("Roboto-1").load();
  114. }
  115. //检查邀请码
  116. const checkInviteCode=()=>{
  117. let name="act";
  118. let code=getUrlParam(name);
  119. if (code!=null){
  120. localStorageSet(LOCAL_WHEEL_INVITE_CODE,code)
  121. }
  122. else {
  123. localStorageSet(LOCAL_WHEEL_INVITE_CODE,"")
  124. }
  125. let cname="c";
  126. let ccode=getUrlParam(cname);
  127. if (ccode!=null){
  128. localStorageSet(LOCAL_ACTIVITY_C,ccode);
  129. }
  130. // else{
  131. // localStorageSet(LOCAL_ACTIVITY_C,"");
  132. // }
  133. let bfp=getUrlParam("bfp");
  134. if (bfp!=null&&bfp!==""){
  135. localStorageSet(LOCAL_VISTOR_ID,bfp);
  136. global.BFP=bfp;
  137. }
  138. }
  139. const reProgressierInstallBanner=()=>{
  140. if (window.NewProgressierInstallBanner!=null)return;
  141. let ProgressierInstallBanner=window.ProgressierInstallBanner;
  142. window.NewProgressierInstallBanner=ProgressierInstallBanner;
  143. window.ProgressierInstallBanner=function (e, t) {
  144. this.remove = async function() {
  145. };
  146. this.render = async function() {
  147. };
  148. this.make = function() {
  149. };
  150. this.init = async function() {
  151. }
  152. }
  153. }
  154. const reqInitData=()=>{
  155. //未初始化
  156. if (userID===-1)return;
  157. //onChangeUser
  158. dispatch(onChangeUser(userInfo));
  159. if (userID>0){
  160. dispatch(connectHall(hallServerUrl));
  161. localStorageSet(LOCAL_WHEEL_INVITE_CODE,"");
  162. // localStorageSet(LOCAL_ACTIVITY_C,"");
  163. //Mail红点屏蔽
  164. dispatch(reqMailCheck());
  165. dispatch(reqFavoGet());
  166. }
  167. else
  168. {
  169. dispatch(disconnectHall());
  170. }
  171. //pay
  172. // reqPayConfig(dispatch,userInfo);
  173. // dispatch(reqFirstPayConfig(userID))
  174. //customer
  175. // dispatch(reqCustomService());
  176. //turnplate
  177. dispatch(reqTurnplateIndex());
  178. //redpack
  179. // dispatch(reqRedpackCheck());
  180. // dispatch(reqRedpackConfig());
  181. //agent
  182. // dispatch(reqAgentCheck());
  183. //activity
  184. // dispatch(reqGameActivity());
  185. }
  186. const appInitData=()=>{
  187. // scroll to top on route change
  188. checkInviteCode();
  189. checkOpenInstallBox();
  190. loadFont();
  191. //请求routes
  192. startReqRoutes();
  193. appRef.current && appRef.current.scrollTo(0, 0);
  194. preventDefault();
  195. reProgressierInstallBanner();
  196. SoundHelp.loadAllAudioFile();
  197. // preventTouchMove();
  198. }
  199. const checkOpenCheckin=()=>{
  200. if (userID>0){
  201. if (pathname==="/"){
  202. if(hash==null||hash===""){
  203. let key="checkin_open_time";
  204. let now=Date.now();
  205. let lst=Number(localStorageGet(key,0));
  206. let delta=now-lst;
  207. if (delta<86400000){
  208. return false;
  209. }
  210. dispatch(reqCheckinCheck((isCanCheckIn)=>{
  211. if(hash==null||hash===""){
  212. if(isCanCheckIn===1){
  213. navigate(ROUTE_PATH.CHECK_IN,{replace:true});
  214. localStorageSet(key,Date.now());
  215. }
  216. }
  217. }));
  218. return true;
  219. }
  220. }
  221. }
  222. return false;
  223. }
  224. const onCloseHallWheel=()=>{
  225. checkOpenCheckin();
  226. }
  227. const checkOpenInstallBox=()=>{
  228. if (global.installed===1)return;
  229. global.installed=1;
  230. let time=200;
  231. // global.showBindPhone();
  232. // time=1000;
  233. setTimeout(async ()=>{
  234. // if (window.progressier==null)return ;
  235. // if (window.progressier.detection.isStandalone())return;
  236. // window.progressier.flow.init("install",true)
  237. if (!isShowInstallBox())return;
  238. if (isInAppBrowser()){
  239. openUrlUseChrome();
  240. }
  241. global.showInstallBox();
  242. },time);
  243. }
  244. //app Effect
  245. //初始化
  246. useEffect(() => {
  247. appInitData();
  248. return ()=>{
  249. }
  250. }, []);
  251. //用户信息改变
  252. useEffect(()=>{
  253. reqInitData();
  254. },[userID])
  255. //转盘
  256. useEffect(()=>{
  257. if(turnplate){
  258. let times=turnplate.times;
  259. if (times>0){
  260. if (pathname==="/"){
  261. if(hash==null||hash===""){
  262. setIsShowWheel(true);
  263. return;
  264. }
  265. }
  266. }
  267. }
  268. checkOpenCheckin();
  269. },[turnplate]);
  270. //监听上一次的路由
  271. useEffect(()=>{
  272. if (preLocation){
  273. let pathname=preLocation.pathname;
  274. if (pathname){
  275. if (pathname.startsWith(ROUTE_PATH.GAME)){
  276. //更新金币
  277. if (userID>0){
  278. dispatch(reqGameInfo());
  279. }
  280. }
  281. }
  282. }
  283. },[preLocation])
  284. //监听gameID
  285. useEffect(()=>{
  286. dispatch(updateCurrGame(gameID))
  287. dispatch(hallSendState());
  288. },[gameID])
  289. //监听被服务器踢掉
  290. useEffect(()=>{
  291. if (serverKick===1){
  292. if (pathname.startsWith(ROUTE_PATH.GAME)){
  293. navigate(ROUTE_PATH.HOME,{replace:true});
  294. }
  295. }
  296. },[serverKick])
  297. //监听安装提示
  298. useEffect(()=>{
  299. // if (promoteInstall>0){
  300. // if (global.installed===1)return;
  301. // global.installed=1;
  302. // let time=promoteInstall*1000;
  303. // // global.showBindPhone();
  304. // // time=1000;
  305. // setTimeout(async ()=>{
  306. // // if (window.progressier==null)return ;
  307. // // if (window.progressier.detection.isStandalone())return;
  308. // // window.progressier.flow.init("install",true)
  309. // if (!isShowInstallBox())return;
  310. // if (isInAppBrowser()){
  311. // openUrlUseChrome();
  312. // }
  313. // global.showInstallBox();
  314. // },time);
  315. // }
  316. },[promoteInstall])
  317. //监听打开pay
  318. useEffect(()=>{
  319. if (openPay===1){
  320. navigate(ROUTE_PATH.PAY,{replace:true});
  321. dispatch(updateOpenPay(0));
  322. }
  323. },[openPay])
  324. //app Effect
  325. const renderLayout=()=>{
  326. return (
  327. <>
  328. {
  329. fetchStatus===REQ_STATUS.IDLE?<Layout onCloseHallWheel={onCloseHallWheel} />:(<LoadingFull />)
  330. }
  331. </>
  332. )
  333. }
  334. const renderPreload=()=>{
  335. return (
  336. <>
  337. {
  338. <WInstallBox/>
  339. }
  340. </>
  341. )
  342. }
  343. return (
  344. <CacheProvider value={cacheRtl}>
  345. <MuiThemeProvider theme={muiTheme}>
  346. <ThemeProvider theme={{theme: theme}}>
  347. <SidebarProvider>
  348. <ThemeStyles/>
  349. <ToastContainer theme={theme} autoClose={2500}
  350. position={direction === 'ltr' ? 'top-right' : 'top-left'}/>
  351. <StyleSheetManager stylisPlugins={plugins}>
  352. {
  353. renderLayout()
  354. }
  355. </StyleSheetManager>
  356. {
  357. renderPreload()
  358. }
  359. </SidebarProvider>
  360. </ThemeProvider>
  361. </MuiThemeProvider>
  362. </CacheProvider>
  363. );
  364. }
  365. export default App