permission.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // 将 ruoyi 后端原有耦合前端的逻辑,迁移到此处
  43. // 处理 meta 属性
  44. route.meta = {
  45. title: route.name,
  46. icon: route.icon
  47. }
  48. // 处理 component 属性
  49. if (route.children) { // 父节点
  50. // debugger
  51. if (route.parentId === 0) {
  52. route.component = Layout
  53. } else {
  54. route.component = ParentView
  55. }
  56. } else { // 根节点
  57. route.component = loadView(route.component)
  58. }
  59. // filterChildren
  60. if (type && route.children) {
  61. route.children = filterChildren(route.children)
  62. }
  63. if (route.children != null && route.children && route.children.length) {
  64. route.children = filterAsyncRouter(route.children, route, type)
  65. } else {
  66. delete route['children']
  67. }
  68. return true
  69. })
  70. }
  71. function filterChildren(childrenMap, lastRouter = false) {
  72. var children = []
  73. childrenMap.forEach((el, index) => {
  74. if (el.children && el.children.length) {
  75. if (el.component === 'ParentView') {
  76. el.children.forEach(c => {
  77. c.path = el.path + '/' + c.path
  78. if (c.children && c.children.length) {
  79. children = children.concat(filterChildren(c.children, c))
  80. return
  81. }
  82. children.push(c)
  83. })
  84. return
  85. }
  86. }
  87. if (lastRouter) {
  88. el.path = lastRouter.path + '/' + el.path
  89. }
  90. children = children.concat(el)
  91. })
  92. return children
  93. }
  94. export const loadView = (view) => { // 路由懒加载
  95. return (resolve) => require([`@/views/${view}`], resolve)
  96. }
  97. export default permission