From 73cf6ffdd432ea08dc72f8f9d614673eac225e69 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 15 Mar 2024 19:32:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(x):=20=E4=BF=AE=E5=A4=8D=20iOS=20=E5=9C=A8?= =?UTF-8?q?=20tab=20=E9=A1=B5=E9=9D=A2=E6=89=A7=E8=A1=8C=20redirectTo=20?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E5=A0=86=E6=A0=88=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-app-plus/dist/uni.x.runtime.esm.js | 113 ++++++++++-------- .../src/x/api/route/redirectTo.ts | 33 +++-- .../uni-app-plus/src/x/api/route/switchTab.ts | 3 +- .../uni-app-plus/src/x/api/route/utils.ts | 4 +- .../src/x/framework/app/tabBar.ts | 9 +- 5 files changed, 89 insertions(+), 73 deletions(-) diff --git a/packages/uni-app-plus/dist/uni.x.runtime.esm.js b/packages/uni-app-plus/dist/uni.x.runtime.esm.js index a122048c9a..e5cf49586c 100644 --- a/packages/uni-app-plus/dist/uni.x.runtime.esm.js +++ b/packages/uni-app-plus/dist/uni.x.runtime.esm.js @@ -1936,49 +1936,6 @@ function back(delta, animationType, animationDuration) { var webview = getNativeApp().pageManager.findPageById(currentPage.$page.id + ""); backPage(webview); } -var redirectTo = /* @__PURE__ */ defineAsyncApi(API_REDIRECT_TO, (_ref, _ref2) => { - var { - url - } = _ref; - var { - resolve, - reject - } = _ref2; - var { - path, - query - } = parseUrl(url); - _redirectTo({ - url, - path, - query - }).then(resolve).catch(reject); -}, RedirectToProtocol, RedirectToOptions); -function _redirectTo(_ref3) { - var { - url, - path, - query - } = _ref3; - var lastPage = getCurrentPage(); - if (lastPage) { - removePage(lastPage); - } - return new Promise((resolve) => { - showWebview(registerPage({ - url, - path, - query, - openType: "redirectTo" - }), "none", 0, () => { - if (lastPage) { - closeWebview(lastPage.$nativePage, "none"); - } - resolve(void 0); - setStatusBarStyle(); - }); - }); -} function hasLeadingSlash(str) { return str.indexOf("/") == 0; } @@ -2063,6 +2020,15 @@ function init() { page.startRender(); page.show(null); } +function removeTabBarPage(page) { + var pagePath = getRealPath(page.route, true); + if (tabs.get(pagePath) === page) { + tabs.delete(pagePath); + if (getTabIndex(pagePath) === selected0) { + selected0 = -1; + } + } +} function getTabBar() { return tabBar0; } @@ -2081,12 +2047,10 @@ function getTabIndex(path) { } return selected; } -var currentPageRoute = null; function findPageRoute(path) { return __uniRoutes.find((route) => route.path === path); } function createTab(path, query, callback) { - currentPageRoute = findPageRoute(path); showWebview(registerPage({ url: path, path, @@ -2094,7 +2058,6 @@ function createTab(path, query, callback) { openType: "switchTab" }), "none", 0, callback); var page = getCurrentPage(); - currentPageRoute = null; tabBar0.appendItem(page.$page.id.toString()); return page; } @@ -2113,9 +2076,6 @@ function findTabPage(path) { return page; } function isTabPage(page) { - if (page.$route === currentPageRoute) { - return true; - } var has2 = false; tabs.forEach((value, key) => { if (value === page) { @@ -2167,8 +2127,59 @@ function switchSelect(selected, path) { selected0 = selected; } function closePage(page, animationType, animationDuration) { - removePage(page); closeWebview(page.$nativePage, animationType, animationDuration); + removePage(page); + removeTabBarPage(page); +} +var redirectTo = /* @__PURE__ */ defineAsyncApi(API_REDIRECT_TO, (_ref, _ref2) => { + var { + url + } = _ref; + var { + resolve, + reject + } = _ref2; + var { + path, + query + } = parseUrl(url); + _redirectTo({ + url, + path, + query + }).then(resolve).catch(reject); +}, RedirectToProtocol, RedirectToOptions); +function _redirectTo(_ref3) { + var { + url, + path, + query + } = _ref3; + var lastPage = getCurrentPage(); + return new Promise((resolve) => { + showWebview(registerPage({ + url, + path, + query, + openType: isTabPage(lastPage) || getAllPages().length === 1 ? "reLaunch" : "redirectTo" + }), "none", 0, () => { + if (lastPage) { + removePages(lastPage); + } + resolve(void 0); + setStatusBarStyle(); + }); + }); +} +function removePages(currentPage) { + if (isTabPage(currentPage)) { + var pages2 = getAllPages().slice(0, -1); + pages2.forEach((page) => { + closePage(page, "none"); + }); + } else { + closePage(currentPage, "none"); + } } var $switchTab = (args, _ref) => { var { @@ -2199,7 +2210,7 @@ function _switchTab(_ref2) { if (selected == -1) { return Promise.reject("tab ".concat(path, " not found")); } - var pages2 = getAllPages(); + var pages2 = getCurrentPages(); switchSelect(selected, path, query); for (var index2 = pages2.length - 1; index2 >= 0; index2--) { var page = pages2[index2]; diff --git a/packages/uni-app-plus/src/x/api/route/redirectTo.ts b/packages/uni-app-plus/src/x/api/route/redirectTo.ts index 437937b9d9..ba36e58a9c 100644 --- a/packages/uni-app-plus/src/x/api/route/redirectTo.ts +++ b/packages/uni-app-plus/src/x/api/route/redirectTo.ts @@ -7,12 +7,14 @@ import { } from '@dcloudio/uni-api' import { parseUrl } from '@dcloudio/uni-shared' import { getCurrentPage } from '@dcloudio/uni-core' -import { removePage } from '../../../service/framework/page/getCurrentPages' +import { getAllPages } from '../../../service/framework/page/getCurrentPages' import { registerPage } from '../../framework/page' import { RouteOptions } from '../../../service/api/route/utils' -import { closeWebview, showWebview } from './webview' +import { showWebview } from './webview' import { ComponentPublicInstance } from 'vue' import { setStatusBarStyle } from '../../statusBar' +import { isTabPage } from '../../framework/app/tabBar' +import { closePage } from './utils' export const redirectTo = defineAsyncApi( API_REDIRECT_TO, @@ -37,10 +39,8 @@ function _redirectTo({ path, query, }: RedirectToOptions): Promise { - const lastPage = getCurrentPage() - if (lastPage) { - removePage(lastPage) - } + const lastPage = getCurrentPage() as ComponentPublicInstance + // 与 uni-app x 安卓一致,后移除页面 return new Promise((resolve) => { showWebview( @@ -48,16 +48,16 @@ function _redirectTo({ url, path, query, - openType: 'redirectTo', + openType: + isTabPage(lastPage) || getAllPages().length === 1 + ? 'reLaunch' + : 'redirectTo', }), 'none', 0, () => { if (lastPage) { - closeWebview( - (lastPage as ComponentPublicInstance).$nativePage!, - 'none' - ) + removePages(lastPage) } resolve(undefined) setStatusBarStyle() @@ -65,3 +65,14 @@ function _redirectTo({ ) }) } + +function removePages(currentPage: ComponentPublicInstance) { + if (isTabPage(currentPage)) { + const pages = getAllPages().slice(0, -1) + pages.forEach((page) => { + closePage(page, 'none') + }) + } else { + closePage(currentPage, 'none') + } +} diff --git a/packages/uni-app-plus/src/x/api/route/switchTab.ts b/packages/uni-app-plus/src/x/api/route/switchTab.ts index ce20dbb6bb..a311af0713 100644 --- a/packages/uni-app-plus/src/x/api/route/switchTab.ts +++ b/packages/uni-app-plus/src/x/api/route/switchTab.ts @@ -14,7 +14,6 @@ import { switchSelect, } from '../../framework/app/tabBar' import { ComponentPublicInstance } from 'vue' -import { getAllPages } from '../../../service/framework/page/getCurrentPages' import { closePage } from './utils' export const $switchTab: DefineAsyncApiFn = ( @@ -46,7 +45,7 @@ function _switchTab({ url, path, query }: SwitchTabOptions) { if (selected == -1) { return Promise.reject(`tab ${path} not found`) } - const pages = getAllPages() + const pages = getCurrentPages() switchSelect(selected, path, query) for (let index = pages.length - 1; index >= 0; index--) { const page = pages[index] as ComponentPublicInstance diff --git a/packages/uni-app-plus/src/x/api/route/utils.ts b/packages/uni-app-plus/src/x/api/route/utils.ts index c1d316ab12..44035e24c4 100644 --- a/packages/uni-app-plus/src/x/api/route/utils.ts +++ b/packages/uni-app-plus/src/x/api/route/utils.ts @@ -1,12 +1,14 @@ import { ComponentPublicInstance } from 'vue' import { removePage } from '../../../service/framework/page/getCurrentPages' import { closeWebview } from './webview' +import { removeTabBarPage } from '../../framework/app/tabBar' export function closePage( page: ComponentPublicInstance, animationType: string, animationDuration?: number ) { - removePage(page) closeWebview(page.$nativePage!, animationType, animationDuration) + removePage(page) + removeTabBarPage(page) } diff --git a/packages/uni-app-plus/src/x/framework/app/tabBar.ts b/packages/uni-app-plus/src/x/framework/app/tabBar.ts index a439c797dd..da50f0f919 100644 --- a/packages/uni-app-plus/src/x/framework/app/tabBar.ts +++ b/packages/uni-app-plus/src/x/framework/app/tabBar.ts @@ -127,7 +127,7 @@ export function clearTabBarStatus() { } export function removeTabBarPage(page: Page) { - const pagePath = getRealPath(page.$route?.path ?? '', true) + const pagePath = getRealPath(page.route, true) if (tabs.get(pagePath) === page) { tabs.delete(pagePath) if (getTabIndex(pagePath) === selected0) { @@ -159,8 +159,6 @@ export function getTabIndex(path: string, list = getTabList()): number { return selected } -let currentPageRoute: unknown = null - function findPageRoute(path: string) { return __uniRoutes.find((route) => route.path === path)! } @@ -170,7 +168,6 @@ function createTab( query: Record, callback?: () => void ): Page { - currentPageRoute = findPageRoute(path) showWebview( registerPage({ url: path, path, query, openType: 'switchTab' }), 'none', @@ -178,7 +175,6 @@ function createTab( callback ) const page = getCurrentPage() as Page - currentPageRoute = null tabBar0!.appendItem(page!.$page.id.toString()) return page } @@ -209,9 +205,6 @@ function findTabPage(path: string): Page | null { } export function isTabPage(page: Page): boolean { - if (page.$route === currentPageRoute) { - return true - } let has = false tabs.forEach((value: Page, key: string) => { if (value === page) { -- GitLab