permission.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 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: '/dashboard/workplace' })
  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. const redirect = decodeURIComponent(from.query.redirect || to.path)
  34. if (to.path === redirect) {
  35. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  36. next({ ...to, replace: true })
  37. } else {
  38. // 跳转到目的路由
  39. next({ path: redirect })
  40. }
  41. })
  42. })
  43. .catch(() => {
  44. /* notification.error({
  45. message: '系统提示',
  46. description: '请求用户信息失败,请重试!'
  47. })*/
  48. store.dispatch('Logout').then(() => {
  49. next({ path: '/user/login', query: { redirect: to.fullPath } })
  50. })
  51. })
  52. } else {
  53. next()
  54. }
  55. }
  56. } else {
  57. if (whiteList.indexOf(to.path) !== -1) {
  58. // 在免登录白名单,直接进入
  59. next()
  60. } else {
  61. next({ path: '/user/login', query: { redirect: to.fullPath } })
  62. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  63. }
  64. }
  65. })
  66. router.afterEach(() => {
  67. NProgress.done() // finish progress bar
  68. })