permission.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from '@zjlib/element-ui2'
  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. component: Layout,
  17. children: []
  18. }
  19. import { pages } from './settings'
  20. var include = []
  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. router.addRoutes([lay])
  56. global.antRouter = []
  57. for (var route of store.getters.menus) {
  58. global.antRouter.push(buildRoute(route, '', false))
  59. }
  60. next({
  61. ...to,
  62. replace: true
  63. })
  64. } catch (error) {
  65. await store.dispatch('user/resetToken')
  66. Message.error(error || 'Has Error')
  67. next('/login')
  68. NProgress.done()
  69. }
  70. }
  71. }
  72. } else {
  73. if (whiteList.indexOf(to.path) !== -1) {
  74. next()
  75. } else {
  76. next('/login')
  77. NProgress.done()
  78. }
  79. }
  80. })
  81. router.afterEach(() => {
  82. NProgress.done()
  83. })
  84. function buildRoute(route, parentUrl = '', bool = true) {
  85. const { url, moduleName, icon, moduleId, code, type, hidden, fullUrl, status, isCache } = route
  86. var item = {}
  87. item.path = ~[3, 4].indexOf(type) ? fullUrl : parentUrl + url
  88. item.name = ~[3].indexOf(type) ? fullUrl : code
  89. item.type = type
  90. item.status = status
  91. item.meta = {
  92. url,
  93. title: moduleName,
  94. icon: icon,
  95. moduleId,
  96. status: status,
  97. isCache: isCache
  98. }
  99. item.hidden = hidden
  100. if (route.children && route.children.length) {
  101. if (type == 2) {
  102. try {
  103. item.component = _import(`${fullUrl}/index`)
  104. } catch (e) {
  105. console.log(e)
  106. }
  107. const roles = []
  108. const roleItems = []
  109. for (var role of route.children.filter(item => item.type == 3)) {
  110. roleItems.push({
  111. code: role.code,
  112. moduleName: role.moduleName
  113. })
  114. roles.push(role.code)
  115. }
  116. item.meta.roles = roles
  117. item.meta.roleItems = roleItems
  118. if (!pages.find(ite => ite.path === item.path)) {
  119. item.path = `${item.path}${bool ? '' : ''}`
  120. pages.push(item)
  121. }
  122. } else if (type == 1) {
  123. item.component = RouterView
  124. item.children = route.children.filter(item => item.type !== 3).map(child => buildRoute(child, item.path, bool))
  125. }
  126. } else {
  127. if (type == 4) {
  128. item.component = IframeView
  129. if (!pages.find(ite => ite.path === item.path)) {
  130. pages.push(item)
  131. }
  132. } else if (type == 2) {
  133. try {
  134. item.component = _import(`${fullUrl}/index`)
  135. if (!pages.find(ite => ite.path === item.path)) {
  136. item.path = `${item.path}${bool ? '' : ''}`
  137. pages.push(item)
  138. }
  139. } catch (e) {
  140. console.log(e)
  141. }
  142. }
  143. }
  144. return item
  145. }