25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

79 satır
3.0 KiB

  1. import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import notification from 'ant-design-vue/es/notification'
  7. import { ACCESS_TOKEN,INDEX_MAIN_PAGE_PATH } from '@/store/mutation-types'
  8. import { generateIndexRouter } from "@/utils/util"
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. const whiteList = ['/user/login', '/user/register', '/user/register-result','/user/alteration'] // no redirect whitelist
  11. router.beforeEach((to, from, next) => {
  12. NProgress.start() // start progress bar
  13. if (Vue.ls.get(ACCESS_TOKEN)) {
  14. /* has token */
  15. if (to.path === '/user/login') {
  16. next({ path: INDEX_MAIN_PAGE_PATH })
  17. NProgress.done()
  18. } else {
  19. if (store.getters.permissionList.length === 0) {
  20. store.dispatch('GetPermissionList').then(res => {
  21. const menuData = res.result.menu;
  22. console.log(res.message)
  23. if (menuData === null || menuData === "" || menuData === undefined) {
  24. return;
  25. }
  26. let constRoutes = [];
  27. constRoutes = generateIndexRouter(menuData);
  28. // 添加主界面路由
  29. store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
  30. // 根据roles权限生成可访问的路由表
  31. // 动态添加可访问路由表
  32. router.addRoutes(store.getters.addRouters)
  33. router.options.routes = store.state.permission.routers
  34. const redirect = decodeURIComponent(from.query.redirect || to.path)
  35. console.log(redirect, 'redirect', to, 'to', from, 'from')
  36. if (to.path === redirect) {
  37. console.log('我进来了', to.path)
  38. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  39. next({ ...to, replace: true })
  40. } else {
  41. console.log('我重定向了', redirect)
  42. // 跳转到目的路由
  43. next({ path: redirect })
  44. }
  45. console.log(router.options)
  46. })
  47. })
  48. .catch(() => {
  49. /* notification.error({
  50. message: '系统提示',
  51. description: '请求用户信息失败,请重试!'
  52. })*/
  53. store.dispatch('Logout').then(() => {
  54. next({ path: '/user/login', query: { redirect: to.fullPath } })
  55. })
  56. })
  57. } else {
  58. next()
  59. }
  60. }
  61. } else {
  62. if (whiteList.indexOf(to.path) !== -1) {
  63. // 在免登录白名单,直接进入
  64. next()
  65. } else {
  66. next({ path: '/user/login', query: { redirect: to.fullPath } })
  67. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  68. }
  69. }
  70. })
  71. router.afterEach(() => {
  72. NProgress.done() // finish progress bar
  73. })