Browse Source

feat: 路由拦截

liming 3 weeks ago
parent
commit
9b84c80da4
3 changed files with 21 additions and 20 deletions
  1. 19 18
      src/router/router.guards.js
  2. 1 1
      src/router/system.js
  3. 1 1
      vite.config.js

+ 19 - 18
src/router/router.guards.js

@@ -1,21 +1,22 @@
+import { useSystemStore } from "@/stores/modules/systemStore";
+import { useWalletStore } from "@/stores/modules/walletStore";
 
-// import { useSystemStore } from '@/stores/modules/systemStore'
-// const systemStore = useSystemStore()
+export function createRouterGuards(router) {
+  const systemStore = useSystemStore();
+  const walletStore = useWalletStore();
+  // 路由拦截
+  router.beforeEach(async (to, from, next) => {
+    // 没有钱包,则跳转到login 页
+    if (!walletStore.account || !systemStore.token) {
+      next("/login");
+    }
+    next();
+  });
 
+  router.afterEach((to, _, next) => {});
 
-export function createRouterGuards(router, systemStore) {
-
-   router.beforeEach(async (to, from, next) => {
-
-     next()
-   })
-
-   router.afterEach((to, _, next) => {
-     
-   })
-
-   // 错误
-  router.onError(error => {
-    console.log(error, '路由错误')
-  })
-}
+  // 错误
+  router.onError((error) => {
+    console.log(error, "路由错误");
+  });
+}

+ 1 - 1
src/router/system.js

@@ -4,7 +4,7 @@ export const systemRoutes = [
   {
     path: "/",
     name: "main",
-    redirect: "dapp",
+    redirect: "wallet",
     component: LAYOUT,
     children: [
       {

+ 1 - 1
vite.config.js

@@ -12,7 +12,7 @@ import { resolve } from "path";
 // https://vite.dev/config/
 export default defineConfig(({ mode }) => {
   const env = loadEnv(mode, process.cwd());
-  console.log(env.VITE_PRO_IM_PATH)
+ 
   return {
     base: "./",
     plugins: [