permission.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import auth from '@/plugins/auth'
  2. import router, { constantRoutes, dynamicRoutes } from '@/router'
  3. import { getRouters } from '@/api/menu'
  4. import Layout from '@/layout/index'
  5. import ParentView from '@/components/ParentView'
  6. import InnerLink from '@/layout/components/InnerLink'
  7. const permission = {
  8. state: {
  9. routes: [],
  10. addRoutes: [],
  11. defaultRoutes: [],
  12. topbarRouters: [],
  13. sidebarRouters: []
  14. },
  15. mutations: {
  16. SET_ROUTES: (state, routes) => {
  17. state.addRoutes = routes
  18. state.routes = constantRoutes.concat(routes)
  19. },
  20. SET_DEFAULT_ROUTES: (state, routes) => {
  21. state.defaultRoutes = constantRoutes.concat(routes)
  22. },
  23. SET_TOPBAR_ROUTES: (state, routes) => {
  24. state.topbarRouters = routes
  25. },
  26. SET_SIDEBAR_ROUTERS: (state, routes) => {
  27. state.sidebarRouters = routes
  28. },
  29. },
  30. actions: {
  31. // 生成路由
  32. GenerateRoutes({ commit }) {
  33. return new Promise(resolve => {
  34. // 向后端请求路由数据
  35. getRouters().then(res => {
  36. const sdata = JSON.parse(JSON.stringify(res.data))
  37. const rdata = JSON.parse(JSON.stringify(res.data))
  38. const sidebarRoutes = filterAsyncRouter(sdata)
  39. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  40. const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
  41. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  42. router.addRoutes(asyncRoutes);
  43. commit('SET_ROUTES', rewriteRoutes)
  44. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
  45. commit('SET_DEFAULT_ROUTES', sidebarRoutes)
  46. commit('SET_TOPBAR_ROUTES', sidebarRoutes)
  47. resolve(rewriteRoutes)
  48. })
  49. })
  50. }
  51. }
  52. }
  53. // 遍历后台传来的路由字符串,转换为组件对象
  54. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  55. return asyncRouterMap.filter(route => {
  56. if (type && route.children) {
  57. route.children = filterChildren(route.children)
  58. }
  59. if (route.component) {
  60. // Layout ParentView 组件特殊处理
  61. if (route.component === 'Layout') {
  62. route.component = Layout
  63. } else if (route.component === 'ParentView') {
  64. route.component = ParentView
  65. } else if (route.component === 'InnerLink') {
  66. route.component = InnerLink
  67. } else {
  68. route.component = loadView(route.component)
  69. }
  70. }
  71. if (route.children != null && route.children && route.children.length) {
  72. route.children = filterAsyncRouter(route.children, route, type)
  73. } else {
  74. delete route['children']
  75. delete route['redirect']
  76. }
  77. return true
  78. })
  79. }
  80. function filterChildren(childrenMap, lastRouter = false) {
  81. var children = []
  82. childrenMap.forEach((el, index) => {
  83. if (el.children && el.children.length) {
  84. if (el.component === 'ParentView' && !lastRouter) {
  85. el.children.forEach(c => {
  86. c.path = el.path + '/' + c.path
  87. if (c.children && c.children.length) {
  88. children = children.concat(filterChildren(c.children, c))
  89. return
  90. }
  91. children.push(c)
  92. })
  93. return
  94. }
  95. }
  96. if (lastRouter) {
  97. el.path = lastRouter.path + '/' + el.path
  98. if (el.children && el.children.length) {
  99. children = children.concat(filterChildren(el.children, el))
  100. return
  101. }
  102. }
  103. children = children.concat(el)
  104. })
  105. return children
  106. }
  107. // 动态路由遍历,验证是否具备权限
  108. export function filterDynamicRoutes(routes) {
  109. const res = []
  110. routes.forEach(route => {
  111. if (route.permissions) {
  112. if (auth.hasPermiOr(route.permissions)) {
  113. res.push(route)
  114. }
  115. } else if (route.roles) {
  116. if (auth.hasRoleOr(route.roles)) {
  117. res.push(route)
  118. }
  119. }
  120. })
  121. return res
  122. }
  123. export const loadView = (view) => {
  124. if (process.env.NODE_ENV === 'development') {
  125. return (resolve) => require([`@/views/${view}`], resolve)
  126. } else {
  127. // 使用 import 实现生产环境的路由懒加载
  128. return () => import(`@/views/${view}`)
  129. }
  130. }
  131. export default permission