permission.js 860 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import router from './router';
  2. import { createDiscreteApi} from 'naive-ui';
  3. import { useUserStore } from '@/stores/modules/userStore';
  4. const whiteList = ['/login'];
  5. const TITLE_SUFFIX = ' - LibraAI人工智能运营体';
  6. const { loadingBar } = createDiscreteApi(['loadingBar'], {
  7. loadingBarProviderProps: {
  8. loadingBarStyle: {
  9. backgroundColor: "#2454FF"
  10. }
  11. }
  12. })
  13. // 简易的基础权限
  14. router.beforeEach(async (to, from, next) => {
  15. loadingBar.start();
  16. const isRouterAuth = whiteList.includes(to.path);
  17. if (isRouterAuth) {
  18. next()
  19. } else {
  20. const userStore = useUserStore();
  21. const { token } = userStore.userInfo;
  22. if ( !token ) {
  23. return next('/login');
  24. } else {
  25. next()
  26. }
  27. }
  28. })
  29. router.afterEach((to) => {
  30. document.title = to?.meta?.title + TITLE_SUFFIX;
  31. loadingBar.finish();
  32. })