handleRoutes.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @description 判断当前路由是否包含权限
  3. * @param permissions
  4. * @param route
  5. * @returns {boolean|*}
  6. */
  7. function hasPermission(permissions, route) {
  8. if (route.meta && route.meta.permissions) {
  9. return permissions.some((role) => route.meta.permissions.includes(role));
  10. } else {
  11. return false;
  12. }
  13. }
  14. /**
  15. * @description intelligence模式根据permissions数组拦截路由
  16. * @param routes
  17. * @param permissions
  18. * @returns {[]}
  19. */
  20. export function filterAsyncRoutes(routes, permissions) {
  21. const finallyRoutes = [];
  22. routes.forEach((route) => {
  23. const item = { ...route };
  24. if (hasPermission(permissions, item)) {
  25. if (item.children) {
  26. item.children = filterAsyncRoutes(item.children, permissions);
  27. }
  28. finallyRoutes.push(item);
  29. }
  30. });
  31. finallyRoutes.forEach((route) => {
  32. if (route.children) {
  33. if (route.children[0].children) {
  34. route.redirect =
  35. route.path +
  36. "/" +
  37. route.children[0].path +
  38. "/" +
  39. route.children[0].children[0].path;
  40. } else {
  41. route.redirect = route.path + "/" + route.children[0].path;
  42. }
  43. route.children.forEach((croute) => {
  44. if (croute.children) {
  45. croute.redirect =
  46. route.path + "/" + croute.path + "/" + croute.children[0].path;
  47. croute.children.forEach((i) => {
  48. if (i.children) {
  49. i.redirect =
  50. route.path +
  51. "/" +
  52. croute.path +
  53. "/" +
  54. i.path +
  55. "/" +
  56. i.children[0].path;
  57. }
  58. });
  59. }
  60. });
  61. }
  62. });
  63. return finallyRoutes;
  64. }
  65. function hasRoute(i, arr) {
  66. if (i && arr.length) {
  67. return arr.some((a) => {
  68. if (a.path.includes(":")) {
  69. return i.path.includes(a.path.substring(0, a.path.indexOf("/:")));
  70. } else {
  71. return i.path.includes(a.path);
  72. }
  73. });
  74. } else {
  75. return false;
  76. }
  77. }
  78. export function fillterMenuRoutes(data, stateRoute) {
  79. let currData = [];
  80. data.forEach((menu) => {
  81. const item = { ...menu };
  82. if (hasRoute(item, stateRoute)) {
  83. if (item.children) {
  84. item.children = fillterMenuRoutes(item.children, stateRoute);
  85. }
  86. currData.push(item);
  87. }
  88. });
  89. return currData;
  90. }