permission.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, removeToken, getConfig } from '@/utils/auth' // get token from cookie
  7. import getPageTitle from '@/utils/get-page-title'
  8. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  9. const whiteList = ['/login', '/BookBrowsing'] // no redirect whitelist
  10. router.beforeEach(async(to, from, next) => {
  11. // start progress bar
  12. NProgress.start()
  13. // set page title
  14. document.title = getPageTitle(to.meta.title)
  15. // next();
  16. NProgress.done()
  17. const hasToken = getToken()
  18. if (hasToken || to.path === '/BookBrowsing' || to.path === '/courseview' || to.path === '/Integration/Courseware') {
  19. const config = getConfig()
  20. if (config || to.path === '/BookBrowsing' || to.path === '/courseview' || to.path === '/Integration/Courseware') {
  21. if (to.path === '/login') {
  22. // if is logged in, redirect to the home page
  23. next({ path: '/EnterSys' })
  24. NProgress.done()
  25. } else {
  26. try {
  27. next()
  28. } catch (error) {
  29. Message.error(error || 'Has Error')
  30. // next(`/login?redirect=${to.path}`)
  31. if (process.env.NODE_ENV == 'development') {
  32. next(`/login?redirect=${to.path}`)
  33. } else {
  34. window.location.href = '/'
  35. }
  36. NProgress.done()
  37. }
  38. }
  39. } else {
  40. removeToken()
  41. // next(`/login?redirect=${to.path}`)
  42. if (process.env.NODE_ENV == 'development') {
  43. next(`/login?redirect=${to.path}`)
  44. } else {
  45. window.location.href = '/'
  46. }
  47. NProgress.done()
  48. }
  49. } else {
  50. /* has no token*/
  51. if (whiteList.indexOf(to.path) !== -1) {
  52. // in the free login whitelist, go directly
  53. next()
  54. } else {
  55. // other pages that do not have permission to access are redirected to the login page.
  56. // next(`/login?redirect=${to.path}`)
  57. if (process.env.NODE_ENV == 'development') {
  58. next(`/login?redirect=${to.path}`)
  59. } else {
  60. window.location.href = '/'
  61. }
  62. NProgress.done()
  63. }
  64. }
  65. })
  66. router.afterEach(() => {
  67. // finish progress bar
  68. NProgress.done()
  69. })