permission.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import auth from '@/plugins/auth'
  2. import router, { constantRoutes, dynamicRoutes } from '@/router'
  3. import Layout from '@/layout/index'
  4. import ParentView from '@/components/ParentView'
  5. import InnerLink from '@/layout/components/InnerLink'
  6. import request from '@/utils/request'
  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. SET_R: (state, routes) => {
  30. state.routes = routes
  31. }
  32. },
  33. actions: {
  34. // 生成路由
  35. GenerateRoutes({ commit }) {
  36. return new Promise((resolve,reject) => {
  37. // 向后端请求路由数据
  38. request.get('/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. const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
  44. // rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  45. // router.addRoutes(asyncRoutes);
  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. changeRouters({ commit }, routers) {
  55. commit('SET_ROUTES', routers)
  56. },
  57. }
  58. }
  59. // 遍历后台传来的路由字符串,转换为组件对象
  60. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  61. return asyncRouterMap.filter(route => {
  62. if (type && route.children) {
  63. route.children = filterChildren(route.children)
  64. }
  65. if (route.component) {
  66. // Layout ParentView 组件特殊处理
  67. if (route.component === 'Layout') {
  68. route.component = Layout
  69. } else if (route.component === 'ParentView') {
  70. route.component = ParentView
  71. } else if (route.component === 'InnerLink') {
  72. route.component = InnerLink
  73. } else {
  74. route.component = loadView(route.component)
  75. }
  76. }
  77. if (route.children != null && route.children && route.children.length) {
  78. route.children = filterAsyncRouter(route.children, route, type)
  79. } else {
  80. delete route['children']
  81. delete route['redirect']
  82. }
  83. return true
  84. })
  85. }
  86. function filterChildren(childrenMap, lastRouter = false) {
  87. var children = []
  88. childrenMap.forEach((el, index) => {
  89. if (el.children && el.children.length) {
  90. if (el.component === 'ParentView' && !lastRouter) {
  91. el.children.forEach(c => {
  92. c.path = el.path + '/' + c.path
  93. if (c.children && c.children.length) {
  94. children = children.concat(filterChildren(c.children, c))
  95. return
  96. }
  97. children.push(c)
  98. })
  99. return
  100. }
  101. }
  102. if (lastRouter) {
  103. el.path = lastRouter.path + '/' + el.path
  104. }
  105. children = children.concat(el)
  106. })
  107. return children
  108. }
  109. // 动态路由遍历,验证是否具备权限
  110. export function filterDynamicRoutes(routes) {
  111. const res = []
  112. routes.forEach(route => {
  113. if (route.permissions) {
  114. if (auth.hasPermiOr(route.permissions)) {
  115. res.push(route)
  116. }
  117. } else if (route.roles) {
  118. if (auth.hasRoleOr(route.roles)) {
  119. res.push(route)
  120. }
  121. }
  122. })
  123. return res
  124. }
  125. export const loadView = (view) => {
  126. if (process.env.NODE_ENV === 'development') {
  127. return (resolve) => require([`@/views/${view}`], resolve)
  128. } else {
  129. //使用 import 实现生产环境的路由懒加载
  130. return () => import(`@/views/${view}`)
  131. }
  132. }
  133. export default permission