ソースを参照

add app message send

jihuaqiang 2 年 前
コミット
b5da79ddc6
2 ファイル変更16 行追加5 行削除
  1. 2 0
      src/uilts/help.js
  2. 14 5
      src/uilts/messageCenter/index.js

+ 2 - 0
src/uilts/help.js

@@ -16,6 +16,8 @@ export function getQueryStringByUrl(url = "", name = "") {
   return null;
 }
 
+export const isInApp = !!navigator.userAgent.includes('denet');
+
 export function debounce(fn, delay) {
   let timer; // 定时器
   return function (...args) {

+ 14 - 5
src/uilts/messageCenter/index.js

@@ -1,4 +1,5 @@
 //  iframe 通信中心
+import { isInApp } from '@/uilts/help'
 class MessageCenter { 
     constructor() { 
         //  缓存事件队列
@@ -8,10 +9,18 @@ class MessageCenter {
     }
 
     send({ actionType, data, callback, overTime, failback }) {
-        window.parent.postMessage({
-            actionType,
-            data
-        }, '*');
+        if (isInApp) {
+            window.ReactNativeWebView.postMessage({
+                actionType,
+                data
+            });
+        } else { 
+            window.parent.postMessage({
+                actionType,
+                data
+            }, '*');
+        }
+        
         if (data.messageID && callback) {
             // 带回调callback 的message, 要求携带messageID,callback,failback等
             this.listen(`${actionType}-${data.messageID}`, callback)
@@ -74,7 +83,7 @@ class MessageCenter {
                 }
             }
 
-        }, 1000)
+        }, 500)
     }
 }