permission.js 941 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import VueRouter from 'vue-router'
  2. import router from './router'
  3. //加载进度
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import { auth } from '@/utils/auth'
  7. const originalPush = VueRouter.prototype.push
  8. VueRouter.prototype.push = function (location) {
  9. return (originalPush.call(this, location)).catch((err) => err)
  10. }
  11. NProgress.configure({
  12. showSpinner: false
  13. })
  14. router.beforeEach(async (to, from, next) => {
  15. NProgress.start()
  16. //登录拦截
  17. const info = auth.initUserInfo();
  18. if (info) {
  19. // auth.initUserInfo()
  20. if (to.path === '/login') {
  21. next({path: '/'})
  22. }
  23. else {
  24. next()
  25. }
  26. }
  27. else {
  28. if (to.path === '/login') {
  29. next()
  30. }
  31. else {
  32. next({
  33. path: `/login` // ?redirect=${to.path}
  34. })
  35. }
  36. }
  37. })
  38. router.afterEach(() => {
  39. NProgress.done()
  40. })