permission.js 3.4 KB

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