Browse Source

feat: 处理主页数据异步问题

huangzhichao 1 year ago
parent
commit
b23c905fa2
3 changed files with 14 additions and 9 deletions
  1. 3 3
      src/http/index.ts
  2. 4 2
      src/plugin/components/custom/index.tsx
  3. 7 4
      src/plugin/index.ts

+ 3 - 3
src/http/index.ts

@@ -64,9 +64,9 @@ class Http {
 }
 
 export function wrapData(params) {
-  const systemInfo = Taro.$global?.systemInfo
-  const launchOption = Taro.$global?.launchOption
-  const network = Taro.$global?.network
+  const systemInfo = Taro.$global?.systemInfo || {}
+  const launchOption = Taro.$global?.launchOption || {}
+  const network = Taro.$global?.network || {}
 
   const defaultInfo = {
     appId: APP_ID,

+ 4 - 2
src/plugin/components/custom/index.tsx

@@ -15,7 +15,9 @@ import AES from 'crypto-js/aes.js'
 let loadedMetaFiredInOnce = 0
 let pqtId
 
-function PqCustom(props: PropsWithChildren<CustomPropsType>) {
+console.log('YLQCustom', Taro.$global)
+
+function YLQCustom(props: PropsWithChildren<CustomPropsType>) {
   let {
     adpId,
     width,
@@ -284,4 +286,4 @@ function Custom({
   )
 }
 
-export default PqCustom
+export default YLQCustom

+ 7 - 4
src/plugin/index.ts

@@ -1,14 +1,15 @@
 import Taro from '@tarojs/taro'
 
-async function loadPlugin() {
-  Taro.$global = await createTaroGlobal()
+function loadPlugin() {
+  Taro.$global = createTaroGlobal()
+  console.log('main', Taro.$global)
   listenNetwork()
 }
 
 async function createTaroGlobal() {
   const global = {
     systemInfo: Taro.getSystemInfoSync() || {},
-    network: await Taro.getNetworkType() || {},
+    network: {},
     launchOption: {}
   }
 
@@ -21,7 +22,9 @@ async function createTaroGlobal() {
   return global
 }
 
-function listenNetwork() {
+async function listenNetwork() {
+  Taro.$global.network = await Taro.getNetworkType() || {}
+
   Taro.onNetworkStatusChange(({ networkType }) => {
     Taro.$global.network.networkType = networkType
   })