permission.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_DEFAULT_ROUTES: (state, routes) => {
  17. state.defaultRoutes = constantRoutes.concat(routes)
  18. },
  19. SET_TOPBAR_ROUTES: (state, routes) => {
  20. state.topbarRouters = routes
  21. },
  22. SET_SIDEBAR_ROUTERS: (state, routes) => {
  23. state.sidebarRouters = routes
  24. },
  25. },
  26. actions: {
  27. // 生成路由
  28. GenerateRoutes({ commit }) {
  29. return new Promise(resolve => {
  30. // 向后端请求路由数据
  31. getRouters().then(res => {
  32. const sdata = JSON.parse(JSON.stringify(res.data))
  33. const rdata = JSON.parse(JSON.stringify(res.data))
  34. const sidebarRoutes = filterAsyncRouter(sdata)
  35. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  36. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  37. commit('SET_ROUTES', rewriteRoutes)
  38. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
  39. commit('SET_DEFAULT_ROUTES', sidebarRoutes)
  40. commit('SET_TOPBAR_ROUTES', sidebarRoutes)
  41. resolve(rewriteRoutes)
  42. })
  43. })
  44. }
  45. }
  46. }
  47. // 遍历后台传来的路由字符串,转换为组件对象
  48. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  49. return asyncRouterMap.filter(route => {
  50. // 将 ruoyi 后端原有耦合前端的逻辑,迁移到此处
  51. // 处理 meta 属性
  52. route.meta = {
  53. title: route.name,
  54. icon: route.icon
  55. }
  56. // 处理 component 属性
  57. if (route.children) { // 父节点
  58. if (route.parentId === 0) {
  59. route.component = Layout
  60. } else {
  61. route.component = ParentView
  62. }
  63. } else { // 根节点
  64. route.component = loadView(route.component)
  65. }
  66. // filterChildren
  67. if (type && route.children) {
  68. route.children = filterChildren(route.children)
  69. }
  70. if (route.children != null && route.children && route.children.length) {
  71. route.children = filterAsyncRouter(route.children, route, type)
  72. } else {
  73. delete route['children']
  74. }
  75. return true
  76. })
  77. }
  78. function filterChildren(childrenMap, lastRouter = false) {
  79. var children = []
  80. childrenMap.forEach((el, index) => {
  81. if (el.children && el.children.length) {
  82. if (el.component === 'ParentView' && !lastRouter) {
  83. el.children.forEach(c => {
  84. c.path = el.path + '/' + c.path
  85. if (c.children && c.children.length) {
  86. children = children.concat(filterChildren(c.children, c))
  87. return
  88. }
  89. children.push(c)
  90. })
  91. return
  92. }
  93. }
  94. if (lastRouter) {
  95. el.path = lastRouter.path + '/' + el.path
  96. }
  97. children = children.concat(el)
  98. })
  99. return children
  100. }
  101. export const loadView = (view) => { // 路由懒加载
  102. return (resolve) => require([`@/views/${view}`], resolve)
  103. }
  104. export default permission