Browse Source

fix:添加登录校验

wangningfei 2 years ago
parent
commit
f2a79aae19
1 changed files with 48 additions and 48 deletions
  1. 48 48
      vue-admin-template-master/src/permission.js

+ 48 - 48
vue-admin-template-master/src/permission.js

@@ -15,61 +15,61 @@ router.beforeEach(async (to, from, next) => {
 
   // set page title
   document.title = getPageTitle(to.meta.title);
-  next();
+  // next();
   // determine whether the user has logged in
   const hasToken = getToken();
   console.log(hasToken);
 
-  // if (!!hasToken && typeof hasToken == "string") {
-  //   if (to.path === "/login") {
-  //     // if is logged in, redirect to the home page
-  //     next({ path: "/" });
-  //     NProgress.done();
-  //   } else {
-  //     const hasRoles = store.getters.roles && store.getters.roles.length > 0;
-  //     if (hasRoles) {
-  //       next();
-  //     } else {
-  //       const roles = await store.dispatch("user/setroles");
-  //       store.dispatch("GenerateRoutes", { roles }).then(() => {
-  //         // 生成可访问的路由表
-  //         router.options.routes = store.getters.addRouters;
-  //         router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
-  //         next({ ...to, replace: true }); // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
-  //       });
-  //     }
+  if (!!hasToken && typeof hasToken == "string") {
+    if (to.path === "/login") {
+      // if is logged in, redirect to the home page
+      next({ path: "/" });
+      NProgress.done();
+    } else {
+      const hasRoles = store.getters.roles && store.getters.roles.length > 0;
+      if (hasRoles) {
+        next();
+      } else {
+        const roles = await store.dispatch("user/setroles");
+        store.dispatch("GenerateRoutes", { roles }).then(() => {
+          // 生成可访问的路由表
+          router.options.routes = store.getters.addRouters;
+          router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
+          next({ ...to, replace: true }); // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
+        });
+      }
 
-  //     // next()
-  //     // const hasGetUserInfo = store.getters.name
-  //     // if (hasGetUserInfo) {
-  //     //   next()
-  //     // } else {
-  //     //   try {
-  //     //     // get user info
-  //     //     await store.dispatch('user/getInfo')
+      // next()
+      // const hasGetUserInfo = store.getters.name
+      // if (hasGetUserInfo) {
+      //   next()
+      // } else {
+      //   try {
+      //     // get user info
+      //     await store.dispatch('user/getInfo')
 
-  //     //     next()
-  //     //   } catch (error) {
-  //     //     // remove token and go to login page to re-login
-  //     //     await store.dispatch('user/resetToken')
-  //     //     Message.error(error || 'Has Error')
-  //     //     next(`/login?redirect=${to.path}`)
-  //     //     NProgress.done()
-  //     //   }
-  //     // }
-  //   }
-  // } else {
-  //   /* has no token*/
+      //     next()
+      //   } catch (error) {
+      //     // remove token and go to login page to re-login
+      //     await store.dispatch('user/resetToken')
+      //     Message.error(error || 'Has Error')
+      //     next(`/login?redirect=${to.path}`)
+      //     NProgress.done()
+      //   }
+      // }
+    }
+  } else {
+    /* has no token*/
 
-  //   if (whiteList.indexOf(to.path) !== -1) {
-  //     // in the free login whitelist, go directly
-  //     next();
-  //   } else {
-  //     // other pages that do not have permission to access are redirected to the login page.
-  //     next(`/login?redirect=${to.path}`);
-  //     NProgress.done();
-  //   }
-  // }
+    if (whiteList.indexOf(to.path) !== -1) {
+      // in the free login whitelist, go directly
+      next();
+    } else {
+      // other pages that do not have permission to access are redirected to the login page.
+      next(`/login?redirect=${to.path}`);
+      NProgress.done();
+    }
+  }
 });
 
 router.afterEach(() => {