permission.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 } = 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. }
  98. item.hidden = hidden
  99. if (route.children && route.children.length) {
  100. if (type == 2) {
  101. try {
  102. item.component = _import(`${fullUrl}/index`)
  103. } catch (e) {
  104. console.log(e)
  105. }
  106. const roles = []
  107. const roleItems = []
  108. for (var role of route.children.filter(item => item.type == 3)) {
  109. roleItems.push({
  110. code: role.code,
  111. moduleName: role.moduleName
  112. })
  113. roles.push(role.code)
  114. }
  115. item.meta.roles = roles
  116. item.meta.roleItems = roleItems
  117. if (!pages.find(ite => ite.path === item.path)) {
  118. item.path = `${item.path}${bool?'':''}`
  119. pages.push(item)
  120. }
  121. } else if (type == 1) {
  122. item.component = RouterView
  123. item.children = route.children.filter(item => item.type !== 3).map(child => buildRoute(child, item.path, bool))
  124. }
  125. } else {
  126. if (type == 4) {
  127. item.component = IframeView
  128. if (!pages.find(ite => ite.path === item.path)) {
  129. pages.push(item)
  130. }
  131. } else if (type == 2) {
  132. try {
  133. item.component = _import(`${fullUrl}/index`)
  134. if (!pages.find(ite => ite.path === item.path)) {
  135. item.path = `${item.path}${bool?'':''}`
  136. pages.push(item)
  137. }
  138. } catch (e) {
  139. console.log(e)
  140. }
  141. }
  142. }
  143. return item
  144. }