getMenu.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. const nd = treeData.filter(item => item.auth_id)
  47. store.dispatch('auth/changeAuthArrs', nd)
  48. // const typeData = setType(treeData, 'up_auth_id', 'auth_id')
  49. const menusArray = parseMenuItem(nd)
  50. const treeMenu = listToTree(menusArray, 'up_auth_id', 'auth_id')
  51. const dataMenu = _.unionBy(treeMenu, 'auth_id')
  52. const menus = parseMenu(dataMenu)
  53. if (PLATFROM_CONFIG.hasStaticRoutes) {
  54. const staticRoutes = await (await import('@/router/routes/routes-file-temp')).default
  55. setStaticRoutes(menus, staticRoutes)
  56. }
  57. store.dispatch('permission/setRoutes', menus)
  58. router.addRoutes(menus)
  59. next({ ...to, replace: true })
  60. } catch (error) {
  61. // remove token and go to login page to re-login
  62. await store.dispatch('user/resetToken')
  63. Message.error(error || 'Has Error')
  64. next(`/login?redirect=${to.path}`)
  65. NProgress.done()
  66. }
  67. }
  68. }
  69. } else {
  70. /* has no token*/
  71. if (whiteList.indexOf(to.path) !== -1) {
  72. // in the free login whitelist, go directly
  73. next()
  74. } else {
  75. // other pages that do not have permission to access are redirected to the login page.
  76. next(`/login?redirect=${to.path}`)
  77. NProgress.done()
  78. }
  79. }
  80. })
  81. function parseMenuItem (data) {
  82. const menus = []
  83. data.map(item => {
  84. if (item.auth_type == 1) {
  85. menus.push({
  86. auth_id: item.auth_id,
  87. up_auth_id: item.up_auth_id,
  88. path: item.route_info,
  89. name: item.auth_ident,
  90. meta: {
  91. title: item.auth_name,
  92. elSvgIcon: item.show_icon ?? 'Fold',
  93. show_index: item.show_index,
  94. qid: item.queryTemplateID,
  95. },
  96. component: Layout,
  97. hidden: !item.is_show,
  98. })
  99. } else if (item.auth_type == 2) {
  100. menus.push({
  101. auth_id: item.auth_id,
  102. up_auth_id: item.up_auth_id,
  103. path: item.route_info,
  104. name: item.auth_ident,
  105. meta: {
  106. title: item.auth_name,
  107. elSvgIcon: item.show_icon ?? 'Fold',
  108. show_index: item.show_index,
  109. qid: item.queryTemplateID,
  110. auth_id: item.auth_id,
  111. up_auth_id: item.up_auth_id,
  112. },
  113. component: resolve => require(['@/views' + item.file_link], resolve),
  114. // component: () => import('./views/table/index.vue'),
  115. hidden: !item.is_show,
  116. })
  117. }
  118. })
  119. return menus
  120. }
  121. function setType (arr, parentKey, key) {
  122. const datas = []
  123. for (let i = 0; i < arr.length; i++) {
  124. for (let j = 0; j < arr.length; j++) {
  125. if (arr[i][key] == arr[j][parentKey]) {
  126. if (arr[j].auth_type == 3) {
  127. datas.push(arr[j])
  128. arr[i].other = datas
  129. }
  130. }
  131. }
  132. }
  133. return arr
  134. }
  135. function parseMenu (arr) {
  136. const menus = arr
  137. const newMenus = menus.flat()
  138. const allMenus = newMenus.length
  139. ? newMenus.sort((a, b) => a.meta.show_index - b.meta.show_index)
  140. : []
  141. // allMenus.map(item => {
  142. // item.children = (item.children && item.children.length) ? item.children.sort((a, b) => a.meta.show_index - b.meta.show_index) : item.children
  143. // })
  144. allMenus.push({
  145. path: '/:pathMatch(.*)',
  146. redirect: '/404',
  147. hidden: true,
  148. })
  149. if (allMenus[0].children && allMenus[0].children.length) {
  150. allMenus[0].redirect = allMenus[0].children[0].path
  151. allMenus[0].path = '/'
  152. } else {
  153. allMenus[0].redirect = allMenus[0].path
  154. allMenus[0].path = '/'
  155. }
  156. return allMenus
  157. }
  158. router.afterEach(() => {
  159. // finish progress bar
  160. NProgress.done()
  161. })