router.guards.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { useSystemStore } from "@/stores/modules/systemStore";
  2. import { useWalletStore } from "@/stores/modules/walletStore";
  3. import { whitelistRoutes } from "@/router/whitelist";
  4. import { systemRoutes } from "@/router/system";
  5. const paths = whitelistRoutes.map(item => {
  6. return item.path
  7. });
  8. const mainPath = systemRoutes[0].children.filter(item => {
  9. return item.meta?.tabbar || false
  10. }).map(cItem => {
  11. return cItem.path
  12. });
  13. export function createRouterGuards(router) {
  14. const systemStore = useSystemStore();
  15. const walletStore = useWalletStore();
  16. // 路由拦截
  17. router.beforeEach(async (to, from, next) => {
  18. if (import.meta.env.VITE_DAPP_BUILD == "1") {
  19. if (mainPath.includes(from.path) && paths.includes(to.path) && to.path != '/login') {
  20. // 停止跳转
  21. next(false)
  22. return
  23. }
  24. if (paths.includes(to.path)) {
  25. return next();
  26. }
  27. // 没有钱包,则跳转到login 页
  28. if (!walletStore.account || !systemStore.token) {
  29. return next("/login");
  30. }
  31. }
  32. if (import.meta.env.VITE_DAPP_BUILD == "2") {
  33. if (to.path == '/login') {
  34. next()
  35. }
  36. if (!systemStore.token) {
  37. return next("/login");
  38. }
  39. }
  40. next();
  41. });
  42. router.afterEach((to, _, next) => { });
  43. // 错误
  44. router.onError((error) => {
  45. console.log(error, "路由错误");
  46. });
  47. }