permission.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 } from '@/store/mutation-types'
  8. import { generateIndexRouter } from '@/utils/util'
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. // 需授权路径
  11. const whiteList = ['/user/login', '/user/todo', '/user/register', '/user/register-result', '/user/alteration'] // no redirect whitelist
  12. router.beforeEach((to, from, next) => {
  13. NProgress.start() // start progress bar
  14. if (Vue.ls.get(ACCESS_TOKEN)) {
  15. /* has token */
  16. if (to.path === '/user/login') {
  17. // next({ path: '/dashboard/workplace' }) // 原始
  18. next({ path: '/user/todo' })
  19. NProgress.done()
  20. } else {
  21. if (store.getters.permissionList.length === 0) {
  22. store
  23. .dispatch('GetPermissionList')
  24. .then(res => {
  25. const menuData = res.result.menu
  26. console.log('res.maessage:', res.message) // 控制台打印 查询成功
  27. // console.log('应该打印出该页面书需要的数据')
  28. if (menuData === null || menuData === '' || menuData === undefined) {
  29. return
  30. }
  31. let constRoutes = []
  32. constRoutes = generateIndexRouter(menuData)
  33. // 添加主界面路由
  34. store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
  35. // 根据roles权限生成可访问的路由表
  36. // 动态添加可访问路由表
  37. router.addRoutes(store.getters.addRouters)
  38. const redirect = decodeURIComponent(from.query.redirect || to.path)
  39. if (to.path === redirect) {
  40. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  41. next({ ...to, replace: true })
  42. } else {
  43. // 跳转到目的路由
  44. next({ path: redirect })
  45. }
  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. // 在免登录白名单,直接进入
  63. if (whiteList.indexOf(to.path) !== -1) {
  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. })