permission.ts 934 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import router from "@/router";
  2. import NProgress from "@/utils/nprogress";
  3. import { useDictionaryStore } from "@/store/modules/dictionary";
  4. export function setupPermission() {
  5. // 白名单路由
  6. const whiteList = ["/login"];
  7. router.beforeEach(async (to, from, next) => {
  8. NProgress.start();
  9. const hasToken = localStorage.getItem("token");
  10. if (hasToken) {
  11. if (to.path === "/login") {
  12. // 如果已登录,跳转首页
  13. next({ path: "/" });
  14. NProgress.done();
  15. } else {
  16. const dictStore = useDictionaryStore();
  17. dictStore.checkDicts();
  18. next();
  19. NProgress.done();
  20. }
  21. } else {
  22. // 未登录可以访问白名单页面
  23. if (whiteList.indexOf(to.path) !== -1) {
  24. next();
  25. } else {
  26. next(`/login?redirect=${to.path}`);
  27. NProgress.done();
  28. }
  29. }
  30. });
  31. router.afterEach(() => {
  32. NProgress.done();
  33. });
  34. }