getMenu.js 4.8 KB

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