xt_permission.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { xt_constantRouterMap, xt_asyncRouterMap } from '@/router'
  2. const xt_permission = {
  3. state: {
  4. routers: xt_constantRouterMap,
  5. addRouters: []
  6. },
  7. mutations: {
  8. XT_SET_ROUTERS: (state, payload) => {
  9. state.routers = payload.concatRouters
  10. state.addRouters = payload.addRouters
  11. }
  12. },
  13. actions: {
  14. xt_GenerateRoutes({ commit }, urls) {
  15. return new Promise(resolve => {
  16. setupRouters(urls)
  17. var addRouters = xt_filterAsyncRouter(xt_asyncRouterMap)
  18. commit('XT_SET_ROUTERS', { concatRouters:xt_constantRouterMap.concat(xt_asyncRouterMap), addRouters:addRouters })
  19. resolve()
  20. })
  21. }
  22. }
  23. }
  24. function xt_filterAsyncRouter(routers) {
  25. var addRouters = routers.filter(route => {
  26. if (route.is_menu == false) {
  27. return true
  28. }
  29. if (route.hidden == false) {
  30. if (route.children && route.children.length) {
  31. route.children = xt_filterAsyncRouter(route.children)
  32. }
  33. return true
  34. }
  35. return false
  36. })
  37. return addRouters
  38. }
  39. function setupRouters(urls) {
  40. console.log(urls)
  41. console.log(xt_asyncRouterMap)
  42. xt_asyncRouterMap.forEach(router => {
  43. console.log(router)
  44. if (Object.prototype.toString.call(router.children) === '[object Array]') {
  45. router.hidden = true
  46. router.children.forEach(c_router => {
  47. if(router.path == "/slow"){
  48. if(IndexOf(urls, router.path) != -1){
  49. router.hidden = false
  50. }else{
  51. router.hidden = true
  52. }
  53. }
  54. if(router.path == "/scrm"){
  55. if(IndexOf(urls, router.path) != -1){
  56. router.hidden = false
  57. }else{
  58. router.hidden = true
  59. }
  60. }
  61. if(router.path == "/shop"){
  62. if(IndexOf(urls, router.path) != -1){
  63. router.hidden = false
  64. }else{
  65. router.hidden = true
  66. }
  67. }
  68. // console.log(c_router.path)
  69. if (IndexOf(urls, c_router.path) !== -1) {
  70. // if (urls.indexOf(c_router.path) !== -1) {
  71. // console.log('router.children show : ' + c_router.path)
  72. router.hidden = false
  73. c_router.hidden = false
  74. } else {
  75. // console.log('router.children hide : ' + c_router.path)
  76. c_router.hidden = true
  77. }
  78. })
  79. } else {
  80. router.hidden = true
  81. if (IndexOf(urls, router.path) !== -1) {
  82. // console.log('router show : ' + router.path)
  83. // if (urls.indexOf(router.path) !== -1) {
  84. router.hidden = false
  85. }
  86. }
  87. })
  88. }
  89. function IndexOf(array, obj) {
  90. for (var i = 0; i < array.length; i++) {
  91. if (array[i] === obj) {
  92. return i
  93. }
  94. }
  95. return -1
  96. }
  97. export default xt_permission