0388873ea07f9eacfcf5b6095c5131973cb0bc14.svn-base 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // getRecuData(state.data1).then(response => {
  96. console.log(response.data,'============')
  97. const menus = transData(response.data.list, 'id', 'parentId', 'children')
  98. console.log(menus)
  99. // const menus = response.data
  100. if (menus.length === 0) {
  101. reject('menus data is null')
  102. }
  103. const accessedRoutes = buildRouter(menus)
  104. // final add 404
  105. accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
  106. // commit to stores
  107. commit('SET_ROUTES', accessedRoutes)
  108. // return for add to router
  109. resolve(accessedRoutes)
  110. })
  111. })
  112. }
  113. }
  114. export default {
  115. namespaced: true,
  116. state,
  117. mutations,
  118. actions
  119. }