permission.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. state.topbarRouters = routes;
  30. },
  31. SET_SIDEBAR_ROUTERS: (state, routes) => {
  32. state.sidebarRouters = routes
  33. },
  34. },
  35. actions: {
  36. // 生成路由
  37. GenerateRoutes({ commit }) {
  38. return new Promise(resolve => {
  39. // 向后端请求路由数据
  40. getRouters().then(res => {
  41. const sdata = JSON.parse(JSON.stringify(res.data))
  42. const rdata = JSON.parse(JSON.stringify(res.data))
  43. const sidebarRoutes = filterAsyncRouter(sdata)
  44. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  45. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  46. commit('SET_ROUTES', rewriteRoutes)
  47. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
  48. commit('SET_DEFAULT_ROUTES', sidebarRoutes)
  49. commit('SET_TOPBAR_ROUTES', sidebarRoutes)
  50. resolve(rewriteRoutes)
  51. })
  52. })
  53. }
  54. }
  55. }
  56. // 遍历后台传来的路由字符串,转换为组件对象
  57. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  58. return asyncRouterMap.filter(route => {
  59. if (type && route.children) {
  60. route.children = filterChildren(route.children)
  61. }
  62. if (route.component) {
  63. // Layout ParentView 组件特殊处理
  64. if (route.component === 'Layout') {
  65. route.component = Layout
  66. } else if (route.component === 'ParentView') {
  67. route.component = ParentView
  68. } else if (route.component === 'InnerLink') {
  69. route.component = InnerLink
  70. } else {
  71. route.component = loadView(route.component)
  72. }
  73. }
  74. if (route.children != null && route.children && route.children.length) {
  75. route.children = filterAsyncRouter(route.children, route, type)
  76. } else {
  77. delete route['children']
  78. delete route['redirect']
  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') {
  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. // 去掉首尾斜杠与 .vue 后缀,避免后端 component 路径差异导致找不到模块
  108. const path = String(view || '').replace(/^\/+|\/+$/g, '').replace(/\.vue$/i, '')
  109. return (resolve) => require([`@/views/${path}`], resolve)
  110. }
  111. export default permission