|
@@ -1,4 +1,4 @@
|
|
|
-import { guid } from '@/uilts/help'
|
|
|
+import { guid, isInApp } from '@/uilts/help'
|
|
|
// iframe 通信中心
|
|
|
class IframeMessageCenter {
|
|
|
constructor() {
|
|
@@ -13,10 +13,17 @@ class IframeMessageCenter {
|
|
|
if (callback) {
|
|
|
info.messageId = `${info.actionType}-${guid()}` // 唯一的ID,用于标记回调函数
|
|
|
}
|
|
|
- window.parent.postMessage({
|
|
|
- info,
|
|
|
- data: JSON.parse(JSON.stringify(data))
|
|
|
- }, '*');
|
|
|
+ if (isInApp) {
|
|
|
+ window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
|
+ info,
|
|
|
+ data
|
|
|
+ }))
|
|
|
+ } else {
|
|
|
+ window.parent.postMessage({
|
|
|
+ info,
|
|
|
+ data: JSON.parse(JSON.stringify(data))
|
|
|
+ }, '*');
|
|
|
+ }
|
|
|
if (callback) {
|
|
|
// 带回调callback 的message, 要求携带messageId,callback,failback等
|
|
|
this.listen(info.messageId, callback)
|
|
@@ -43,9 +50,13 @@ class IframeMessageCenter {
|
|
|
|
|
|
init() {
|
|
|
window.addEventListener('message', (e) => {
|
|
|
- let { info = {}, data = {} } = e.data;
|
|
|
+ console.log('on message in webview', e.data)
|
|
|
+ let response = e.data
|
|
|
+ if (isInApp) {
|
|
|
+ response = JSON.parse(e.data)
|
|
|
+ }
|
|
|
+ let { info = {}, data = {} } = response;
|
|
|
let key, item
|
|
|
-
|
|
|
// 如果有messageId && 是自己发送的
|
|
|
if (info.messageId) {
|
|
|
key = info.messageId
|