permission.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { constantRoutes } from '@/router'
  2. import { getRouters } from '@/api/menu'
  3. import Layout from '@/layout/index'
  4. import ParentView from '@/components/ParentView';
  5. const permission = {
  6. state: {
  7. routes: [],
  8. addRoutes: [],
  9. sidebarRouters: []
  10. },
  11. mutations: {
  12. SET_ROUTES: (state, routes) => {
  13. state.addRoutes = routes
  14. state.routes = constantRoutes.concat(routes)
  15. },
  16. SET_SIDEBAR_ROUTERS: (state, routers) => {
  17. state.sidebarRouters = constantRoutes.concat(routers)
  18. },
  19. },
  20. actions: {
  21. // 生成路由
  22. GenerateRoutes({ commit }) {
  23. return new Promise(resolve => {
  24. // 向后端请求路由数据
  25. getRouters().then(res => {
  26. const sdata = JSON.parse(JSON.stringify(res.data))
  27. const rdata = JSON.parse(JSON.stringify(res.data))
  28. const sidebarRoutes = filterAsyncRouter(sdata)
  29. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  30. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  31. commit('SET_ROUTES', rewriteRoutes)
  32. commit('SET_SIDEBAR_ROUTERS', sidebarRoutes)
  33. resolve(rewriteRoutes)
  34. })
  35. })
  36. }
  37. }
  38. }
  39. // 遍历后台传来的路由字符串,转换为组件对象
  40. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  41. return asyncRouterMap.filter(route => {
  42. if (type && route.children) {
  43. route.children = filterChildren(route.children)
  44. }
  45. if (route.component) {
  46. // Layout ParentView 组件特殊处理
  47. if (route.component === 'Layout') {
  48. route.component = Layout
  49. } else if (route.component === 'ParentView') {
  50. route.component = ParentView
  51. } else {
  52. route.component = loadView(route.component)
  53. }
  54. }
  55. if (route.children != null && route.children && route.children.length) {
  56. route.children = filterAsyncRouter(route.children, route, type)
  57. } else {
  58. delete route['children']
  59. delete route['redirect']
  60. }
  61. return true
  62. })
  63. }
  64. function filterChildren(childrenMap, lastRouter = false) {
  65. var children = []
  66. childrenMap.forEach((el, index) => {
  67. if (el.children && el.children.length) {
  68. if (el.component === 'ParentView') {
  69. el.children.forEach(c => {
  70. c.path = el.path + '/' + c.path
  71. if (c.children && c.children.length) {
  72. children = children.concat(filterChildren(c.children, c))
  73. return
  74. }
  75. children.push(c)
  76. })
  77. return
  78. }
  79. }
  80. if (lastRouter) {
  81. el.path = lastRouter.path + '/' + el.path
  82. }
  83. children = children.concat(el)
  84. })
  85. return children
  86. }
  87. export const loadView = (view) => { // 路由懒加载
  88. return (resolve) => require([`@/views/${view}`], resolve)
  89. }
  90. export default permission