permission.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { constantRoutes } from '@/router'
  2. import { getRouters } from '@/api/menu'
  3. import Layout from '@/layout/index'
  4. import AdminLayout from '@/layout/AdminLayout'
  5. import ParentView from '@/components/ParentView';
  6. import InnerLink from '@/layout/components/InnerLink'
  7. const permission = {
  8. state: {
  9. routes: [],
  10. addRoutes: [],
  11. defaultRoutes: [],
  12. topbarRouters: [],
  13. sidebarRouters: []
  14. },
  15. mutations: {
  16. SET_ROUTES: (state, routes) => {
  17. state.addRoutes = routes
  18. state.routes = constantRoutes.concat(routes)
  19. },
  20. SET_DEFAULT_ROUTES: (state, routes) => {
  21. state.defaultRoutes = constantRoutes.concat(routes)
  22. },
  23. SET_TOPBAR_ROUTES: (state, routes) => {
  24. state.topbarRouters = routes;
  25. },
  26. SET_SIDEBAR_ROUTERS: (state, routes) => {
  27. state.sidebarRouters = routes
  28. },
  29. },
  30. actions: {
  31. // 生成路由
  32. GenerateRoutes({ commit, rootGetters }) {
  33. return new Promise(resolve => {
  34. // 向后端请求路由数据
  35. getRouters().then(res => {
  36. const sdata = JSON.parse(JSON.stringify(res.data))
  37. const rdata = JSON.parse(JSON.stringify(res.data))
  38. const sidebarRoutes = filterAsyncRouter(sdata)
  39. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  40. // 修复:为顶级路由path添加/前缀
  41. rewriteRoutes.forEach(route => {
  42. if (route.path && !route.path.startsWith('/')) {
  43. route.path = '/' + route.path;
  44. }
  45. });
  46. sidebarRoutes.forEach(route => {
  47. if (route.path && !route.path.startsWith('/')) {
  48. route.path = '/' + route.path;
  49. }
  50. });
  51. // 所有角色统一使用后端返回的菜单数据(去除admin硬编码绕过)
  52. const finalSidebarRoutes = sidebarRoutes
  53. const finalRewriteRoutes = rewriteRoutes
  54. finalRewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  55. commit('SET_ROUTES', finalRewriteRoutes)
  56. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(finalSidebarRoutes))
  57. commit('SET_DEFAULT_ROUTES', finalSidebarRoutes)
  58. commit('SET_TOPBAR_ROUTES', finalSidebarRoutes)
  59. resolve(finalRewriteRoutes)
  60. })
  61. })
  62. }
  63. }
  64. }
  65. // 遍历后台传来的路由字符串,转换为组件对象
  66. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  67. return asyncRouterMap.filter(route => {
  68. if (type && route.children) {
  69. route.children = filterChildren(route.children)
  70. }
  71. if (route.component) {
  72. if (route.component === 'Layout') {
  73. route.component = Layout
  74. } else if (route.component === 'AdminLayout') {
  75. route.component = AdminLayout
  76. } else if (route.component === 'ParentView') {
  77. route.component = ParentView
  78. } else if (route.component === 'InnerLink') {
  79. route.component = InnerLink
  80. } else {
  81. route.component = loadView(route.component)
  82. }
  83. }
  84. if (route.children != null && route.children && route.children.length) {
  85. route.children = filterAsyncRouter(route.children, route, type)
  86. } else {
  87. delete route['children']
  88. delete route['redirect']
  89. }
  90. return true
  91. })
  92. }
  93. function filterChildren(childrenMap, lastRouter = false) {
  94. var children = []
  95. childrenMap.forEach((el, index) => {
  96. if (el.children && el.children.length) {
  97. // ParentView 或空component(无实体页面的分组菜单)都需要展平
  98. if (el.component === 'ParentView' || !el.component) {
  99. el.children.forEach(c => {
  100. c.path = (el.path ? el.path + '/' : '') + c.path
  101. if (c.children && c.children.length) {
  102. children = children.concat(filterChildren(c.children, c))
  103. return
  104. }
  105. children.push(c)
  106. })
  107. return
  108. }
  109. }
  110. if (lastRouter) {
  111. el.path = lastRouter.path + '/' + el.path
  112. }
  113. children = children.concat(el)
  114. })
  115. return children
  116. }
  117. export const loadView = (view) => { // 路由懒加载
  118. return (resolve) => require([`@/views/${view}`], resolve)
  119. }
  120. export default permission