permission.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 menus from '@/router/menus'
  9. import utils from '@/utils/utils'
  10. NProgress.configure({ showSpinner: false }); // NProgress Configuration
  11. const whiteList = ['/login', '/404', '/401','/prod/validate']; // no redirect whitelist
  12. export function getList(tree) {
  13. const list = [];
  14. function treeMap(data) {
  15. data.forEach(item => {
  16. list.push(item);
  17. if (item.children && item.children.length > 0) {
  18. treeMap(item.children)
  19. }
  20. })
  21. }
  22. treeMap(tree);
  23. return list
  24. }
  25. export function getBreadcrumb(treeListData, menuId) {
  26. // 计算父级
  27. let matched = [];
  28. function getMatched(list, menuId) {
  29. list.forEach(item => {
  30. // eslint-disable-next-line eqeqeq
  31. if (menuId == item.id) {
  32. matched.unshift({
  33. // path: item.pcUrl?`${item.pcUrl}?menuLevel1=${this.$route.query.menuLevel1}&menuId=${item.id}`:'',
  34. meta: {
  35. title: item.name,
  36. icon: item.pcIcon,
  37. noCache: item.cachedViews,
  38. ...item
  39. }
  40. });
  41. if (item.parentId) {
  42. // console.info(item, item.parentId)
  43. getMatched(list, item.parentId)
  44. }
  45. }
  46. })
  47. }
  48. // console.log(treeListData, 'getter 路由数据')
  49. getMatched(treeListData, menuId);
  50. // console.log(matched, menuId, '菜单路径数组')
  51. return matched
  52. }
  53. router.beforeEach(async (to, from, next) => {
  54. // start progress bar
  55. NProgress.start();
  56. // set page title
  57. // document.title = getPageTitle(to.meta.title);
  58. // determine whether the user has logged in
  59. const hasToken = getToken();
  60. if (hasToken) {
  61. // alert(1)
  62. if (to.path === '/login') {
  63. // if is logged in, redirect to the home page
  64. next({ path:'/home' }); // 登录页面,有token 直接进入首页
  65. // next()
  66. NProgress.done()
  67. } else { // 判断vuex存储的权限路由是否存在,否则重新获取用户信息,计算路由。(user都存于vuex,刷新自然会重新获取用户信息。页面获取用户信息可通过getter获取)
  68. /**/
  69. utils.loading.show()
  70. if (store.getters.userInfo && store.getters.permission_routes && store.getters.permission_routes.length > 0) {
  71. // 计算下菜单路径
  72. let menuId = to.meta && to.meta.id;
  73. if (menuId && store.getters.permission_menus) {
  74. await store.dispatch('permission/menusLevelList', getBreadcrumb(getList(store.getters.permission_menus), menuId))
  75. }
  76. next()
  77. } else {
  78. const { name, postList } = await store.dispatch('user/userInfo');
  79. console.log('账户/岗位', name, postList);
  80. const accessedRoutes = await store.dispatch('user/getMenus');
  81. const accessRoutes = await store.dispatch('permission/generateRoutes', accessedRoutes);
  82. accessRoutes.push({
  83. path: '*',
  84. redirect: '/404',
  85. hidden: true
  86. }); // 不能在自己写的路由里边添加404 要在addRoutes中添加404页面不然不会跳转404
  87. // console.log('动态菜单', accessRoutes)
  88. // dynamically add accessible routes
  89. router.addRoutes(accessRoutes); // 每次刷新添加得动态路由是会丢失
  90. next({
  91. ...to,
  92. replace: true
  93. }) // hack方法 确保addRoutes已完成
  94. }
  95. utils.loading.hide()
  96. }
  97. } else {
  98. /* has no token*/
  99. if (whiteList.indexOf(to.path) !== -1) {
  100. // in the free login whitelist, go directly
  101. next()
  102. } else{
  103. // other pages that do not have permission to access are redirected to the login page.
  104. if(to.query.id){
  105. next(`/login?redirect=${to.path}&id=${to.query.id}`);
  106. }else{
  107. next(`/login?redirect=${to.path}`);
  108. }
  109. NProgress.done()
  110. }
  111. }
  112. setTimeout(() => {
  113. NProgress.done();
  114. next()
  115. }, 200)
  116. });
  117. router.afterEach(() => {
  118. // finish progress bar
  119. NProgress.done()
  120. });