From b0e7ac94d741c8c1839f5a457931be59f1fa6db0 Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Thu, 28 Nov 2019 14:24:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20webview=E7=BB=84=E4=BB=B6=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit webview在canBack时应优先返回webview内的页面 --- src/platforms/app-plus/constants.js | 2 ++ .../service/api/route/navigate-back.js | 19 +++++++++++-------- .../framework/subscribe-handlers/index.js | 14 ++++++++++++-- .../on-webview-lifecycle.js | 16 ++++++++++++++++ .../view/components/web-view/index.vue | 8 +++++++- 5 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 src/platforms/app-plus/service/framework/subscribe-handlers/on-webview-lifecycle.js diff --git a/src/platforms/app-plus/constants.js b/src/platforms/app-plus/constants.js index f4391ff0c..722431bcb 100644 --- a/src/platforms/app-plus/constants.js +++ b/src/platforms/app-plus/constants.js @@ -16,3 +16,5 @@ export const WEBVIEW_UI_EVENT = 'webviewUIEvent' export const VD_SYNC_CALLBACK = 'vdSyncCallback' export const INVOKE_API = 'invokeApi' export const WEB_INVOKE_APPSERVICE = 'WEB_INVOKE_APPSERVICE' +export const WEBVIEW_INSERTED = 'webviewInserted' +export const WEBVIEW_REMOVED = 'webviewRemoved' diff --git a/src/platforms/app-plus/service/api/route/navigate-back.js b/src/platforms/app-plus/service/api/route/navigate-back.js index d975e9792..f20e65466 100644 --- a/src/platforms/app-plus/service/api/route/navigate-back.js +++ b/src/platforms/app-plus/service/api/route/navigate-back.js @@ -22,9 +22,6 @@ function quit () { } function backWebview (webview, callback) { - if (!webview.__uniapp_webview) { - return callback() - } const children = webview.children() if (!children || !children.length) { // 有子 webview return callback() @@ -53,14 +50,14 @@ function back (delta, animationType, animationDuration) { }) } - backWebview(currentPage, () => { + const backPage = function () { if (animationType) { - currentPage.$getAppWebview().close(animationType, animationDuration || ANI_DURATION) + webview.close(animationType, animationDuration || ANI_DURATION) } else { if (currentPage.$page.openType === 'redirect') { // 如果是 redirectTo 跳转的,需要制定 back 动画 - currentPage.$getAppWebview().close(ANI_CLOSE, ANI_DURATION) + webview.close(ANI_CLOSE, ANI_DURATION) } - currentPage.$getAppWebview().close('auto') + webview.close('auto') } pages.slice(len - delta, len).forEach(page => page.$remove()) @@ -70,7 +67,13 @@ function back (delta, animationType, animationDuration) { UniServiceJSBridge.emit('onAppRoute', { type: 'navigateBack' }) - }) + } + + if (!currentPage.__uniapp_webview) { + return backPage() + } + const webview = currentPage.$getAppWebview() + backWebview(webview, backPage) } export function navigateBack ({ 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 1a3583025..9c5ccd239 100644 --- a/src/platforms/app-plus/service/framework/subscribe-handlers/index.js +++ b/src/platforms/app-plus/service/framework/subscribe-handlers/index.js @@ -5,7 +5,9 @@ import { VD_SYNC_CALLBACK, INVOKE_API, WEBVIEW_READY, - WEB_INVOKE_APPSERVICE + WEB_INVOKE_APPSERVICE, + WEBVIEW_INSERTED, + WEBVIEW_REMOVED } from '../../../constants' import { @@ -20,6 +22,11 @@ import onVdSyncCallback from './on-vd-sync-callback' import onInvokeApi from './on-invoke-api' import onWebInvokeAppService from './on-web-invoke-app-service' import onWxsInvokeCallMethod from './on-wxs-invoke-call-method' + +import { + onWebviewInserted, + onWebviewRemoved +} from './on-webview-lifecycle' export function initSubscribeHandlers () { const { @@ -59,5 +66,8 @@ export function initSubscribeHandlers () { subscribe(VD_SYNC, onVdSync) subscribe(VD_SYNC_CALLBACK, onVdSyncCallback) - subscribe(INVOKE_API, onInvokeApi) + subscribe(INVOKE_API, onInvokeApi) + + subscribe(WEBVIEW_INSERTED, onWebviewInserted) + subscribe(WEBVIEW_REMOVED, onWebviewRemoved) } diff --git a/src/platforms/app-plus/service/framework/subscribe-handlers/on-webview-lifecycle.js b/src/platforms/app-plus/service/framework/subscribe-handlers/on-webview-lifecycle.js new file mode 100644 index 000000000..7923919d3 --- /dev/null +++ b/src/platforms/app-plus/service/framework/subscribe-handlers/on-webview-lifecycle.js @@ -0,0 +1,16 @@ +function findPage (pageId) { + pageId = parseInt(pageId) + const page = getCurrentPages(true).find(page => page.$page.id === pageId) + if (!page) { + return console.error(`Page[${pageId}] not found`) + } + return page +} +export function onWebviewInserted (data, pageId) { + const page = findPage(pageId) + page && (page.__uniapp_webview = true) +} +export function onWebviewRemoved (data, pageId) { + const page = findPage(pageId) + page && (delete page.__uniapp_webview) +} diff --git a/src/platforms/app-plus/view/components/web-view/index.vue b/src/platforms/app-plus/view/components/web-view/index.vue index fab432ebe..f01b4ca96 100644 --- a/src/platforms/app-plus/view/components/web-view/index.vue +++ b/src/platforms/app-plus/view/components/web-view/index.vue @@ -1,7 +1,11 @@ - -- GitLab