index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /// <reference types="@uni-helper/vite-plugin-uni-pages/client" />
  2. import { pages, subPackages } from "virtual:uni-pages";
  3. function generateRoutes() {
  4. const routes = pages.map((page) => {
  5. const newPath = `/${page.path}`;
  6. return { ...page, path: newPath };
  7. });
  8. if (subPackages && subPackages.length > 0) {
  9. subPackages.forEach((subPackage) => {
  10. const subRoutes = subPackage.pages.map((page: any) => {
  11. const newPath = `/${subPackage.root}/${page.path}`;
  12. return { ...page, path: newPath };
  13. });
  14. routes.push(...subRoutes);
  15. });
  16. }
  17. return routes;
  18. }
  19. const router = createRouter({
  20. routes: generateRoutes(),
  21. });
  22. const writePath = ["index", "login"];
  23. router.beforeEach((to, from, next) => {
  24. console.log("🚀 beforeEach 守卫触发:", { to, from });
  25. const { token } = useUserStore();
  26. if (!writePath.includes(String(to.name)) && !token) {
  27. uni.showToast({ title: "请先登录", icon: "none" });
  28. next(false);
  29. }
  30. // 继续导航
  31. next();
  32. });
  33. router.afterEach((to, from) => {
  34. console.log("🎯 afterEach 钩子触发:", { to, from });
  35. // // 演示:简单的页面切换记录
  36. // if (to.path) {
  37. // console.log(`📄 页面切换完成: ${to.path}`);
  38. // }
  39. // // 演示:针对 afterEach 演示页面的简单提示
  40. // if (to.name === "demo-aftereach") {
  41. // console.log("📊 进入 afterEach 演示页面");
  42. // setTimeout(() => {
  43. // showToast("afterEach 钩子已触发!");
  44. // }, 500);
  45. // }
  46. });
  47. export default router;