Rap 原分销系统代码Web
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

84 satır
2.3KB

  1. import { constantRouterMap } from '@/router/routers'
  2. import Layout from '@/layout/index'
  3. import ParentView from '@/components/ParentView'
  4. const permission = {
  5. state: {
  6. routers: constantRouterMap,
  7. addRouters: [],
  8. sidebarRouters: []
  9. },
  10. mutations: {
  11. SET_ROUTERS: (state, routers) => {
  12. state.addRouters = routers
  13. state.routers = constantRouterMap.concat(routers)
  14. },
  15. SET_SIDEBAR_ROUTERS: (state, routers) => {
  16. state.sidebarRouters = constantRouterMap.concat(routers)
  17. }
  18. },
  19. actions: {
  20. GenerateRoutes({ commit }, asyncRouter) {
  21. commit('SET_ROUTERS', asyncRouter)
  22. },
  23. SetSidebarRouters({ commit }, sidebarRouter) {
  24. commit('SET_SIDEBAR_ROUTERS', sidebarRouter)
  25. }
  26. }
  27. }
  28. export const filterAsyncRouter = (routers, lastRouter = false, type = false) => { // 遍历后台传来的路由字符串,转换为组件对象
  29. return routers.filter(router => {
  30. if (type && router.children) {
  31. router.children = filterChildren(router.children)
  32. }
  33. if (router.component) {
  34. if (router.component === 'Layout') { // Layout组件特殊处理
  35. router.component = Layout
  36. } else if (router.component === 'ParentView') {
  37. router.component = ParentView
  38. } else {
  39. const component = router.component
  40. router.component = loadView(component)
  41. }
  42. }
  43. if (router.children != null && router.children && router.children.length) {
  44. router.children = filterAsyncRouter(router.children, router, type)
  45. } else {
  46. delete router['children']
  47. delete router['redirect']
  48. }
  49. return true
  50. })
  51. }
  52. function filterChildren(childrenMap, lastRouter = false) {
  53. var children = []
  54. childrenMap.forEach((el, index) => {
  55. if (el.children && el.children.length) {
  56. if (el.component === 'ParentView') {
  57. el.children.forEach(c => {
  58. c.path = el.path + '/' + c.path
  59. if (c.children && c.children.length) {
  60. children = children.concat(filterChildren(c.children, c))
  61. return
  62. }
  63. children.push(c)
  64. })
  65. return
  66. }
  67. }
  68. if (lastRouter) {
  69. el.path = lastRouter.path + '/' + el.path
  70. }
  71. children = children.concat(el)
  72. })
  73. return children
  74. }
  75. export const loadView = (view) => {
  76. return (resolve) => require([`@/views/${view}`], resolve)
  77. }
  78. export default permission