0c70463e5b7f810601df0c18d8d73c72ea668a7e.svn-base 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 } 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. oRouter.meta.icon = item.icon
  31. oRouter.meta.title = item.title
  32. // oRouter.meta.noCache = item.noCache || false
  33. oRouter.meta.noCache = true
  34. // oRouter.meta.breadcrumb = item.breadcrumb || true
  35. oRouter.children = validatenull(item.children) ? [] : buildRouter(item.children)
  36. aRouter.push(oRouter)
  37. }
  38. }
  39. })
  40. return aRouter
  41. }
  42. /**
  43. * Use meta.role to determine if the current user has permission
  44. * @param roles
  45. * @param route
  46. */
  47. function hasPermission(roles, route) {
  48. if (route.meta && route.meta.roles) {
  49. return roles.some(role => route.meta.roles.includes(role))
  50. } else {
  51. return true
  52. }
  53. }
  54. /**
  55. * Filter asynchronous routing tables by recursion
  56. * @param routes asyncRoutes
  57. * @param roles
  58. */
  59. export function filterAsyncRoutes(routes, roles) {
  60. const res = []
  61. routes.forEach(route => {
  62. const tmp = { ...route }
  63. if (hasPermission(roles, tmp)) {
  64. if (tmp.children) {
  65. tmp.children = filterAsyncRoutes(tmp.children, roles)
  66. }
  67. res.push(tmp)
  68. }
  69. })
  70. return res
  71. }
  72. const state = {
  73. routes: [],
  74. addRoutes: [],
  75. data1: { name: 'getmenu',
  76. idname: 'id',
  77. parammaps: { id: -1 }
  78. }
  79. }
  80. const mutations = {
  81. SET_ROUTES: (state, routes) => {
  82. state.addRoutes = routes
  83. state.routes = constantRoutes.concat(routes)
  84. }
  85. }
  86. const actions = {
  87. generateRoutes({ commit }, roles) {
  88. return new Promise((resolve, reject) => {
  89. getRecuData(state.data1).then(response => {
  90. const menus = response.data
  91. if (menus.length === 0) {
  92. reject('menus data is null')
  93. }
  94. const accessedRoutes = buildRouter(menus)
  95. // final add 404
  96. accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
  97. // commit to stores
  98. commit('SET_ROUTES', accessedRoutes)
  99. // return for add to router
  100. resolve(accessedRoutes)
  101. })
  102. })
  103. }
  104. }
  105. export default {
  106. namespaced: true,
  107. state,
  108. mutations,
  109. actions
  110. }