瀏覽代碼

记录上次路由 处理用户信息改变

zhejiushizhuce 2 年之前
父節點
當前提交
f32df4b1c5

+ 31 - 22
src/App.js

@@ -41,12 +41,12 @@ import createCache from '@emotion/cache';
 import {BrowserRouter, HashRouter, Route, Routes, useLocation, useNavigate} from 'react-router-dom';
 import {ToastContainer} from 'react-toastify';
 import Layout from "@layout/Layout";
-import {getBFP, getFetchStatus, getHallServerUrl, getUserID, getUserInfo} from '@features/mainSlice';
+import {getBFP, getFetchStatus, getHallServerUrl, getUserID, getUserInfo, isLogined} from '@features/mainSlice';
 import {useDispatch, useSelector} from "react-redux";
 import {REQ_STATUS} from "@api/config";
 import {
     reqAgentCheck,
-    reqCustomService, reqFirstPayConfig, reqGameActivity,
+    reqCustomService, reqFirstPayConfig, reqGameActivity, reqGameInfo,
     reqGetRoutes,
     reqMailCheck,
     reqRedpackCheck, reqRedpackConfig,
@@ -66,6 +66,9 @@ import LoadingFull from "@components/Loading/LoadingFull";
 import {ClickAwayListener} from "@mui/base/ClickAwayListener";
 import useGlobalClick from "@hooks/useGlobalClick";
 import SoundHelp from "@utils/SoundManger";
+import usePreviousRoute from "@hooks/usePreviousRoute";
+import {ROUTE_PATH} from "@constants/routes";
+import {onChangeUser, onChangeUserID} from "@features/actions/mainAction";
 const App = () => {
     const dispatch = useDispatch();
     const fetchStatus=useSelector(getFetchStatus);
@@ -83,6 +86,7 @@ const App = () => {
     const plugins = direction === 'rtl' ? [rtlPlugin] : [];
     const hallServerUrl=useSelector(getHallServerUrl);
     useGlobalClick();
+    const prevLocation=usePreviousRoute();
     const muiTheme = createTheme({
         direction: direction,
     });
@@ -172,6 +176,7 @@ const App = () => {
         // dispatch(reqAgentCheck());
         //activity
         // dispatch(reqGameActivity());
+        dispatch(onChangeUser(userInfo));
     }
     const appInitData=()=>{
         // scroll to top on route change
@@ -203,12 +208,33 @@ const App = () => {
             if (times>0){
                 if (pathname==="/"){
                     if(hash==null||hash===""){
-                        setIsShowWheel(true)
+                        setIsShowWheel(true);
+                        return;
                     }
                 }
             }
         }
-    },[turnplate])
+        if (userID>0){
+            if (pathname==="/"){
+                if(hash==null||hash===""){
+                    navigate(ROUTE_PATH.CHECK_IN);
+                    return;
+                }
+            }
+        }
+    },[turnplate]);
+    useEffect(()=>{
+        if (prevLocation){
+            let pathname=prevLocation.pathname;
+            if (pathname){
+                if (pathname.startsWith(ROUTE_PATH.GAME)){
+                    //更新金币
+                    dispatch(reqGameInfo());
+                }
+            }
+        }
+    },[prevLocation])
+
     const renderLayout=()=>{
         return (
             <>
@@ -218,24 +244,7 @@ const App = () => {
             </>
         )
     }
-    // return (
-    //     <CacheProvider value={cacheRtl}>
-    //         <MuiThemeProvider theme={muiTheme}>
-    //             <SidebarProvider>
-    //                 <ThemeProvider theme={{theme: theme}}>
-    //                     <ThemeStyles/>
-    //                     <ToastContainer theme={theme} autoClose={2500}
-    //                                     position={direction === 'ltr' ? 'top-right' : 'top-left'}/>
-    //                     <StyleSheetManager stylisPlugins={plugins}>
-    //                         {
-    //                             renderLayout()
-    //                         }
-    //                     </StyleSheetManager>
-    //                 </ThemeProvider>
-    //             </SidebarProvider>
-    //         </MuiThemeProvider>
-    //     </CacheProvider>
-    // );
+
     return (
         <CacheProvider value={cacheRtl}>
             <MuiThemeProvider theme={muiTheme}>

+ 16 - 0
src/features/actions/mainAction.js

@@ -0,0 +1,16 @@
+const handleChangeUser=(dispatch,getState,useInfo)=>{
+    let state=getState();
+    for (const key in state) {
+        //如需切换用户刷新数据 slice 监听onSliceChangeUser
+        let action={
+            type:key+"/onSliceChangeUser",
+            payload:useInfo,
+        }
+        dispatch(action);
+    }
+}
+export const onChangeUser=(userInfo)=>{
+    return (dispatch, getState) => {
+        handleChangeUser(dispatch,getState,userInfo);
+    }
+}

+ 4 - 1
src/features/agentSlice.js

@@ -107,7 +107,10 @@ export const AgentSlice = createSlice({
         link:""
     },
     reducers:{
-
+        //如需切换用户刷新数据 slice 监听onSliceChangeUser
+        onSliceChangeUser:((state, action) => {
+            state.isInit=false;
+        })
     },
     extraReducers: (builder) => {
         buildReqReducer(builder,reqAgentCheck,reducerAgentCheck,agentCheckStatus);

+ 2 - 1
src/features/checkInSlice.js

@@ -32,7 +32,8 @@ const reducerCheckinCheck=(state,action,arg)=>{
             let NextGetTime=0;
             if (data.LastDate!=null){
                 LastDate=data.LastDate;
-                LastDate=Date.parse(LastDate+" GMT-3");
+                LastDate=LastDate.split(".").shift();
+                LastDate=Date.parse(LastDate+"-03:00");
                 NextGetTime=LastDate+86400000;
             }
             let NextDayNumber=Number(data.NextDayNumber);

+ 3 - 0
src/features/paySlice.js

@@ -259,6 +259,9 @@ export const Pay=createSlice({
         setChangePayPassType:((state, action) => {
             state.changePayPassType=action.payload;
         }),
+        onSliceChangeUser:((state, action) => {
+            state.isInit=false;
+        })
     },
     extraReducers: (builder) => {
         buildReqReducer(builder,reqRechargeConfig,reducerRechargeConfig,rechargeConfigStatus);

+ 7 - 4
src/hooks/usePreviousRoute.js

@@ -2,13 +2,16 @@ import {useLocation} from 'react-router-dom';
 import {useEffect, useRef, useState} from 'react';
 const usePreviousRoute = () => {
     const location = useLocation();
-    const prevLocation = useRef(null);
-
+    const [preLocation, setPreLocation] = useState({});
+    // const prevLocation = useRef(null);
+    // const currentLocation = useRef(null);
+    const [currentLocation, setCurrentLocation] = useState({});
     useEffect(() => {
-        prevLocation.current = location;
+            setPreLocation({...currentLocation})
+            setCurrentLocation({...location});
         }, [location]);
 
-    return prevLocation.current;
+    return preLocation;
 }
 
 export default usePreviousRoute

+ 1 - 1
src/widgets/WGame/index.jsx

@@ -25,7 +25,7 @@ const WGame=()=>{
     var gameURL="";
     const navigate=useNavigate();
     const onBack=()=>{
-        dispatch(reqGameInfo());
+        // dispatch(reqGameInfo());
         navigate(ROUTE_PATH.BACK);
     }
     const receiveMessage=(event)=>{