You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import { asyncRouterMap, constantRouterMap } from '@/router'
  2. /**
  3. * 通过meta.role判断是否与当前用户权限匹配
  4. * 通过 route.path与menu.rpath比对看是否有对应的权限
  5. * @param roles 我拥有的角色
  6. * @param menus 我拥有的菜单
  7. * @param route
  8. */
  9. function hasPermission( roles,menus ,route) {
  10. if(route==null || route=='undefined'){
  11. return false;
  12. }
  13. if (route.meta && route.meta.roles) {
  14. return roles.some(role => route.meta.roles.indexOf(role.roleid) >= 0)
  15. } else {
  16. if(route.children && route.children.length){
  17. return true;
  18. }else{
  19. if(!route.meta||!route.meta.menu){
  20. return true
  21. }
  22. if( route.meta && route.meta.menu && menus && menus.length ){
  23. return menus.some(menu => menu.rpath==null?false:menu.rpath.indexOf(route.fullPath) >= 0);
  24. }else if( !route.meta || !route.meta.menu ){
  25. return true;
  26. }else{
  27. return false
  28. }
  29. }
  30. return false
  31. }
  32. }
  33. function findRouteInner(routers,fullPath){
  34. return findRouteByFullPath({children:routers},fullPath)
  35. }
  36. function findRouteByFullPath(router,fullPath){
  37. if(router==null){
  38. return null;
  39. }else{
  40. if(router.children && router.children.length>0){
  41. var routerFind=null;
  42. router.children.forEach(i=>{
  43. var r= findRouteByFullPath(i,fullPath)
  44. if(r){
  45. routerFind=r;
  46. return routerFind;
  47. }
  48. })
  49. return routerFind;
  50. }else{
  51. if(router.fullPath==fullPath){
  52. return router;
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * 递归过滤异步路由表返回符合用户角色权限的路由表
  59. * @param asyncRouterMap
  60. * @param roles
  61. */
  62. function filterAsyncRouter(asyncRouterMap, roles,menus) {
  63. const accessedRouters = asyncRouterMap.filter(route => {
  64. if(!route.fullPath){
  65. route.fullPath=route.path;
  66. }else{
  67. route.fullPath=route.fullPath+"/"+route.path
  68. }
  69. route.fullPath=route.fullPath.replace("//","/")
  70. if (hasPermission(roles,menus, route)) {
  71. if (route.children && route.children.length) {
  72. route.children = filterAsyncRouterWithParentRoute(route, roles,menus )
  73. if(route.children==null || route.children.length==0){
  74. return false
  75. }
  76. }
  77. return true
  78. }
  79. return false
  80. })
  81. return accessedRouters
  82. }
  83. function filterAsyncRouterWithParentRoute(proute, roles,menus) {
  84. var accessedRouters = proute.children.filter(route => {
  85. if(!route.fullPath){
  86. route.fullPath=proute.fullPath+"/"+route.path;
  87. route.fullPath=route.fullPath.replace("//","/")
  88. }
  89. if (hasPermission(roles,menus, route)) {
  90. if (route.children && route.children.length) {
  91. route.children = filterAsyncRouterWithParentRoute(route, roles,menus )
  92. if(route.children==null || route.children.length==0){
  93. return false
  94. }
  95. }
  96. return true
  97. }
  98. return false
  99. })
  100. return accessedRouters
  101. }
  102. function initRouter(proute) {
  103. if(proute==null){
  104. return;
  105. }else{
  106. if(!proute.fullPath){
  107. if(proute.path){
  108. proute.fullPath=proute.path
  109. }else{
  110. proute.fullPath=""
  111. }
  112. }
  113. if(proute.children && proute.children.length>0){
  114. proute.children.forEach(i=>{
  115. if(!i.fullPath){
  116. i.fullPath=proute.fullPath+"/"+i.path
  117. i.fullPath=i.fullPath.replace("//","/")
  118. }
  119. initRouter(i)
  120. })
  121. }
  122. }
  123. }
  124. const permission = {
  125. state: {
  126. routers: constantRouterMap,
  127. addRouters: [],
  128. added:false //是否已经计算完毕动态路由false没有,true已计算完毕
  129. },
  130. mutations: {
  131. SET_ROUTERS: (state, routers) => {
  132. state.addRouters = routers
  133. state.routers = constantRouterMap.concat(routers)
  134. },
  135. SET_ADDED: (state, added) => {
  136. state.added = added
  137. }
  138. },
  139. actions: {
  140. GenerateRoutes({ commit }, {roles,menus}) {
  141. return new Promise(resolve => {
  142. debugger
  143. initRouter({children:asyncRouterMap})
  144. let accessedRouters
  145. if (roles.some(role => role.roleid==='superAdmin'||role.roleid==='platformAdmin')) {
  146. accessedRouters = asyncRouterMap
  147. } else {
  148. accessedRouters = filterAsyncRouter(asyncRouterMap, roles,menus)
  149. }
  150. commit('SET_ROUTERS', accessedRouters)
  151. commit('SET_ADDED', true)
  152. resolve()
  153. })
  154. },
  155. FindRouter({ commit ,state}, fullPath){
  156. return new Promise(resolve => {
  157. resolve(findRouteInner(state.routers,fullPath))
  158. })
  159. }
  160. },
  161. }
  162. export default permission