Leon преди 1 година
родител
ревизия
3a0dee7553
променени са 2 файла, в които са добавени 54 реда и са изтрити 3 реда
  1. 50 2
      src/app.tsx
  2. 4 1
      src/services/http/request.ts

+ 50 - 2
src/app.tsx

@@ -1,11 +1,12 @@
 import { Component, PropsWithChildren } from 'react'
 import './app.scss'
 import '@/context/locales/index'
-import { Provider } from 'react-redux'
+import { Provider, useDispatch } from 'react-redux'
 import store from './store/store'
 import { View } from '@tarojs/components'
 import GlobalModal from './components/layout/GlobalModal'
 import Taro from '@tarojs/taro'
+import { getInfoSuccess } from './store/user'
 // import { StatusBar } from 'react-native'
 // import jgPush from './utils/push.rn'
 
@@ -22,10 +23,15 @@ if (process.env.TARO_ENV == 'rn') {
 
 
 const App: React.FC<PropsWithChildren> = ({ children }) => {
-  if (process.env.TARO_ENV=='rn'){
+
+  // const dispatch = useDispatch();
+
+
+  if (process.env.TARO_ENV == 'rn') {
     // alert(Intl.DateTimeFormat().resolvedOptions().timeZone)
   }
   if (process.env.TARO_ENV == 'weapp') {
+    loadWXCache()
     global.isDebug = Taro.getStorageSync('isDebug')
 
     const updateManager = Taro.getUpdateManager()
@@ -53,6 +59,7 @@ const App: React.FC<PropsWithChildren> = ({ children }) => {
   }
   else {
     //关闭其中某些yellow警告
+    loadRNCache()
     LogBox.ignoreLogs(['Warning: ...']); // Ignore log notification by message
     LogBox.ignoreAllLogs();//Ignore all log notifications
 
@@ -105,6 +112,47 @@ const App: React.FC<PropsWithChildren> = ({ children }) => {
 
   }
 
+  function loadWXCache() {
+  //   var showDayRing = Taro.getStorageSync('showDayRing') || false;
+  //   var showNightRing = Taro.getStorageSync('showNightRing') || false;
+  //   dispatch(showDay(showDayRing))
+  //   dispatch(showNight(showNightRing))
+  //   var gps = Taro.getStorageSync('gps')
+  //   if (gps) {
+  //     global.locationDetail = JSON.parse(gps)
+  //   }
+    var userData = Taro.getStorageSync('userData')
+    if (userData) {
+      global.token = JSON.parse(userData).token
+      // dispatch(getInfoSuccess(JSON.parse(userData)));
+    }
+  }
+
+  async function loadRNCache() {
+    // var showDayRing = await getStorage('showDayRing') || false;
+    // var showNightRing = await getStorage('showNightRing') || false;
+    // dispatch(showDay(showDayRing))
+    // dispatch(showNight(showNightRing))
+    // var gps = await getStorage('gps')
+    // if (gps) {
+    //   global.locationDetail = JSON.parse(gps)
+    // }
+    var userData = await getStorage('userData')
+    if (userData) {
+      global.token = JSON.parse(userData).token
+      // dispatch(getInfoSuccess(JSON.parse(userData)));
+    }
+  }
+
+  async function getStorage(key: string) {
+    try {
+      const res = await Taro.getStorage({ key });
+      return res.data;
+    } catch {
+      return '';
+    }
+  }
+
   return <Provider store={store}>
     {process.env.TARO_ENV == 'rn' && <StatusBar barStyle='light-content' />}
     {children}

+ 4 - 1
src/services/http/request.ts

@@ -125,7 +125,10 @@ export async function request<T>(param: RequestParam): Promise<T> {
                             return;
                         }
                     }
-                    global.dispatch(logoutSuccess());
+                    if (global.dispatch) {
+                        global.dispatch(logoutSuccess());
+                    }
+
                 } else if (statusCode == 500 && response.data.error_code == 'WX_STEP_PARSE_FAIL') {
                     //单独对计步第一次请求失败处理
                     resolve(response.data);