From f43230ed9ff6554a10fbb5085e78ef98be691373 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 31 Oct 2019 20:23:21 +0800 Subject: [PATCH] feat(v3): postMessage --- packages/uni-app-plus/dist/index.v3.js | 48 ++++++++++++++----- src/core/service/bridge/on.js | 15 +----- .../framework/subscribe-handlers/index.js | 2 + .../on-web-invoke-app-service.js | 33 +++++++++++++ 4 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 src/platforms/app-plus/service/framework/subscribe-handlers/on-web-invoke-app-service.js diff --git a/packages/uni-app-plus/dist/index.v3.js b/packages/uni-app-plus/dist/index.v3.js index 320b7648f5..799ce2b193 100644 --- a/packages/uni-app-plus/dist/index.v3.js +++ b/packages/uni-app-plus/dist/index.v3.js @@ -9326,15 +9326,6 @@ var serviceContext = (function () { callCurrentPageHook('onShow'); } - function onWebInvokeAppService ({ - name, - arg - }, pageIds) { - if (name === 'postMessage') ; else { - uni[name](arg); - } - } - const routeHooks = { navigateTo () { callCurrentPageHook('onHide'); @@ -9368,9 +9359,7 @@ var serviceContext = (function () { on('onNavigationBarSearchInputChanged', createCallCurrentPageHook('onNavigationBarSearchInputChanged')); on('onNavigationBarSearchInputConfirmed', createCallCurrentPageHook('onNavigationBarSearchInputConfirmed')); - on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked')); - - on('onWebInvokeAppService', onWebInvokeAppService); + on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked')); } function initSubscribe (subscribe, { @@ -9526,6 +9515,40 @@ var serviceContext = (function () { uni[method] && uni[method](args); } + function onMessage (pageId, arg) { + pageId = parseInt(pageId); + const page = getCurrentPages(true).find(page => page.$page.id === pageId); + if (!page) { + return + } + if (!page.$page.meta.isNVue) { + const target = page.$vm._$vd.elements.find(target => target.tagName === 'web-view' && target.events['message']); + if (!target) { + return + } + target.dispatchEvent('message', { + type: 'message', + target: Object.create(null), + currentTarget: Object.create(null), + timeStamp: Date.now(), + detail: { + data: [arg] + } + }); + } + } + + function onWebInvokeAppService ({ + name, + arg + }, pageIds) { + if (name === 'postMessage') { + onMessage(pageIds[0], arg); + } else { + uni[name](arg); + } + } + function initSubscribeHandlers () { const { on, @@ -9556,6 +9579,7 @@ var serviceContext = (function () { on('api.' + WEB_INVOKE_APPSERVICE$1, function (data, webviewIds) { emit('onWebInvokeAppService', data, webviewIds); }); + on('onWebInvokeAppService', onWebInvokeAppService); subscribe(VD_SYNC, onVdSync); subscribe(VD_SYNC_CALLBACK, onVdSyncCallback); diff --git a/src/core/service/bridge/on.js b/src/core/service/bridge/on.js index 80645eff66..2294d04705 100644 --- a/src/core/service/bridge/on.js +++ b/src/core/service/bridge/on.js @@ -50,17 +50,6 @@ export default function initOn (on, { callCurrentPageHook('onShow') } - function onWebInvokeAppService ({ - name, - arg - }, pageIds) { - if (name === 'postMessage') { - // TODO 小程序后退、组件销毁、分享时通知 - } else { - uni[name](arg) - } - } - const routeHooks = { navigateTo () { callCurrentPageHook('onHide') @@ -94,7 +83,5 @@ export default function initOn (on, { on('onNavigationBarSearchInputChanged', createCallCurrentPageHook('onNavigationBarSearchInputChanged')) on('onNavigationBarSearchInputConfirmed', createCallCurrentPageHook('onNavigationBarSearchInputConfirmed')) - on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked')) - - on('onWebInvokeAppService', onWebInvokeAppService) + on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked')) } diff --git a/src/platforms/app-plus/service/framework/subscribe-handlers/index.js b/src/platforms/app-plus/service/framework/subscribe-handlers/index.js index fa83c16de2..84489307ed 100644 --- a/src/platforms/app-plus/service/framework/subscribe-handlers/index.js +++ b/src/platforms/app-plus/service/framework/subscribe-handlers/index.js @@ -18,6 +18,7 @@ import onVdSync from './on-vd-sync' import onVdSyncCallback from './on-vd-sync-callback' import onInvokeApi from './on-invoke-api' +import onWebInvokeAppService from './on-web-invoke-app-service' export function initSubscribeHandlers () { const { @@ -49,6 +50,7 @@ export function initSubscribeHandlers () { on('api.' + WEB_INVOKE_APPSERVICE, function (data, webviewIds) { emit('onWebInvokeAppService', data, webviewIds) }) + on('onWebInvokeAppService', onWebInvokeAppService) subscribe(VD_SYNC, onVdSync) subscribe(VD_SYNC_CALLBACK, onVdSyncCallback) diff --git a/src/platforms/app-plus/service/framework/subscribe-handlers/on-web-invoke-app-service.js b/src/platforms/app-plus/service/framework/subscribe-handlers/on-web-invoke-app-service.js new file mode 100644 index 0000000000..195196e484 --- /dev/null +++ b/src/platforms/app-plus/service/framework/subscribe-handlers/on-web-invoke-app-service.js @@ -0,0 +1,33 @@ +function onMessage (pageId, arg) { + pageId = parseInt(pageId) + const page = getCurrentPages(true).find(page => page.$page.id === pageId) + if (!page) { + return + } + if (!page.$page.meta.isNVue) { + const target = page.$vm._$vd.elements.find(target => target.tagName === 'web-view' && target.events['message']) + if (!target) { + return + } + target.dispatchEvent('message', { + type: 'message', + target: Object.create(null), + currentTarget: Object.create(null), + timeStamp: Date.now(), + detail: { + data: [arg] + } + }) + } +} + +export default function onWebInvokeAppService ({ + name, + arg +}, pageIds) { + if (name === 'postMessage') { + onMessage(pageIds[0], arg) + } else { + uni[name](arg) + } +} -- GitLab