From 8a00c42a924f5a9cc4d65fa6c46049fbfc726d3c Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 8 Dec 2021 15:23:20 +0800 Subject: [PATCH] feat(app): add referrerInfo --- lib/apis.js | 7 +- packages/uni-app-plus/dist/index.v3.js | 153 +++++++++++++++--- .../app-plus/service/framework/app.js | 12 +- .../app-plus/service/framework/config.js | 19 ++- .../app-plus/service/framework/utils.js | 80 +++++++++ .../webview/on-webview-pop-gesture.js | 50 ++++-- 6 files changed, 276 insertions(+), 45 deletions(-) create mode 100644 src/platforms/app-plus/service/framework/utils.js diff --git a/lib/apis.js b/lib/apis.js index 0a6bd7fae..b704bab65 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -244,8 +244,9 @@ const third = [ 'sendNativeEvent', 'preloadPage', 'unPreloadPage', - 'loadSubPackage', - 'sendHostEvent' + 'loadSubPackage', + 'sendHostEvent', + 'navigateToMiniProgram' ] const ad = [ @@ -272,4 +273,4 @@ const apis = [ ...ad ] -module.exports = apis +module.exports = apis diff --git a/packages/uni-app-plus/dist/index.v3.js b/packages/uni-app-plus/dist/index.v3.js index f44a61f1e..4e6bd74bb 100644 --- a/packages/uni-app-plus/dist/index.v3.js +++ b/packages/uni-app-plus/dist/index.v3.js @@ -255,8 +255,9 @@ var serviceContext = (function () { 'sendNativeEvent', 'preloadPage', 'unPreloadPage', - 'loadSubPackage', - 'sendHostEvent' + 'loadSubPackage', + 'sendHostEvent', + 'navigateToMiniProgram' ]; const ad = [ @@ -1702,13 +1703,17 @@ var serviceContext = (function () { "uni.chooseLocation.cancel": "取消" }; - const messages = { - en, - es, - fr, - 'zh-Hans': zhHans, - 'zh-Hant': zhHant - }; + const messages = {}; + + { + Object.assign(messages, { + en, + es, + fr, + 'zh-Hans': zhHans, + 'zh-Hant': zhHant + }); + } let locale; @@ -5802,7 +5807,7 @@ var serviceContext = (function () { animationDuration: 200, uniNView: { path: `${(typeof process === 'object' && process.env && process.env.VUE_APP_TEMPLATE_PATH) || ''}/${url}.js`, - defaultFontSize: plus_.screen.resolutionWidth / 20, + defaultFontSize: 16, viewport: plus_.screen.resolutionWidth } }; @@ -9225,31 +9230,64 @@ var serviceContext = (function () { }); } - function onWebviewPopGesture (webview) { + function onWebviewPopGesture(webview) { let popStartStatusBarStyle; webview.addEventListener('popGesture', e => { if (e.type === 'start') { // 设置下一个页面的 statusBarStyle const pages = getCurrentPages(); - const page = pages[pages.length - 2]; + const page = pages[pages.length - 2]; popStartStatusBarStyle = lastStatusBarStyle; const statusBarStyle = page && page.$page.meta.statusBarStyle; statusBarStyle && setStatusBarStyle(statusBarStyle); } else if (e.type === 'end' && !e.result) { // 拖拽未完成,设置为当前状态栏前景色 setStatusBarStyle(popStartStatusBarStyle); - } else if (e.type === 'end' && e.result) { + } else if (e.type === 'end' && e.result) { const pages = getCurrentPages(); const page = pages[pages.length - 1]; page && page.$remove(); - setStatusBarStyle(); - - UniServiceJSBridge.emit('onAppRoute', { - type: 'navigateBack' - }); + if (page && isDirectPage(page)) { + reLaunchEntryPage(); + } else { + UniServiceJSBridge.emit('onAppRoute', { + type: 'navigateBack' + }); + } } }); + } + + + /** + * 是否处于直达页面 + * @param page + * @returns + */ + function isDirectPage(page) { + return ( + __uniConfig.realEntryPagePath && + page.$page.route === __uniConfig.entryPagePath + ) + } + /** + * 重新启动到首页 + */ + function reLaunchEntryPage() { + __uniConfig.entryPagePath = __uniConfig.realEntryPagePath; + delete __uniConfig.realEntryPagePath; + uni.reLaunch({ + url: addLeadingSlash(__uniConfig.entryPagePath), + }); + } + + function hasLeadingSlash(str) { + return str.indexOf('/') === 0 + } + + function addLeadingSlash(str) { + return hasLeadingSlash(str) ? str : '/' + str } let preloadWebview; @@ -9753,6 +9791,64 @@ var serviceContext = (function () { return pageVm } + const extend = Object.assign; + + function createLaunchOptions () { + return { + path: '', + query: {}, + scene: 1001, + referrerInfo: { + appId: '', + extraData: {} + } + } + } + + const enterOptions = createLaunchOptions(); + const launchOptions = createLaunchOptions(); + + function initLaunchOptions ({ + path, + query, + referrerInfo + }) { + extend(launchOptions, { + path, + query: query ? parseQuery(query) : {}, + referrerInfo: referrerInfo || {} + }); + extend(enterOptions, launchOptions); + return launchOptions + } + + function parseRedirectInfo () { + const weexPlus = weex.requireModule('plus'); + if (weexPlus.getRedirectInfo) { + const { + path, + query, + extraData, + userAction, + fromAppid + } = + weexPlus.getRedirectInfo() || {}; + const referrerInfo = { + appId: fromAppid, + extraData: {} + }; + if (extraData) { + referrerInfo.extraData = extraData; + } + return { + path: path || '', + query: query ? '?' + query : '', + referrerInfo, + userAction + } + } + } + let isInitEntryPage = false; function initEntryPage () { @@ -9767,9 +9863,16 @@ var serviceContext = (function () { const weexPlus = weex.requireModule('plus'); if (weexPlus.getRedirectInfo) { - const info = weexPlus.getRedirectInfo() || {}; - entryPagePath = info.path; - entryPageQuery = info.query ? ('?' + info.query) : ''; + const { + path, + query, + referrerInfo + } = parseRedirectInfo(); + if (path) { + entryPagePath = path; + entryPageQuery = query; + } + __uniConfig.referrerInfo = referrerInfo; } else { const argsJsonStr = plus.runtime.arguments; if (!argsJsonStr) { @@ -21993,11 +22096,11 @@ var serviceContext = (function () { } function initAppLaunch (appVm) { - const args = { + const args = initLaunchOptions({ path: __uniConfig.entryPagePath, - query: {}, - scene: 1001 - }; + query: __uniConfig.entryPageQuery, + referrerInfo: __uniConfig.referrerInfo + }); callAppHook(appVm, 'onLaunch', args); callAppHook(appVm, 'onShow', args); diff --git a/src/platforms/app-plus/service/framework/app.js b/src/platforms/app-plus/service/framework/app.js index b16d5d577..a36b296e3 100644 --- a/src/platforms/app-plus/service/framework/app.js +++ b/src/platforms/app-plus/service/framework/app.js @@ -45,6 +45,10 @@ import { backbuttonListener } from './backbutton' +import { + initLaunchOptions +} from './utils' + let appCtx const defaultApp = { @@ -143,11 +147,11 @@ function onPlusMessage (e) { } function initAppLaunch (appVm) { - const args = { + const args = initLaunchOptions({ path: __uniConfig.entryPagePath, - query: {}, - scene: 1001 - } + query: __uniConfig.entryPageQuery, + referrerInfo: __uniConfig.referrerInfo + }) callAppHook(appVm, 'onLaunch', args) callAppHook(appVm, 'onShow', args) diff --git a/src/platforms/app-plus/service/framework/config.js b/src/platforms/app-plus/service/framework/config.js index ee8be1e17..3ed6143ba 100644 --- a/src/platforms/app-plus/service/framework/config.js +++ b/src/platforms/app-plus/service/framework/config.js @@ -1,3 +1,7 @@ +import { + parseRedirectInfo +} from './utils' + function parseRoutes (config) { __uniRoutes.length = 0 /* eslint-disable no-mixed-operators */ @@ -51,9 +55,16 @@ export function initEntryPage () { const weexPlus = weex.requireModule('plus') if (weexPlus.getRedirectInfo) { - const info = weexPlus.getRedirectInfo() || {} - entryPagePath = info.path - entryPageQuery = info.query ? ('?' + info.query) : '' + const { + path, + query, + referrerInfo + } = parseRedirectInfo() + if (path) { + entryPagePath = path + entryPageQuery = query + } + __uniConfig.referrerInfo = referrerInfo } else { const argsJsonStr = plus.runtime.arguments if (!argsJsonStr) { @@ -90,4 +101,4 @@ export function initEntryPage () { if (process.env.NODE_ENV !== 'production') { console.log(`[uni-app] entryPagePath(${entryPagePath + entryPageQuery})`) } -} +} diff --git a/src/platforms/app-plus/service/framework/utils.js b/src/platforms/app-plus/service/framework/utils.js new file mode 100644 index 000000000..3326becfb --- /dev/null +++ b/src/platforms/app-plus/service/framework/utils.js @@ -0,0 +1,80 @@ +import { + parseQuery +} from 'uni-shared' +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 ? parseQuery(query) : {}, + referrerInfo: referrerInfo || {} + }) +} + +export function initLaunchOptions ({ + path, + query, + referrerInfo +}) { + extend(launchOptions, { + path, + query: query ? parseQuery(query) : {}, + referrerInfo: referrerInfo || {} + }) + extend(enterOptions, launchOptions) + return launchOptions +} + +export function parseRedirectInfo () { + const weexPlus = weex.requireModule('plus') + if (weexPlus.getRedirectInfo) { + const { + path, + query, + extraData, + userAction, + fromAppid + } = + weexPlus.getRedirectInfo() || {} + const referrerInfo = { + appId: fromAppid, + extraData: {} + } + if (extraData) { + referrerInfo.extraData = extraData + } + return { + path: path || '', + query: query ? '?' + query : '', + referrerInfo, + userAction + } + } +} diff --git a/src/platforms/app-plus/service/framework/webview/on-webview-pop-gesture.js b/src/platforms/app-plus/service/framework/webview/on-webview-pop-gesture.js index e883fcab0..f92386d84 100644 --- a/src/platforms/app-plus/service/framework/webview/on-webview-pop-gesture.js +++ b/src/platforms/app-plus/service/framework/webview/on-webview-pop-gesture.js @@ -1,30 +1,62 @@ -import { +import { lastStatusBarStyle, setStatusBarStyle } from '../../bridge' -export function onWebviewPopGesture (webview) { +export function onWebviewPopGesture (webview) { let popStartStatusBarStyle webview.addEventListener('popGesture', e => { if (e.type === 'start') { // 设置下一个页面的 statusBarStyle const pages = getCurrentPages() - const page = pages[pages.length - 2] + const page = pages[pages.length - 2] popStartStatusBarStyle = lastStatusBarStyle const statusBarStyle = page && page.$page.meta.statusBarStyle statusBarStyle && setStatusBarStyle(statusBarStyle) } else if (e.type === 'end' && !e.result) { // 拖拽未完成,设置为当前状态栏前景色 setStatusBarStyle(popStartStatusBarStyle) - } else if (e.type === 'end' && e.result) { + } else if (e.type === 'end' && e.result) { const pages = getCurrentPages() const page = pages[pages.length - 1] page && page.$remove() - setStatusBarStyle() - - UniServiceJSBridge.emit('onAppRoute', { - type: 'navigateBack' - }) + if (page && isDirectPage(page)) { + reLaunchEntryPage() + } else { + UniServiceJSBridge.emit('onAppRoute', { + type: 'navigateBack' + }) + } } }) +} + +/** + * 是否处于直达页面 + * @param page + * @returns + */ +function isDirectPage (page) { + return ( + __uniConfig.realEntryPagePath && + page.$page.route === __uniConfig.entryPagePath + ) +} +/** + * 重新启动到首页 + */ +function reLaunchEntryPage () { + __uniConfig.entryPagePath = __uniConfig.realEntryPagePath + delete __uniConfig.realEntryPagePath + uni.reLaunch({ + url: addLeadingSlash(__uniConfig.entryPagePath) + }) +} + +function hasLeadingSlash (str) { + return str.indexOf('/') === 0 +} + +function addLeadingSlash (str) { + return hasLeadingSlash(str) ? str : '/' + str } -- GitLab