提交 f43230ed 编写于 作者: fxy060608's avatar fxy060608

feat(v3): postMessage

上级 3321b0aa
......@@ -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);
......
......@@ -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'))
}
......@@ -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)
......
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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册