wip: 临时提交

This commit is contained in:
Ray.Hao
2025-07-27 11:49:43 +08:00
parent 7fa0758b19
commit 043f5b0ec4
11 changed files with 173 additions and 116 deletions

View File

@@ -1,5 +1,7 @@
import { pages, subPackages } from "virtual:uni-pages";
import { isLoggedIn } from "@/utils/auth";
import { createRouter } from "uni-mini-router";
// 生成路由配置
function generateRoutes() {
const routes = pages.map((page: { path: string; [key: string]: any }) => {
@@ -17,7 +19,6 @@ function generateRoutes() {
routes.push(...subRoutes);
});
}
return routes;
}
@@ -28,39 +29,41 @@ const router = createRouter({
// 全局前置守卫
router.beforeEach((to, from, next) => {
console.log("路由跳转:", to.path);
// 检查页面是否需要登录
if (to.meta && to.meta.requireAuth) {
if (!isLoggedIn) {
// 显示登录提示
uni.showModal({
title: "提示",
content: "该功能需要登录后使用",
confirmText: "去登录",
cancelText: "返回",
success: (res) => {
if (res.confirm) {
// 记住原来要去的页面
uni.setStorageSync("redirect", to.fullPath);
next("/pages/login/index");
} else {
// 取消则返回首页
next("/pages/index/index");
}
},
});
return;
}
if (to.meta && to.meta.requireAuth && !isLoggedIn()) {
uni.showModal({
title: "提示",
content: "该功能需要登录后使用",
confirmText: "去登录",
cancelText: "返回",
success: (res) => {
if (res.confirm) {
// 记住原来要去的页面
uni.setStorageSync("redirect", to.fullPath);
// 使用 uni 原生导航而不是 router
uni.navigateTo({
url: "/pages/login/index",
});
} else {
// 取消则返回首页
uni.switchTab({
url: "/pages/index/index",
});
}
},
// 确保在取消弹窗时也能调用 next
fail: () => {
next(false);
},
});
} else {
// 继续导航
next();
}
// 继续导航
next();
});
router.afterEach((to) => {
console.log("路由跳转完成:", to.path);
// 可以在这里做一些统计或记录
});
export default router;