diff --git a/packages/uni-app-plus/dist/index.v3.js b/packages/uni-app-plus/dist/index.v3.js index 320b7648f5aed49d96114c5e36aab33810c99503..799ce2b193d8a0bc91de7f003afbacaa8c2e8c20 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 80645eff6657f1d09b9fc4d0e6bf5fae20f6a1c7..2294d04705fd7014cca74ce99edaf8714360d041 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 fa83c16de25c435fa5e2a789a999f9f1c9933748..84489307edd14ae2e387dc648289928387c9c983 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 0000000000000000000000000000000000000000..195196e484572035d89178efca4353d4be771ebf --- /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) + } +}