From 2b18c3f138577f983010bb0021afed755e747580 Mon Sep 17 00:00:00 2001 From: Hooray <304327508@qq.com> Date: Sat, 29 Mar 2025 01:53:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor!:=20=E9=87=8D=E5=86=99=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=20`push/go/replace`=20=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20useTabbar=20=E4=B8=AD=20`open/go/replace`?= =?UTF-8?q?=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/extensions.ts | 64 ++++++++++++++++++++++++++++++ src/router/index.ts | 3 +- src/utils/composables/useTabbar.ts | 24 ----------- 3 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 src/router/extensions.ts diff --git a/src/router/extensions.ts b/src/router/extensions.ts new file mode 100644 index 00000000..941d92cf --- /dev/null +++ b/src/router/extensions.ts @@ -0,0 +1,64 @@ +import type { RouteLocationRaw, Router } from 'vue-router' +import pinia from '@/store' +import useSettingsStore from '@/store/modules/settings' +import useTabbarStore from '@/store/modules/tabbar' + +function getId(router: Router) { + return router.currentRoute.value.fullPath +} + +function extendPush(router: Router) { + const originalPush = router.push + router.push = function (to: RouteLocationRaw) { + const settingsStore = useSettingsStore(pinia) + if (settingsStore.settings.tabbar.enable) { + const tabbarStore = useTabbarStore(pinia) + const index = tabbarStore.list.findIndex(item => item.tabId === getId(router)) + tabbarStore.$patch({ + leaveIndex: index, + }) + } + return originalPush(to) + } +} + +function extendGo(router: Router) { + const originalGo = router.go + router.go = function (delta: number) { + const settingsStore = useSettingsStore(pinia) + if (settingsStore.settings.tabbar.enable) { + const tabId = getId(router) + const tabbarStore = useTabbarStore(pinia) + originalGo(delta) + if (delta < 0) { + tabbarStore.remove(tabId) + } + } + else { + originalGo(delta) + } + } +} + +function extendReplace(router: Router) { + const originalReplace = router.replace + router.replace = function (to: RouteLocationRaw) { + const settingsStore = useSettingsStore(pinia) + if (settingsStore.settings.tabbar.enable) { + const tabId = getId(router) + const tabbarStore = useTabbarStore(pinia) + return originalReplace(to).then(() => { + tabbarStore.remove(tabId) + }) + } + else { + return originalReplace(to) + } + } +} + +export default function setupExtensions(router: Router) { + extendPush(router) + extendGo(router) + extendReplace(router) +} diff --git a/src/router/index.ts b/src/router/index.ts index 81340eb9..5ef4e8b9 100755 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,8 +1,8 @@ import pinia from '@/store' import useSettingsStore from '@/store/modules/settings' import { loadingFadeOut } from 'virtual:app-loading' - import { createRouter, createWebHashHistory } from 'vue-router' +import setupExtensions from './extensions' import setupGuards from './guards' // 路由相关数据 import { constantRoutes, constantRoutesByFilesystem } from './routes' @@ -13,6 +13,7 @@ const router = createRouter({ }) setupGuards(router) +setupExtensions(router) router.isReady().then(() => { loadingFadeOut() diff --git a/src/utils/composables/useTabbar.ts b/src/utils/composables/useTabbar.ts index 3104c374..f21cc00d 100755 --- a/src/utils/composables/useTabbar.ts +++ b/src/utils/composables/useTabbar.ts @@ -12,27 +12,6 @@ export default function useTabbar() { return route.fullPath } - function open(to: RouteLocationRaw) { - const index = tabbarStore.list.findIndex(item => item.tabId === getId()) - tabbarStore.$patch({ - leaveIndex: index, - }) - router.push(to) - } - - function go(delta: number) { - const tabId = getId() - router.go(delta) - tabbarStore.remove(tabId) - } - - function replace(to: RouteLocationRaw) { - const tabId = getId() - router.replace(to).then(() => { - tabbarStore.remove(tabId) - }) - } - function close(to: RouteLocationRaw) { const tabId = getId() router.push(to).then(() => { @@ -170,9 +149,6 @@ export default function useTabbar() { return { getId, - open, - go, - replace, close, closeById, closeOtherSide,