3dde8d1cbd0c2a513938472bf5027457d6283163.svn-base 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. if(response.data.list !== null){
  96. for(let i=0;i<response.data.list.length;i++){
  97. response.data.list[i].affix = true
  98. response.data.list[i].keepAlive = true
  99. if(response.data.list[i].children !== undefined){
  100. for(let j=0;j<response.data.list[i].children.length;j++){
  101. response.data.list[i].children[j].affix = true
  102. response.data.list[i].children[j].keepAlive = true
  103. }
  104. }
  105. }
  106. console.log(response.data.list,'==========9999')
  107. const menus = transData(response.data.list, 'id', 'parentId', 'children')
  108. // const menus = response.data
  109. if (menus.length === 0) {
  110. reject('menus data is null1')
  111. }
  112. const accessedRoutes = buildRouter(menus)
  113. // final add 404
  114. accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
  115. // commit to stores
  116. commit('SET_ROUTES', accessedRoutes)
  117. // return for add to router
  118. resolve(accessedRoutes)
  119. } else {
  120. const menus = []
  121. // const menus = response.data
  122. if (menus.length == 0) {
  123. reject('当前用户角色未分配页面权限,无法登录')
  124. }
  125. const accessedRoutes = buildRouter(menus)
  126. // final add 404
  127. accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
  128. // commit to stores
  129. commit('SET_ROUTES', accessedRoutes)
  130. // return for add to router
  131. resolve(accessedRoutes)
  132. }
  133. })
  134. })
  135. }
  136. }
  137. export default {
  138. namespaced: true,
  139. state,
  140. mutations,
  141. actions
  142. }