permission.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 sdata = JSON.parse(JSON.stringify(res))
  29. const rdata = JSON.parse(JSON.stringify(res))
  30. const sidebarRoutes = filterAsyncRouter(sdata)
  31. const rewriteRoutes = filterAsyncRouter(rdata, true)
  32. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  33. commit('SET_ROUTES', rewriteRoutes)
  34. commit('SET_SIDEBAR_ROUTERS', sidebarRoutes)
  35. resolve(rewriteRoutes)
  36. })
  37. })
  38. }
  39. }
  40. }
  41. // 遍历后台传来的路由字符串,转换为组件对象
  42. function filterAsyncRouter(asyncRouterMap, isRewrite = false) {
  43. return asyncRouterMap.filter(route => {
  44. // 将 ruoyi 后端原有耦合前端的逻辑,迁移到此处
  45. // 处理 meta 属性
  46. route.meta = {
  47. title: route.menuName,
  48. icon: route.icon
  49. }
  50. // 处理 component 属性
  51. if (route.children) { // 父节点
  52. // debugger
  53. if (route.parentId === 0) {
  54. route.component = Layout
  55. } else {
  56. route.component = ParentView
  57. }
  58. } else { // 根节点
  59. route.component = loadView(route.component)
  60. }
  61. // filterChildren
  62. if (isRewrite && route.children) {
  63. route.children = filterChildren(route.children)
  64. }
  65. if (route.children != null && route.children && route.children.length) {
  66. route.children = filterAsyncRouter(route.children, route, isRewrite)
  67. }
  68. return true
  69. })
  70. }
  71. function filterChildren(childrenMap) {
  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. children = children.concat(el)
  88. })
  89. return children
  90. }
  91. export const loadView = (view) => { // 路由懒加载
  92. return (resolve) => require([`@/views/${view}`], resolve)
  93. }
  94. export default permission