permission.js 823 B

1234567891011121314151617181920212223242526272829303132333435
  1. import router from './router';
  2. import { getSessionID, getConfig } from '@/utils/auth';
  3. import NProgress from 'nprogress';
  4. import 'nprogress/nprogress.css';
  5. NProgress.configure({ showSpinner: false });
  6. const whiteList = ['/login']; // 重定向白名单
  7. // 全局前置守卫
  8. router.beforeEach(async (to, from, next) => {
  9. NProgress.start();
  10. const { isHas } = getConfig();
  11. if (getSessionID() && isHas) {
  12. if (to.path === '/login') {
  13. next({ path: '/' });
  14. NProgress.done();
  15. } else {
  16. next();
  17. }
  18. } else if (whiteList.includes(to.path)) {
  19. // 在登录白名单中,直接进入
  20. next();
  21. } else {
  22. // 其他无权访问的页面将重定向到登录页面
  23. next(`/login`);
  24. NProgress.done();
  25. }
  26. });
  27. // 全局后置钩子
  28. router.afterEach(() => {
  29. NProgress.done();
  30. });