permission.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import { getToken } from '@/utils/auth' // get token from cookie
  5. import NProgress from 'nprogress' // progress bar
  6. import 'nprogress/nprogress.css' // progress bar style
  7. import getPageTitle from '@/utils/get-page-title'
  8. import Layout from '@/layout'
  9. import RouterView from '@/views/routerView.vue'
  10. import IframeView from '@/views/iframeView.vue'
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. const _import = require('./router/_import_' + process.env.NODE_ENV) // 获取组件的方法
  13. const whiteList = ['/login'] // no redirect whitelist
  14. const lay = {
  15. path: '/',
  16. redirect: '/dashboard',
  17. component: Layout,
  18. children: []
  19. }
  20. import { pages } from './settings'
  21. // 递归找最后一级页面
  22. function getc(obj) {
  23. if (!obj.children || !obj.children.length) {
  24. return obj
  25. } else {
  26. return getc(obj.children[0])
  27. }
  28. }
  29. router.beforeEach(async (to, from, next) => {
  30. NProgress.start()
  31. document.title = getPageTitle(to.meta.title)
  32. const hasToken = getToken()
  33. if (hasToken) {
  34. if (to.path === '/login') {
  35. next({ path: '/' })
  36. NProgress.done()
  37. } else {
  38. const hasGetUserInfo = store.getters.name
  39. if (hasGetUserInfo) {
  40. next()
  41. } else {
  42. try {
  43. await store.dispatch('user/getInfo')
  44. await store.dispatch('user/getRouter')
  45. if (store.getters.menus.length < 1) {
  46. global.antRouter = []
  47. next()
  48. }
  49. // 设置路由
  50. lay.children = []
  51. for (var route of store.getters.menus) {
  52. lay.children.push(buildRoute(route))
  53. }
  54. lay.redirect = getc(lay).path
  55. console.log(lay.redirect, 9999, lay)
  56. router.addRoutes([lay])
  57. global.antRouter = lay.children
  58. next({
  59. ...to,
  60. replace: true
  61. })
  62. } catch (error) {
  63. await store.dispatch('user/resetToken')
  64. Message.error(error || 'Has Error')
  65. next('/login')
  66. NProgress.done()
  67. }
  68. }
  69. }
  70. } else {
  71. if (whiteList.indexOf(to.path) !== -1) {
  72. next()
  73. } else {
  74. next('/login')
  75. NProgress.done()
  76. }
  77. }
  78. })
  79. router.afterEach(() => {
  80. NProgress.done()
  81. })
  82. function buildRoute(route, parentUrl = '') {
  83. const { url, moduleName, icon, moduleId, code, type, hidden, fullUrl } = route
  84. var item = {}
  85. item.path = ~[3, 4].indexOf(type) ? fullUrl : parentUrl + url
  86. item.name = ~[3, 4].indexOf(type) ? fullUrl : code
  87. item.type = type
  88. item.meta = {
  89. url,
  90. title: moduleName,
  91. icon: icon,
  92. moduleId
  93. }
  94. item.hidden = hidden
  95. if (route.children && route.children.length) {
  96. if (type == 2) {
  97. try {
  98. item.component = _import(`${fullUrl}`)
  99. } catch (e) {
  100. console.log(e)
  101. }
  102. const roles = []
  103. for (var role of route.children) {
  104. roles.push(role.code)
  105. }
  106. item.meta.roles = roles
  107. if (!pages.find(ite => ite.path === item.path)) {
  108. pages.push(item)
  109. }
  110. } else if (type == 1) {
  111. item.component = RouterView
  112. item.children = route.children.filter(item => item.type !== 3).map(child => buildRoute(child, item.path))
  113. }
  114. } else {
  115. if (type == 4) {
  116. item.component = IframeView
  117. if (!pages.find(ite => ite.path === item.path)) {
  118. pages.push(item)
  119. }
  120. } else if (type == 2) {
  121. try {
  122. item.component = _import(`${fullUrl}`)
  123. if (!pages.find(ite => ite.path === item.path)) {
  124. pages.push(item)
  125. }
  126. } catch (e) {
  127. console.log(e)
  128. }
  129. }
  130. }
  131. return item
  132. }