permission.js 3.4 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. defaultRoutes: [],
  10. topbarRouters: [],
  11. sidebarRouters: []
  12. },
  13. mutations: {
  14. SET_ROUTES: (state, routes) => {
  15. state.addRoutes = routes
  16. state.routes = constantRoutes.concat(routes)
  17. },
  18. SET_DEFAULT_ROUTES: (state, routes) => {
  19. state.defaultRoutes = constantRoutes.concat(routes)
  20. },
  21. SET_TOPBAR_ROUTES: (state, routes) => {
  22. // 顶部导航菜单默认添加统计报表栏指向首页
  23. const index = [{
  24. path: 'index',
  25. meta: { title: '统计报表', icon: 'dashboard'}
  26. }]
  27. state.topbarRouters = routes.concat(index);
  28. },
  29. SET_SIDEBAR_ROUTERS: (state, routes) => {
  30. state.sidebarRouters = routes
  31. },
  32. },
  33. actions: {
  34. // 生成路由
  35. GenerateRoutes({ commit }) {
  36. return new Promise(resolve => {
  37. // 向后端请求路由数据
  38. getRouters().then(res => {
  39. const sdata = JSON.parse(JSON.stringify(res.data))
  40. const rdata = JSON.parse(JSON.stringify(res.data))
  41. const sidebarRoutes = filterAsyncRouter(sdata)
  42. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  43. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  44. commit('SET_ROUTES', rewriteRoutes)
  45. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
  46. commit('SET_DEFAULT_ROUTES', sidebarRoutes)
  47. commit('SET_TOPBAR_ROUTES', sidebarRoutes)
  48. resolve(rewriteRoutes)
  49. })
  50. })
  51. }
  52. }
  53. }
  54. // 遍历后台传来的路由字符串,转换为组件对象
  55. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  56. return asyncRouterMap.filter(route => {
  57. if (type && route.children) {
  58. route.children = filterChildren(route.children)
  59. }
  60. if (route.component) {
  61. // Layout ParentView 组件特殊处理
  62. if (route.component === 'Layout') {
  63. route.component = Layout
  64. } else if (route.component === 'ParentView') {
  65. route.component = ParentView
  66. } else {
  67. route.component = loadView(route.component)
  68. }
  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. delete route['redirect']
  75. }
  76. return true
  77. })
  78. }
  79. function filterChildren(childrenMap, lastRouter = false) {
  80. var children = []
  81. childrenMap.forEach((el, index) => {
  82. if (el.children && el.children.length) {
  83. if (el.component === 'ParentView') {
  84. el.children.forEach(c => {
  85. c.path = el.path + '/' + c.path
  86. if (c.children && c.children.length) {
  87. children = children.concat(filterChildren(c.children, c))
  88. return
  89. }
  90. children.push(c)
  91. })
  92. return
  93. }
  94. }
  95. if (lastRouter) {
  96. el.path = lastRouter.path + '/' + el.path
  97. }
  98. children = children.concat(el)
  99. })
  100. return children
  101. }
  102. export const loadView = (view) => { // 路由懒加载
  103. return (resolve) => require([`@/views/${view}`], resolve)
  104. }
  105. export default permission