permission.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;
  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. //大屏跳转判定(路由地址修改)
  42. let newData = JSON.parse(JSON.stringify(res.data));
  43. localStorage.setItem('routeData',JSON.stringify(newData))
  44. for(let i=0;i<newData.length;i++){
  45. if( newData[i].path == 'https://www.sxitdlc.com'){
  46. // console.log('newData[i]',newData[i])
  47. newData[i].name = localStorage.getItem('screenUrl') + '?identity=' + localStorage.getItem('identity')
  48. newData[i].path = localStorage.getItem('screenUrl') + '?identity=' + localStorage.getItem('identity')
  49. }
  50. }
  51. const sdata = JSON.parse(JSON.stringify(newData))
  52. const rdata = JSON.parse(JSON.stringify(newData))
  53. const sidebarRoutes = filterAsyncRouter(sdata)
  54. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  55. rewriteRoutes.push({ path: '*', redirect: '/login', hidden: true })
  56. // rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  57. commit('SET_ROUTES', rewriteRoutes)
  58. commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
  59. commit('SET_DEFAULT_ROUTES', sidebarRoutes)
  60. commit('SET_TOPBAR_ROUTES', sidebarRoutes)
  61. resolve(rewriteRoutes)
  62. })
  63. })
  64. }
  65. }
  66. }
  67. // 遍历后台传来的路由字符串,转换为组件对象
  68. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  69. return asyncRouterMap.filter(route => {
  70. if (type && route.children) {
  71. route.children = filterChildren(route.children)
  72. }
  73. if (route.component) {
  74. // Layout ParentView 组件特殊处理
  75. if (route.component === 'Layout') {
  76. route.component = Layout
  77. } else if (route.component === 'ParentView') {
  78. route.component = ParentView
  79. } else if (route.component === 'InnerLink') {
  80. route.component = InnerLink
  81. } else {
  82. route.component = loadView(route.component)
  83. }
  84. }
  85. if (route.children != null && route.children && route.children.length) {
  86. route.children = filterAsyncRouter(route.children, route, type)
  87. } else {
  88. delete route['children']
  89. delete route['redirect']
  90. }
  91. return true
  92. })
  93. }
  94. function filterChildren(childrenMap, lastRouter = false) {
  95. var children = []
  96. childrenMap.forEach((el, index) => {
  97. if (el.children && el.children.length) {
  98. if (el.component === 'ParentView') {
  99. el.children.forEach(c => {
  100. c.path = el.path + '/' + c.path
  101. if (c.children && c.children.length) {
  102. children = children.concat(filterChildren(c.children, c))
  103. return
  104. }
  105. children.push(c)
  106. })
  107. return
  108. }
  109. }
  110. if (lastRouter) {
  111. el.path = lastRouter.path + '/' + el.path
  112. }
  113. children = children.concat(el)
  114. })
  115. return children
  116. }
  117. export const loadView = (view) => { // 路由懒加载
  118. return (resolve) => require([`@/views/${view}`], resolve)
  119. }
  120. export default permission