permission.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // 顶部导航菜单默认添加统计报表栏指向首页
  25. const index = [{
  26. path: 'index',
  27. meta: { title: '统计报表', icon: 'dashboard' }
  28. }]
  29. state.topbarRouters = routes.concat(index);
  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. const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
  46. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  47. router.addRoutes(asyncRoutes);
  48. commit('SET_ROUTES', rewriteRoutes)
  49. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
  50. commit('SET_DEFAULT_ROUTES', sidebarRoutes)
  51. commit('SET_TOPBAR_ROUTES', sidebarRoutes)
  52. resolve(rewriteRoutes)
  53. })
  54. })
  55. }
  56. }
  57. }
  58. // 遍历后台传来的路由字符串,转换为组件对象
  59. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  60. return asyncRouterMap.filter(route => {
  61. if (type && route.children) {
  62. route.children = filterChildren(route.children)
  63. }
  64. if (route.component) {
  65. // Layout ParentView 组件特殊处理
  66. if (route.component === 'Layout') {
  67. route.component = Layout
  68. } else if (route.component === 'ParentView') {
  69. route.component = ParentView
  70. } else if (route.component === 'InnerLink') {
  71. route.component = InnerLink
  72. } else {
  73. route.component = loadView(route.component)
  74. }
  75. }
  76. if (route.children != null && route.children && route.children.length) {
  77. route.children = filterAsyncRouter(route.children, route, type)
  78. } else {
  79. delete route['children']
  80. delete route['redirect']
  81. }
  82. return true
  83. })
  84. }
  85. function filterChildren(childrenMap, lastRouter = false) {
  86. var children = []
  87. childrenMap.forEach((el, index) => {
  88. if (el.children && el.children.length) {
  89. if (el.component === 'ParentView' && !lastRouter) {
  90. el.children.forEach(c => {
  91. c.path = el.path + '/' + c.path
  92. if (c.children && c.children.length) {
  93. children = children.concat(filterChildren(c.children, c))
  94. return
  95. }
  96. children.push(c)
  97. })
  98. return
  99. }
  100. }
  101. if (lastRouter) {
  102. el.path = lastRouter.path + '/' + el.path
  103. }
  104. children = children.concat(el)
  105. })
  106. return children
  107. }
  108. // 动态路由遍历,验证是否具备权限
  109. export function filterDynamicRoutes(routes) {
  110. const res = []
  111. routes.forEach(route => {
  112. if (route.permissions) {
  113. if (auth.hasPermiOr(route.permissions)) {
  114. res.push(route)
  115. }
  116. } else if (route.roles) {
  117. if (auth.hasRoleOr(route.roles)) {
  118. res.push(route)
  119. }
  120. }
  121. })
  122. return res
  123. }
  124. export const loadView = (view) => {
  125. if (process.env.NODE_ENV === 'development') {
  126. return (resolve) => require([`@/views/${view}`], resolve)
  127. } else {
  128. // 使用 import 实现生产环境的路由懒加载
  129. return () => import(`@/views/${view}`)
  130. }
  131. }
  132. export default permission