permission1.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import { getToken } from '@/utils/auth' // get token from cookie
  7. import getPageTitle from '@/utils/get-page-title'
  8. import Cookies from 'js-cookie';
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. const whiteList = ['/login'] // no redirect whitelist
  11. router.beforeEach(async(to, from, next) => {
  12. // start progress bar
  13. NProgress.start()
  14. // set page title
  15. document.title = getPageTitle(to.meta.title)
  16. //next();
  17. NProgress.done()
  18. const hasToken = getToken();
  19. const jId = Cookies.get('JSESSSIONID');
  20. if (hasToken) {
  21. if (!jId) {
  22. store.dispatch('user/setJsessionId').then(res => {
  23. if (to.path === '/login') {
  24. // if is logged in, redirect to the home page
  25. next({ path: '/' })
  26. NProgress.done()
  27. } else {
  28. try {
  29. next()
  30. } catch (error) {
  31. Message.error(error || 'Has Error')
  32. next(`/login?redirect=${to.path}`)
  33. NProgress.done()
  34. }
  35. }
  36. })
  37. } else {
  38. if (to.path === '/login') {
  39. // if is logged in, redirect to the home page
  40. next({ path: '/' })
  41. NProgress.done()
  42. } else {
  43. try {
  44. next()
  45. } catch (error) {
  46. Message.error(error || 'Has Error')
  47. next(`/login?redirect=${to.path}`)
  48. NProgress.done()
  49. }
  50. }
  51. }
  52. } else {
  53. /* has no token*/
  54. if (whiteList.indexOf(to.path) !== -1) {
  55. // in the free login whitelist, go directly
  56. next()
  57. } else {
  58. // other pages that do not have permission to access are redirected to the login page.
  59. next(`/login?redirect=${to.path}`)
  60. NProgress.done()
  61. }
  62. }
  63. })
  64. router.afterEach(() => {
  65. // finish progress bar
  66. NProgress.done()
  67. })