getMenu.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Layout from '@/layout'
  9. import { setTree, listToTree } from '@/utils/validate'
  10. import * as _ from 'lodash'
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. const whiteList = ['/login'] // no redirect whitelist
  13. function setStaticRoutes (menus, staticRoutes) {
  14. staticRoutes.forEach(route => {
  15. const routeIndex = menus.findIndex(
  16. permissionRoute => permissionRoute.name === route.name
  17. )
  18. if (routeIndex > -1) {
  19. menus[routeIndex] = {
  20. ...menus[routeIndex],
  21. component: route.component,
  22. children: route.children,
  23. }
  24. }
  25. })
  26. }
  27. router.beforeEach(async (to, from, next) => {
  28. // start progress bar
  29. NProgress.start()
  30. // set page title
  31. document.title = getPageTitle(to.meta.title)
  32. // determine whether the user has logged in
  33. const hasToken = getToken()
  34. if (hasToken) {
  35. if (to.path === '/login') {
  36. // if is logged in, redirect to the home page
  37. next({ path: '/' })
  38. NProgress.done()
  39. } else {
  40. const hasRoles = store.getters.roles && store.getters.roles.length > 0
  41. if (hasRoles) {
  42. next()
  43. } else {
  44. try {
  45. const treeData = await store.dispatch('user/getMenuInfo')
  46. store.dispatch('auth/changeAuthArrs', treeData)
  47. // const typeData = setType(treeData, 'up_auth_id', 'auth_id')
  48. const menusArray = parseMenuItem(treeData)
  49. const treeMenu = listToTree(menusArray, 'up_auth_id', 'auth_id')
  50. const dataMenu = _.unionBy(treeMenu, 'auth_id')
  51. const menus = parseMenu(dataMenu)
  52. if (PLATFROM_CONFIG.hasStaticRoutes) {
  53. const staticRoutes = await (await import('@/router/routes/routes-file-temp')).default
  54. setStaticRoutes(menus, staticRoutes)
  55. }
  56. store.dispatch('permission/setRoutes', menus)
  57. router.addRoutes(menus)
  58. next({ ...to, replace: true })
  59. } catch (error) {
  60. // remove token and go to login page to re-login
  61. await store.dispatch('user/resetToken')
  62. Message.error(error || 'Has Error')
  63. next(`/login?redirect=${to.path}`)
  64. NProgress.done()
  65. }
  66. }
  67. }
  68. } else {
  69. /* has no token*/
  70. if (whiteList.indexOf(to.path) !== -1) {
  71. // in the free login whitelist, go directly
  72. next()
  73. } else {
  74. // other pages that do not have permission to access are redirected to the login page.
  75. next(`/login?redirect=${to.path}`)
  76. NProgress.done()
  77. }
  78. }
  79. })
  80. function parseMenuItem (data) {
  81. const menus = []
  82. data.map(item => {
  83. if (item.auth_type == 1) {
  84. menus.push({
  85. auth_id: item.auth_id,
  86. up_auth_id: item.up_auth_id,
  87. path: item.route_info,
  88. name: item.auth_ident,
  89. meta: {
  90. title: item.auth_name,
  91. elSvgIcon: item.show_icon ?? 'Fold',
  92. show_index: item.show_index,
  93. qid: item.queryTemplateID,
  94. },
  95. component: Layout,
  96. hidden: !item.is_show,
  97. })
  98. } else if (item.auth_type == 2) {
  99. menus.push({
  100. auth_id: item.auth_id,
  101. up_auth_id: item.up_auth_id,
  102. path: item.route_info,
  103. name: item.auth_ident,
  104. meta: {
  105. title: item.auth_name,
  106. elSvgIcon: item.show_icon ?? 'Fold',
  107. show_index: item.show_index,
  108. qid: item.queryTemplateID,
  109. auth_id: item.auth_id,
  110. up_auth_id: item.up_auth_id,
  111. },
  112. component: resolve => require(['@/views' + item.file_link], resolve),
  113. // component: () => import('./views/table/index.vue'),
  114. hidden: !item.is_show,
  115. })
  116. }
  117. })
  118. return menus
  119. }
  120. function setType (arr, parentKey, key) {
  121. const datas = []
  122. for (let i = 0; i < arr.length; i++) {
  123. for (let j = 0; j < arr.length; j++) {
  124. if (arr[i][key] == arr[j][parentKey]) {
  125. if (arr[j].auth_type == 3) {
  126. datas.push(arr[j])
  127. arr[i].other = datas
  128. }
  129. }
  130. }
  131. }
  132. return arr
  133. }
  134. function parseMenu (arr) {
  135. const menus = arr
  136. const newMenus = menus.flat()
  137. const allMenus = newMenus.length
  138. ? newMenus.sort((a, b) => a.meta.show_index - b.meta.show_index)
  139. : []
  140. // allMenus.map(item => {
  141. // item.children = (item.children && item.children.length) ? item.children.sort((a, b) => a.meta.show_index - b.meta.show_index) : item.children
  142. // })
  143. allMenus.push({
  144. path: '/:pathMatch(.*)',
  145. redirect: '/404',
  146. hidden: true,
  147. })
  148. if (allMenus[0].children && allMenus[0].children.length) {
  149. allMenus[0].redirect = allMenus[0].children[0].path
  150. allMenus[0].path = '/'
  151. } else {
  152. allMenus[0].redirect = allMenus[0].path
  153. allMenus[0].path = '/'
  154. }
  155. return allMenus
  156. }
  157. router.afterEach(() => {
  158. // finish progress bar
  159. NProgress.done()
  160. })