permission.ts 543 B

12345678910111213141516171819202122232425262728
  1. import router from '@/router'
  2. import { getToken } from '@/utils/cache/cookies'
  3. import NProgress from 'nprogress'
  4. import 'nprogress/nprogress.css'
  5. NProgress.configure({ showSpinner: false })
  6. router.beforeEach(async (to, _form, next) => {
  7. NProgress.start()
  8. const token = getToken()
  9. if (!token) {
  10. if (to.path !== '/login') {
  11. next('/login')
  12. } else {
  13. next()
  14. }
  15. } else {
  16. if (to.path === '/login') {
  17. next({ path: '/' })
  18. } else {
  19. next()
  20. }
  21. }
  22. })
  23. router.afterEach(() => {
  24. NProgress.done()
  25. })