refactor(router): 移除空转的 uni-mini-router
- 删除路由模块与全局守卫,无页面声明 requireAuth,守卫从未生效 - 菜单与登录跳转统一为 uni API,字典标题按约定编码解码 - 依赖、自动导入与类型声明一并清理
This commit is contained in:
1 parent
2700b60a31
commit
54e9ab2761
6 files changed
+6
-122
No files matched your search
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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" });
|
||||
}
|
||||
|
||||
// 公众号介绍页对游客开放,不需要登录
|
||||
|
||||
@@ -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<string, unknown>;
|
||||
[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;
|
||||
Vendored
-21
@@ -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 {};
|
||||
@@ -99,10 +99,6 @@ export default defineConfig(async ({ mode }: ConfigEnv): Promise<UserConfig> =>
|
||||
"vue",
|
||||
"uni-app",
|
||||
"pinia",
|
||||
{
|
||||
from: "uni-mini-router",
|
||||
imports: ["createRouter", "useRouter", "useRoute"],
|
||||
},
|
||||
{
|
||||
from: "@wot-ui/ui",
|
||||
imports: ["useToast", "useDialog", "useNotify", "CommonUtil"],
|
||||
|
||||
Reference in new issue
Block a user