26a2d87778dad73b47a4c6d8d7052279f7eca98d.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // import { asyncRoutes, constantRoutes } from '@/router'
  2. import { constantRoutes } from '@/router'
  3. import { validatenull } from '@/utils/validate'
  4. // for get menus from server
  5. // import { getMenuByRole } from '@/api/menu'
  6. import { getRecuData,transData,GetDataByName } from '@/api/common'
  7. import Layout from '@/layout'
  8. function buildRouter(aMenu) {
  9. const aRouter = []
  10. aMenu.forEach(item => {
  11. if (!validatenull(item.component)) {
  12. const oRouter = {
  13. meta: { 'title': '', 'icon': '' },
  14. children: []
  15. }
  16. if (item.component !== 'Iframe') {
  17. if (item.component === 'Layout') {
  18. oRouter.component = Layout
  19. } else {
  20. oRouter.component = require('@/views/' + item.component + '.vue').default
  21. }
  22. oRouter.name = item.path
  23. oRouter.id = item.id || null
  24. if (item.redirect !== '') {
  25. oRouter.redirect = item.redirect || null
  26. oRouter.path = '/' + item.path
  27. } else {
  28. oRouter.path = item.path
  29. }
  30. // console.log(item)
  31. if(item.seebutton == 1 || item.seebutton == 2){
  32. oRouter.meta.icon = item.icon
  33. oRouter.meta.title = item.title
  34. // oRouter.meta.noCache = item.noCache || false
  35. oRouter.meta.noCache = true
  36. // oRouter.meta.breadcrumb = item.breadcrumb || true
  37. oRouter.children = validatenull(item.children) ? [] : buildRouter(item.children)
  38. aRouter.push(oRouter)
  39. }
  40. }
  41. }
  42. })
  43. return aRouter
  44. }
  45. /**
  46. * Use meta.role to determine if the current user has permission
  47. * @param roles
  48. * @param route
  49. */
  50. function hasPermission(roles, route) {
  51. if (route.meta && route.meta.roles) {
  52. return roles.some(role => route.meta.roles.includes(role))
  53. } else {
  54. return true
  55. }
  56. }
  57. /**
  58. * Filter asynchronous routing tables by recursion
  59. * @param routes asyncRoutes
  60. * @param roles
  61. */
  62. export function filterAsyncRoutes(routes, roles) {
  63. const res = []
  64. routes.forEach(route => {
  65. const tmp = { ...route }
  66. if (hasPermission(roles, tmp)) {
  67. if (tmp.children) {
  68. tmp.children = filterAsyncRoutes(tmp.children, roles)
  69. }
  70. res.push(tmp)
  71. }
  72. })
  73. return res
  74. }
  75. const state = {
  76. routes: [],
  77. addRoutes: [],
  78. data1: {
  79. name: 'getmenu',
  80. idname: 'id',
  81. parammaps: { id: -1 }
  82. }
  83. }
  84. const mutations = {
  85. SET_ROUTES: (state, routes) => {
  86. // console.log(state,routes)
  87. state.addRoutes = routes
  88. state.routes = constantRoutes.concat(routes)
  89. }
  90. }
  91. const actions = {
  92. generateRoutes({ commit }, roles) {
  93. return new Promise((resolve, reject) => {
  94. GetDataByName(state.data1).then(response => {
  95. const menus = transData(response.data.list, 'id', 'parentId', 'children')
  96. // const menus = response.data
  97. if (menus.length === 0) {
  98. reject('menus data is null')
  99. }
  100. const accessedRoutes = buildRouter(menus)
  101. // final add 404
  102. accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
  103. // commit to stores
  104. commit('SET_ROUTES', accessedRoutes)
  105. // return for add to router
  106. resolve(accessedRoutes)
  107. })
  108. })
  109. }
  110. }
  111. export default {
  112. namespaced: true,
  113. state,
  114. mutations,
  115. actions
  116. }