From 2aa9cd32085cfbd663cf63b611499362a8b33537 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Thu, 7 Jul 2022 16:05:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(h5):=20getLaunchOptionsSync=E3=80=81getEnte?= =?UTF-8?q?rOptionsSync=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/service/bridge/on.js | 7 +- src/core/service/plugins/app/index.js | 76 +++++++++++++------ .../api/plugin/get-launch-options-sync.js | 7 +- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/src/core/service/bridge/on.js b/src/core/service/bridge/on.js index cb866820d..9aef79595 100644 --- a/src/core/service/bridge/on.js +++ b/src/core/service/bridge/on.js @@ -9,6 +9,8 @@ import { import onWebInvokeAppService from 'uni-platform/service/on-web-invoke-app-service' +import { initEnterOptions, getEnterOptions } from '../plugins/app' + export default function initOn (on, { getApp, getCurrentPages @@ -53,7 +55,8 @@ export default function initOn (on, { } function onAppEnterForeground (enterOptions) { - callAppHook(getApp(), 'onShow', enterOptions) + initEnterOptions(enterOptions) + callAppHook(getApp(), 'onShow', getEnterOptions()) const pages = getCurrentPages() if (pages.length === 0) { return @@ -99,4 +102,4 @@ export default function initOn (on, { on('onNavigationBarSearchInputFocusChanged', createCallCurrentPageHook('onNavigationBarSearchInputFocusChanged')) on('onWebInvokeAppService', onWebInvokeAppService) -} +} diff --git a/src/core/service/plugins/app/index.js b/src/core/service/plugins/app/index.js index edc8540b1..4585bd7c2 100644 --- a/src/core/service/plugins/app/index.js +++ b/src/core/service/plugins/app/index.js @@ -10,29 +10,57 @@ export { } from './create-app' -export let createLaunchOptions = function () { - const scene = 1001 - const referrerInfo = { - appId: '', - extraData: {} - } - try { - return { - path: this.$route.meta && this.$route.meta.pagePath, - query: this.$route.query, - scene, - referrerInfo - } - } catch (error) { - return { - path: '', - query: {}, - scene, - referrerInfo +const extend = Object.assign + +function createLaunchOptions () { + return { + path: '', + query: {}, + scene: 1001, + referrerInfo: { + appId: '', + extraData: {} } } } +const enterOptions = createLaunchOptions() +const launchOptions = createLaunchOptions() + +export function getLaunchOptions () { + return launchOptions +} + +export function getEnterOptions () { + return enterOptions +} + +export function initEnterOptions ({ + path, + query, + referrerInfo +}) { + extend(enterOptions, { + path, + query: query || {}, + referrerInfo: referrerInfo || {} + }) +} + +export function initLaunchOptions ({ + path, + query, + referrerInfo +}) { + extend(launchOptions, { + path, + query: query || {}, + referrerInfo: referrerInfo || {} + }) + extend(enterOptions, launchOptions) + return launchOptions +} + export function createAppMixin (Vue, routes, entryRoute) { return { created: function AppCreated () { @@ -54,10 +82,12 @@ export function createAppMixin (Vue, routes, entryRoute) { }, mounted: function appMounted () { // 稍微靠后点,让 App 有机会在 mounted 事件前注册一些全局事件监听,如 UI 显示(showModal) - createLaunchOptions = createLaunchOptions.bind(this) - const args = createLaunchOptions() - callAppHook(this, 'onLaunch', args) - callAppHook(this, 'onShow', args) + initLaunchOptions({ + path: this.$route.meta && this.$route.meta.pagePath, + query: this.$route.query + }) + callAppHook(this, 'onLaunch', launchOptions) + callAppHook(this, 'onShow', enterOptions) } } } diff --git a/src/platforms/h5/service/api/plugin/get-launch-options-sync.js b/src/platforms/h5/service/api/plugin/get-launch-options-sync.js index dba99a144..a65ad9981 100644 --- a/src/platforms/h5/service/api/plugin/get-launch-options-sync.js +++ b/src/platforms/h5/service/api/plugin/get-launch-options-sync.js @@ -1,10 +1,11 @@ import { - createLaunchOptions + getLaunchOptions, + getEnterOptions } from 'uni-core/service/plugins/app' export function getLaunchOptionsSync () { - return createLaunchOptions() + return getLaunchOptions() } export function getEnterOptionsSync () { - return createLaunchOptions() + return getEnterOptions() } -- GitLab