permission.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // 递归找最后一级页面
  21. function getc(obj) {
  22. if (!obj.children || !obj.children.length) {
  23. return obj
  24. } else {
  25. return getc(obj.children[0])
  26. }
  27. }
  28. router.beforeEach(async (to, from, next) => {
  29. NProgress.start()
  30. document.title = getPageTitle(to.meta.title)
  31. const hasToken = getToken()
  32. if (hasToken) {
  33. if (to.path === '/login') {
  34. next({ path: '/' })
  35. NProgress.done()
  36. } else {
  37. const hasGetUserInfo = store.getters.name
  38. if (hasGetUserInfo) {
  39. next()
  40. } else {
  41. try {
  42. await store.dispatch('user/getInfo')
  43. await store.dispatch('user/getRouter')
  44. if (store.getters.menus.length < 1) {
  45. global.antRouter = []
  46. next()
  47. }
  48. // 设置路由
  49. lay.children = []
  50. for (var route of store.getters.menus) {
  51. lay.children.push(...buildRoute(route))
  52. }
  53. lay.redirect = getc(lay).path
  54. router.addRoutes([lay])
  55. global.antRouter = []
  56. for (var route of store.getters.menus) {
  57. global.antRouter.push(...buildRoute(route, '', false))
  58. }
  59. next({
  60. ...to,
  61. replace: true
  62. })
  63. } catch (error) {
  64. await store.dispatch('user/resetToken')
  65. Message.error(error || 'Has Error')
  66. next('/login')
  67. NProgress.done()
  68. }
  69. }
  70. }
  71. } else {
  72. if (whiteList.indexOf(to.path) !== -1) {
  73. next()
  74. } else {
  75. next('/login')
  76. NProgress.done()
  77. }
  78. }
  79. })
  80. router.afterEach(() => {
  81. NProgress.done()
  82. })
  83. function buildRoute(route, parentUrl = '', bool = true) {
  84. const { url, moduleName, icon, moduleId, code, type, fullUrl, status, isCache } = route
  85. var item = {}
  86. var itemparent = null
  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. if (route.children && route.children.length) {
  100. if (type == 2) {
  101. var childrenPage = (route.children || []).filter(item => item.type == 2)
  102. if (childrenPage.length) {
  103. itemparent = {
  104. ...item,
  105. status: false,
  106. meta: {
  107. url: `/${code}_children`,
  108. isCache: 0,
  109. status: false,
  110. roles: [],
  111. roleItems: []
  112. },
  113. component: RouterView,
  114. children: []
  115. }
  116. childrenPage.map(child => {
  117. itemparent.children.push(...buildRoute(child, `${parentUrl}/${code}_children`, bool))
  118. })
  119. }
  120. try {
  121. item.component = _import(`${fullUrl}/index`)
  122. } catch (e) {
  123. console.log(e)
  124. }
  125. const roles = []
  126. const roleItems = []
  127. for (var role of route.children.filter(item => item.type == 3)) {
  128. roleItems.push({
  129. code: role.code,
  130. moduleName: role.moduleName
  131. })
  132. roles.push(role.code)
  133. }
  134. item.meta.roles = roles
  135. item.meta.roleItems = roleItems
  136. if (!pages.find(ite => ite.path === item.path)) {
  137. item.path = `${item.path}${bool ? '' : ''}`
  138. pages.push(item)
  139. }
  140. } else if (type == 1) {
  141. item.component = RouterView
  142. item.children = []
  143. route.children.filter(itema => itema.type !== 3).map(child => {
  144. item.children.push(...buildRoute(child, item.path, bool))
  145. })
  146. }
  147. } else {
  148. if (type == 4) {
  149. item.component = IframeView
  150. if (!pages.find(ite => ite.path === item.path)) {
  151. pages.push(item)
  152. }
  153. } else if (type == 2) {
  154. try {
  155. item.component = _import(`${fullUrl}/index`)
  156. if (!pages.find(ite => ite.path === item.path)) {
  157. item.path = `${item.path}${bool ? '' : ''}`
  158. pages.push(item)
  159. }
  160. } catch (e) {
  161. console.log(e)
  162. }
  163. }
  164. }
  165. return itemparent ? [item, itemparent] : [item]
  166. }