getMenu.js 5.6 KB

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