Jelajahi Sumber

feat: 减少环境下不必要的执行

linwenxin 1 tahun lalu
induk
melakukan
9f82085cbe
1 mengubah file dengan 45 tambahan dan 54 penghapusan
  1. 45 54
      src/common/utils/navPag.js

+ 45 - 54
src/common/utils/navPag.js

@@ -90,20 +90,7 @@ export const navToPage = (function() {
   }
 })()
 
-// var curRouteH5 = {}
-// var curRouteMini = {}
-
 export function webViewHref(url, pam = {}, crossPagePam = {}) {
-
-
-  // if(crossPagePam.crossPageTs){
-  //   var funs = Object.keys(JSON.parse(decodeURIComponent(crossPagePam.crossPageTs)))
-  //   var H5 = Object.keys(curRouteH5).find(_url=>!!~url.indexOf(_url))
-  //   var Mini = Object.keys(curRouteMini).find(_url=>!!~url.indexOf(_url))
-  // }else{
-
-  // }
-
   var defaultPam = {
     ...pam,
     ...crossPagePam,
@@ -126,8 +113,6 @@ export function webViewHref(url, pam = {}, crossPagePam = {}) {
   }
 }
 
-
-
 export const crossPage = (function() {
   var funs = {}
   var tiemIdIs = {}
@@ -164,6 +149,7 @@ export const crossPage = (function() {
 
   var funco = {
     $emit(name, data) {
+      // 同个环境下可以触发
       if (funs[name]) {
         Object.keys(funs[name]).map(key => {
           funs[name][key](data)
@@ -171,21 +157,34 @@ export const crossPage = (function() {
       }
 
       // #ifndef H5
+      // 用于小程序触发更新H5页面
       datacd.push([name, data])
       delaydeparture()
       // #endif
 
       // #ifdef H5
-      var postMessage = (window.uniWebview && window.uniWebview.postMessage) || (window.postMessage)
-      if (postMessage) {
-        postMessage({
-          data: {
-            type: "crossPageTs",
-            name,
-            data
+      // h5在小程序环境下通过定时器传送
+      wx.miniProgram.getEnv(res => {
+        if (res.miniprogram) {
+          var postMessage = (window.uniWebview && window.uniWebview.postMessage) || (window.postMessage)
+          if (postMessage) {
+            postMessage({
+              data: {
+                type: "crossPageTs",
+                name,
+                data
+              }
+            })
           }
-        })
-      }
+          if (tiemIdIs[name]) {
+            clearTimeout(tiemIdIs[name])
+          }
+          uni.setStorageSync(name, data);
+          tiemIdIs[name] = setTimeout(function() {
+            uni.removeStorageSync(name)
+          }, 350)
+        }
+      })
       // #endif
     },
 
@@ -193,40 +192,11 @@ export const crossPage = (function() {
       if (!funs[name]) {
         funs[name] = {}
       }
-      let funcName = md5(fun.toString())
-      funs[name][funcName] = fun;
+      funs[name][md5(fun.toString())] = fun
 
       // #ifdef H5
       delaydepartFun()
       // #endif
-
-      // -----------------------------------
-      // (function getrouter() {
-      //   let routes = getCurrentPages();
-      //   if (routes.length) {
-      //     let curRoute = routes[routes.length - 1].route
-      //     wx.miniProgram.getEnv(res => {
-      //       if (res.miniprogram) {
-      //         if (!curRouteMini[curRoute]) {
-      //           curRouteMini[curRoute] = []
-      //         }
-      //         if (!!~curRouteMini[curRoute].indexOf(name)) {
-      //           curRouteMini[curRoute].push(name)
-      //         }
-      //       } else {
-      //         if (!curRouteH5[curRoute]) {
-      //           curRouteH5[curRoute] = []
-      //         }
-      //         if (!!~curRouteH5[curRoute].indexOf(name)) {
-      //           curRouteH5[curRoute].push(name)
-      //         }
-      //       }
-      //     })
-
-      //   } else {
-      //     setTimeout(getrouter, 300)
-      //   }
-      // })();
     },
 
     $off(name, fun) {
@@ -256,5 +226,26 @@ export const crossPage = (function() {
     },
     // #endif
   };
+
+  // #ifdef H5
+  wx.miniProgram.getEnv(res => {
+    if (res.miniprogram) {
+      (function tiemID() {
+        Object.keys(funs).map(name => {
+          if (funs[name]) {
+            var data = uni.getStorageSync(name);
+            if (data) {
+              Object.keys(funs[name]).map(key => {
+                funs[name][key](data)
+              })
+            }
+          }
+        })
+        setTimeout(tiemID, 200)
+      })()
+    }
+  })
+  // #endif
+
   return funco
 })()