From 54e9ab27612cb567e6105457276ca3985c19b739 Mon Sep 17 00:00:00 2001 From: "Ray.Hao" <1490493387@qq.com> Date: Fri, 16 Oct 2026 20:33:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(router):=20=E7=A7=BB=E9=99=A4=E7=A9=BA?= =?UTF-8?q?=E8=BD=AC=E7=9A=84=20uni-mini-router?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除路由模块与全局守卫,无页面声明 requireAuth,守卫从未生效 - 菜单与登录跳转统一为 uni API,字典标题按约定编码解码 - 依赖、自动导入与类型声明一并清理 --- package.json | 1 - src/main.ts | 2 - src/pages/mine/index.vue | 12 ++--- src/router/index.ts | 88 ---------------------------------- src/types/uni-mini-router.d.ts | 21 -------- vite.config.ts | 4 -- 6 files changed, 6 insertions(+), 122 deletions(-) delete mode 100644 src/router/index.ts delete mode 100644 src/types/uni-mini-router.d.ts diff --git a/package.json b/package.json index eacad06..4ffb612 100644 --- a/package.json +++ b/package.json @@ -143,7 +143,6 @@ "stylelint-prettier": "^5.0.3", "typescript": "^5.7.0", "typescript-eslint": "^8.46.0", - "uni-mini-router": "^0.1.6", "unocss": "^0.62.4", "unocss-preset-weapp": "^0.62.2", "unplugin-auto-import": "^0.18.6", diff --git a/src/main.ts b/src/main.ts index 6a5c248..ee3289c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,7 +6,6 @@ import "@/styles/index.scss"; import { setupPinia, useUserStore } from "@/store"; import { setUnauthorizedHandler } from "@/utils/request"; -import router from "./router"; export function createApp() { const app = createSSRApp(App); @@ -14,7 +13,6 @@ export function createApp() { setupPinia(app); // 注册会话失效回调:401 时由 store 统一清理内存与持久化登录态 setUnauthorizedHandler(() => useUserStore().resetSession()); - app.use(router); return { app, diff --git a/src/pages/mine/index.vue b/src/pages/mine/index.vue index df84a43..a7894d0 100644 --- a/src/pages/mine/index.vue +++ b/src/pages/mine/index.vue @@ -67,7 +67,6 @@ import { computed, ref } from "vue"; import { onPageScroll, onShow } from "@dcloudio/uni-app"; import { storeToRefs } from "pinia"; import { useUserStore, useThemeStore } from "@/store"; -import { useRouter } from "uni-mini-router"; import { useNavbarMetrics } from "@/composables/useNavbarMetrics"; import { useScanLogin } from "@/composables/useScanLogin"; import { useLogout } from "@/composables/useLogout"; @@ -89,7 +88,6 @@ type SettingsMenuEntry = SettingsMenuItem & const userStore = useUserStore(); const themeStore = useThemeStore(); -const router = useRouter(); const navbar = useNavbarMetrics(); const { scanForLogin } = useScanLogin(); const { isLoggingOut, requestLogout } = useLogout(); @@ -201,7 +199,7 @@ function handleMenuSelect(id: string) { navigateToLogin(); return; } - router.push({ path: entry.target }); + uni.navigateTo({ url: entry.target }); } // 登录页带回跳地址,登录成功后原路返回 @@ -209,7 +207,9 @@ function navigateToLogin() { const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; const currentPagePath = `/${currentPage.route}`; - router.push({ path: "/pages/login/index", query: { redirect: currentPagePath } }); + uni.navigateTo({ + url: `/pages/login/index?redirect=${encodeURIComponent(currentPagePath)}`, + }); } // 资料页要求登录,未登录先跳登录页 @@ -218,7 +218,7 @@ function navigateToProfile() { navigateToLogin(); return; } - router.push({ path: "/subPages/mine/profile/index" }); + uni.navigateTo({ url: "/subPages/mine/profile/index" }); } // 账号安全页要求登录,未登录先跳登录页 @@ -227,7 +227,7 @@ function navigateToAccountSecurity() { navigateToLogin(); return; } - router.push({ path: "/subPages/mine/account/index" }); + uni.navigateTo({ url: "/subPages/mine/account/index" }); } // 公众号介绍页对游客开放,不需要登录 diff --git a/src/router/index.ts b/src/router/index.ts deleted file mode 100644 index b0c7ad9..0000000 --- a/src/router/index.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { pages, subPackages } from "virtual:uni-pages"; -import { getAccessToken } from "@/utils/auth"; -import { createRouter } from "uni-mini-router"; -import { useUserStore } from "@/store"; - -/** virtual:uni-pages 生成的页面路由描述 */ -interface PageMetaRoute { - path: string; - meta?: Record; - [key: string]: unknown; -} - -/** 分包路由描述 */ -interface SubPackageRoute { - root: string; - pages: PageMetaRoute[]; -} - -// 生成路由配置 -function generateRoutes() { - const routes = pages.map((page: PageMetaRoute) => { - const newPath = `/${page.path}`; - const meta = page.meta ?? undefined; - return { ...page, path: newPath, meta }; - }); - - // 处理分包路由 - if (subPackages && subPackages.length > 0) { - subPackages.forEach((subPackage: SubPackageRoute) => { - const subRoutes = subPackage.pages.map((page) => { - const newPath = `/${subPackage.root}/${page.path}`; - const meta = page.meta ?? undefined; - return { ...page, path: newPath, meta }; - }); - routes.push(...subRoutes); - }); - } - return routes; -} - -// 创建路由实例 -const router = createRouter({ - routes: generateRoutes(), -}); - -// 全局前置守卫 -router.beforeEach(async (to, from, next) => { - if (to.meta && to.meta.requireAuth) { - const token = getAccessToken(); - - // 无 token:弹窗提示登录 - if (!token) { - const redirectPath = (to.path || "/pages/index/index") as string; - next(false); - uni.showModal({ - title: "提示", - content: "该功能需要登录后使用", - confirmText: "去登录", - cancelText: "返回", - success: (res) => { - if (res.confirm) { - router.push({ - path: "/pages/login/index", - query: { redirect: encodeURIComponent(redirectPath) }, - }); - } - }, - }); - return; - } - - // 有 token 但无用户信息:尝试获取 - const userStore = useUserStore(); - if (!userStore.userInfo) { - try { - await userStore.loadUserInfo(); - } catch { - next(false); - userStore.logout(); - return; - } - } - } - - next(); -}); - -export default router; diff --git a/src/types/uni-mini-router.d.ts b/src/types/uni-mini-router.d.ts deleted file mode 100644 index b7eca43..0000000 --- a/src/types/uni-mini-router.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -// 扩展 uni-mini-router 路由类型,支持从 pages.json 注入的 meta 字段 -// 该声明文件会被 ts 自动合并到库类型中 - -import "uni-mini-router"; - -declare module "uni-mini-router" { - /** 自定义路由元信息类型,可按需扩展 */ - interface RouteMeta { - /** 是否需要登录 */ - requireAuth?: boolean; - /** 其他自定义字段 */ - [key: string]: any; - } - - interface Route { - /** 来自 pages.json 的 meta 字段(动态生成时透传) */ - meta?: RouteMeta; - } -} - -export {}; diff --git a/vite.config.ts b/vite.config.ts index e1bf789..244b5b0 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -99,10 +99,6 @@ export default defineConfig(async ({ mode }: ConfigEnv): Promise => "vue", "uni-app", "pinia", - { - from: "uni-mini-router", - imports: ["createRouter", "useRouter", "useRoute"], - }, { from: "@wot-ui/ui", imports: ["useToast", "useDialog", "useNotify", "CommonUtil"],