From 0200ea64d458c32c4ab4f543ec0b786e74f1c7ca Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 24 Nov 2021 15:07:55 +0800 Subject: [PATCH] refactor: reLaunch --- .../src/service/api/route/_reLaunch.ts | 57 +++++++++++++++++++ .../src/service/api/route/reLaunch.ts | 54 +----------------- .../service/framework/app/initGlobalEvent.ts | 15 ++++- .../src/service/framework/app/utils.ts | 16 +----- 4 files changed, 74 insertions(+), 68 deletions(-) create mode 100644 packages/uni-app-plus/src/service/api/route/_reLaunch.ts diff --git a/packages/uni-app-plus/src/service/api/route/_reLaunch.ts b/packages/uni-app-plus/src/service/api/route/_reLaunch.ts new file mode 100644 index 000000000..e448ae565 --- /dev/null +++ b/packages/uni-app-plus/src/service/api/route/_reLaunch.ts @@ -0,0 +1,57 @@ +import { + API_TYPE_RE_LAUNCH, + DefineAsyncApiFn, + ReLaunchOptions, +} from '@dcloudio/uni-api' +import { parseUrl } from '@dcloudio/uni-shared' +import tabBar from '../../framework/app/tabBar' +import { registerPage } from '../../framework/page/register' +import { getAllPages } from '../../framework/page/getCurrentPages' +import { setStatusBarStyle } from '../../statusBar' +import { closePage, navigate, RouteOptions } from './utils' +import { showWebview } from './webview' + +interface ReLaunchOptions extends RouteOptions {} + +export const $reLaunch: DefineAsyncApiFn = ( + { url }, + { resolve, reject } +) => { + const { path, query } = parseUrl(url) + navigate(path, () => { + _reLaunch({ + url, + path, + query, + }) + .then(resolve) + .catch(reject) + }) +} + +function _reLaunch({ url, path, query }: ReLaunchOptions): Promise { + return new Promise((resolve) => { + // 获取目前所有页面 + const pages = getAllPages().slice(0) + const routeOptions = __uniRoutes.find((route) => route.path === path)! + if (routeOptions.meta.isTabBar) { + tabBar.switchTab(path.slice(1)) + } + + showWebview( + registerPage({ + url, + path, + query, + openType: 'reLaunch', + }), + 'none', + 0, + () => { + pages.forEach((page) => closePage(page, 'none')) + resolve(undefined) + } + ) + setStatusBarStyle() + }) +} diff --git a/packages/uni-app-plus/src/service/api/route/reLaunch.ts b/packages/uni-app-plus/src/service/api/route/reLaunch.ts index 6565fad40..d54077afe 100644 --- a/packages/uni-app-plus/src/service/api/route/reLaunch.ts +++ b/packages/uni-app-plus/src/service/api/route/reLaunch.ts @@ -2,33 +2,10 @@ import { API_RE_LAUNCH, API_TYPE_RE_LAUNCH, defineAsyncApi, - DefineAsyncApiFn, ReLaunchOptions, ReLaunchProtocol, } from '@dcloudio/uni-api' -import { parseUrl } from '@dcloudio/uni-shared' -import tabBar from '../../framework/app/tabBar' -import { registerPage } from '../../framework/page' -import { getAllPages } from '../../framework/page/getCurrentPages' -import { setStatusBarStyle } from '../../statusBar' -import { closePage, navigate, RouteOptions } from './utils' -import { showWebview } from './webview' - -export const $reLaunch: DefineAsyncApiFn = ( - { url }, - { resolve, reject } -) => { - const { path, query } = parseUrl(url) - navigate(path, () => { - _reLaunch({ - url, - path, - query, - }) - .then(resolve) - .catch(reject) - }) -} +import { $reLaunch } from './_reLaunch' export const reLaunch = defineAsyncApi( API_RE_LAUNCH, @@ -36,32 +13,3 @@ export const reLaunch = defineAsyncApi( ReLaunchProtocol, ReLaunchOptions ) - -interface ReLaunchOptions extends RouteOptions {} - -function _reLaunch({ url, path, query }: ReLaunchOptions): Promise { - return new Promise((resolve) => { - // 获取目前所有页面 - const pages = getAllPages().slice(0) - const routeOptions = __uniRoutes.find((route) => route.path === path)! - if (routeOptions.meta.isTabBar) { - tabBar.switchTab(path.slice(1)) - } - - showWebview( - registerPage({ - url, - path, - query, - openType: 'reLaunch', - }), - 'none', - 0, - () => { - pages.forEach((page) => closePage(page, 'none')) - resolve(undefined) - } - ) - setStatusBarStyle() - }) -} diff --git a/packages/uni-app-plus/src/service/framework/app/initGlobalEvent.ts b/packages/uni-app-plus/src/service/framework/app/initGlobalEvent.ts index f99c82026..129866741 100644 --- a/packages/uni-app-plus/src/service/framework/app/initGlobalEvent.ts +++ b/packages/uni-app-plus/src/service/framework/app/initGlobalEvent.ts @@ -4,14 +4,16 @@ import { ON_APP_ENTER_FOREGROUND, ON_THEME_CHANGE, ON_KEYBOARD_HEIGHT_CHANGE, + addLeadingSlash, } from '@dcloudio/uni-shared' +import { $reLaunch } from '../../api/route/_reLaunch' import { EVENT_BACKBUTTON, backbuttonListener, parseRedirectInfo, initEnterOptions, getEnterOptions, - initEnterReLaunch, + RedirectInfo, } from './utils' export function initGlobalEvent() { @@ -89,3 +91,14 @@ export function onPlusMessage( ) { UniServiceJSBridge.subscribe('plusMessage.' + type, callback, once) } + +function initEnterReLaunch(info: RedirectInfo) { + __uniConfig.realEntryPagePath = + __uniConfig.realEntryPagePath || __uniConfig.entryPagePath + __uniConfig.entryPagePath = info.path + __uniConfig.entryPageQuery = info.query + $reLaunch( + { url: addLeadingSlash(info.path) + info.query }, + { resolve() {}, reject() {} } + ) +} diff --git a/packages/uni-app-plus/src/service/framework/app/utils.ts b/packages/uni-app-plus/src/service/framework/app/utils.ts index 9d31899f0..7c8ec0255 100644 --- a/packages/uni-app-plus/src/service/framework/app/utils.ts +++ b/packages/uni-app-plus/src/service/framework/app/utils.ts @@ -1,7 +1,6 @@ import { extend } from '@vue/shared' -import { addLeadingSlash, parseQuery } from '@dcloudio/uni-shared' +import { parseQuery } from '@dcloudio/uni-shared' import { createLaunchOptions, LaunchOptions } from '@dcloudio/uni-core' -import { $reLaunch } from '../../api/route/reLaunch' export const EVENT_BACKBUTTON = 'backbutton' @@ -35,17 +34,6 @@ export function initEnterOptions({ }) } -export function initEnterReLaunch(info: RedirectInfo) { - __uniConfig.realEntryPagePath = - __uniConfig.realEntryPagePath || __uniConfig.entryPagePath - __uniConfig.entryPagePath = info.path - __uniConfig.entryPageQuery = info.query - $reLaunch( - { url: addLeadingSlash(info.path) + info.query }, - { resolve() {}, reject() {} } - ) -} - export function initLaunchOptions({ path, query, @@ -60,7 +48,7 @@ export function initLaunchOptions({ return launchOptions } -interface RedirectInfo extends Omit { +export interface RedirectInfo extends Omit { query: string userAction: boolean } -- GitLab