permission.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import {constantRoutes} from '@/router'
  2. import Layout from '@/layout/index'
  3. import ParentView from '@/components/ParentView';
  4. import {toCamelCase} from "@/utils";
  5. const permission = {
  6. state: {
  7. routes: [],
  8. addRoutes: [],
  9. sidebarRouters: [], // 左侧边菜单的路由,被 Sidebar/index.vue 使用
  10. topbarRouters: [], // 顶部菜单的路由,被 TopNav/index.vue 使用
  11. },
  12. mutations: {
  13. SET_ROUTES: (state, routes) => {
  14. state.addRoutes = routes
  15. state.routes = constantRoutes.concat(routes)
  16. },
  17. SET_DEFAULT_ROUTES: (state, routes) => {
  18. state.defaultRoutes = constantRoutes.concat(routes)
  19. },
  20. SET_TOPBAR_ROUTES: (state, routes) => {
  21. state.topbarRouters = routes
  22. },
  23. SET_SIDEBAR_ROUTERS: (state, routes) => {
  24. state.sidebarRouters = routes
  25. },
  26. },
  27. actions: {
  28. /**
  29. * 生成路由
  30. *
  31. * @param commit commit 函数
  32. * @param menus 路由参数
  33. */
  34. GenerateRoutes({commit}, menus) {
  35. return new Promise(resolve => {
  36. // 将 menus 菜单,转换为 route 路由数组
  37. const sdata = JSON.parse(JSON.stringify(menus)) // 【重要】用于菜单中的数据
  38. const rdata = JSON.parse(JSON.stringify(menus)) // 用于最后添加到 Router 中的数据
  39. const sidebarRoutes = filterAsyncRouter(sdata)
  40. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  41. rewriteRoutes.push({path: '*', redirect: '/404', hidden: true})
  42. commit('SET_ROUTES', rewriteRoutes)
  43. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
  44. commit('SET_DEFAULT_ROUTES', sidebarRoutes)
  45. commit('SET_TOPBAR_ROUTES', sidebarRoutes)
  46. resolve(rewriteRoutes)
  47. })
  48. }
  49. }
  50. }
  51. // 遍历后台传来的路由字符串,转换为组件对象
  52. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  53. return asyncRouterMap.filter(route => {
  54. // 将 ruoyi 后端原有耦合前端的逻辑,迁移到此处
  55. // 处理 meta 属性
  56. route.meta = {
  57. title: route.name,
  58. icon: route.icon,
  59. noCache: !route.keepAlive,
  60. }
  61. route.hidden = !route.visible
  62. // 处理 name 属性
  63. if (route.componentName && route.componentName.length > 0) {
  64. route.name = route.componentName
  65. } else {
  66. // 路由地址转首字母大写驼峰,作为路由名称,适配 keepAlive
  67. route.name = toCamelCase(route.path, true)
  68. // 处理三级及以上菜单路由缓存问题,将 path 名字赋值给 name
  69. if (route.path.indexOf("/") !== -1) {
  70. const pathArr = route.path.split("/");
  71. route.name = toCamelCase(pathArr[pathArr.length - 1], true)
  72. }
  73. }
  74. // 处理 component 属性
  75. if (route.children) { // 父节点
  76. if (route.parentId == localStorage.getItem('parentId')) {
  77. route.component = Layout
  78. } else {
  79. route.component = ParentView
  80. }
  81. } else { // 根节点
  82. route.component = loadView(route.component)
  83. }
  84. // filterChildren
  85. if (type && route.children) {
  86. route.children = filterChildren(route.children)
  87. }
  88. if (route.children != null && route.children && route.children.length) {
  89. route.children = filterAsyncRouter(route.children, route, type)
  90. route.alwaysShow = route.alwaysShow !== undefined ? route.alwaysShow : true
  91. } else {
  92. delete route['children']
  93. delete route['alwaysShow'] // 如果没有子菜单,就不需要考虑 alwaysShow 字段
  94. }
  95. return true
  96. })
  97. }
  98. function filterChildren(childrenMap, lastRouter = false) {
  99. let children = [];
  100. childrenMap.forEach((el, index) => {
  101. if (el.children && el.children.length) {
  102. if (!el.component && !lastRouter) {
  103. el.children.forEach(c => {
  104. c.path = el.path + '/' + c.path
  105. if (c.children && c.children.length) {
  106. children = children.concat(filterChildren(c.children, c))
  107. return
  108. }
  109. children.push(c)
  110. })
  111. return
  112. }
  113. }
  114. if (lastRouter) {
  115. el.path = lastRouter.path + '/' + el.path
  116. }
  117. children = children.concat(el)
  118. })
  119. return children
  120. }
  121. export const loadView = (view) => { // 路由懒加载
  122. return (resolve) => require([`@/views/${view}`], resolve)
  123. }
  124. export default permission