From dc0d58dcf6150cc4a2be0e08eaf56fc5a7f8e732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E5=88=BA=E8=9E=88?= Date: Wed, 4 Dec 2024 19:19:41 +0800 Subject: [PATCH] fix: router --- docs/.vitepress/theme/index.ts | 83 +++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/docs/.vitepress/theme/index.ts b/docs/.vitepress/theme/index.ts index de0a41e..6c07f66 100644 --- a/docs/.vitepress/theme/index.ts +++ b/docs/.vitepress/theme/index.ts @@ -1,17 +1,17 @@ import 'uno.css'; -import type { Theme } from 'vitepress'; +import { useRouter, type Theme } from 'vitepress'; import DefaultTheme from 'vitepress/theme'; +import { + defineComponent, + Fragment, + h, + onMounted, + shallowRef, + Teleport, +} from 'vue'; import components from '../components'; import BodyScrollbar from '../components/BodyScrollbar.vue'; import './custom.css'; -import { - Fragment, - h, - Teleport, - defineComponent, - shallowRef, - onMounted, -} from 'vue'; const ScrollbarWrapper = defineComponent(() => { const show = shallowRef(false); @@ -30,40 +30,49 @@ const ScrollbarWrapper = defineComponent(() => { }; }); +const Redirect = defineComponent(() => { + const router = useRouter(); + onMounted(() => { + // 兼容旧链接/短链重定向 + const u = location.href.substring(location.origin.length); + if (location.pathname.startsWith('/selector/')) { + if (location.pathname.at(-1) === '/') { + router.go('/guide/selector'); + } else { + router.go(location.pathname.replace('/selector/', '/guide/')); + } + } else if (location.pathname === '/subscription/') { + router.go('/guide/subscription'); + } else if (location.pathname === '/') { + const r = new URLSearchParams(location.search).get('r'); + if (r === '1') { + router.go('/guide/snapshot#how-to-upload'); + } else if (r === '2') { + router.go('/guide/faq#restriction'); + } else if (r === '3') { + router.go('/guide/faq#adb_failed'); + } else if (r === '4') { + location.href = 'https://shizuku.rikka.app'; + } + } else if (u === '/guide/faq#fail_setting_secure_settings') { + location.hash = 'adb_failed'; + } + }); + return () => {}; +}); + export default { extends: DefaultTheme, Layout() { - return h(Fragment, null, [h(DefaultTheme.Layout), h(ScrollbarWrapper)]); + return h(Fragment, null, [ + h(DefaultTheme.Layout), + h(ScrollbarWrapper), + h(Redirect), + ]); }, - enhanceApp({ app, router }) { + enhanceApp({ app }) { Object.entries(components).forEach(([name, component]) => { app.component(name, component); }); - // 兼容旧链接/短链重定向 - if (!import.meta.env.SSR) { - const u = location.href.substring(location.origin.length); - if (location.pathname.startsWith('/selector/')) { - if (location.pathname.at(-1) === '/') { - router.go('/guide/selector'); - } else { - router.go(location.pathname.replace('/selector/', '/guide/')); - } - } else if (location.pathname === '/subscription/') { - router.go('/guide/subscription'); - } else if (location.pathname === '/') { - const r = new URLSearchParams(location.search).get('r'); - if (r === '1') { - router.go('/guide/snapshot#how-to-upload'); - } else if (r === '2') { - router.go('/guide/faq#restriction'); - } else if (r === '3') { - router.go('/guide/faq#adb_failed'); - } else if (r === '4') { - location.href = 'https://shizuku.rikka.app'; - } - } else if (u === '/guide/faq#fail_setting_secure_settings') { - location.hash = 'adb_failed'; - } - } }, } satisfies Theme;