permission.js 3.7 KB

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