permission.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { asyncRoutes, constantRoutes } from '@/router'
  2. import { getSilder } from '@/api/silder'
  3. // import { stat } from 'fs'
  4. // import { removeToken } from '@/utils/auth'
  5. // import Layout from '@/layout'
  6. /**
  7. * 遍历接口菜单添加页面
  8. * @param asyncRoutes
  9. * @param getMenu
  10. */
  11. function generateAsyncRouter (asyncRoutes, data) {
  12. if (!data) {
  13. return []
  14. }
  15. // console.log(data)
  16. data.forEach((item) => {
  17. item.component = asyncRoutes[item.component]
  18. if (item.children && item.children.length > 0) {
  19. generateAsyncRouter(asyncRoutes, item.children)
  20. }
  21. })
  22. return data
  23. }
  24. /**
  25. * 判断平台端添加首页
  26. * @param type
  27. */
  28. // const type = getters.type
  29. const state = {
  30. routes: [],
  31. addRoutes: [],
  32. type: '', // 登录的平台类型
  33. permission: [] // 权限
  34. }
  35. const mutations = {
  36. SET_ROUTES: (state, routes) => {
  37. state.addRoutes = routes
  38. state.routes = constantRoutes.concat(routes)
  39. },
  40. SET_PERMISSION: (state, permission) => {
  41. state.permission = permission
  42. }
  43. }
  44. // 路由
  45. // 递归遍历数组
  46. function recursionRouter (arr) {
  47. if (arr.length > 0) {
  48. let newArr = [];
  49. for (let i = 0; i < arr.length; i++) {
  50. if (arr[i].type == 1) {
  51. continue
  52. }
  53. let obj = {};
  54. obj.component = arr[i].component;
  55. obj.name = arr[i].component;
  56. // if (item.type != '1' && item.component) {
  57. // if (!item.path.startsWith('/') && item.component != 'Layout') {
  58. // obj.names = item.name
  59. // }
  60. // }
  61. arr[i].hid == 0 ? obj.hidden = false : obj.hidden = true
  62. // console.log('高亮标签'+arr[i].parentPermission,'普通路径'+arr[i].path)
  63. obj.path = arr[i].path;
  64. obj.meta = { 'title': arr[i].name, 'icon': arr[i].icon, 'noCache': arr[i].keepAlive, 'activeMenu': arr[i].parentPermission }
  65. if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
  66. obj.children = recursionRouter(arr[i].sysMenus);
  67. }
  68. newArr.push(obj)
  69. }
  70. return newArr
  71. }
  72. }
  73. // 权限
  74. // 递归遍历数组
  75. let tempArr = []
  76. function recursionPermission (arr) {
  77. arr.map(item => {
  78. tempArr.push(item.memo)
  79. if (item.sysMenus && item.sysMenus.length > 0) {
  80. recursionPermission(item.sysMenus)
  81. }
  82. })
  83. }
  84. const actions = {
  85. generateRoutes ({ commit }) {
  86. return new Promise(resolve => {
  87. // 获取接口返回的权限菜单
  88. getSilder().then(res => {
  89. if (res.code == 200) {
  90. let newData = recursionRouter(res.data);
  91. recursionPermission(res.data)
  92. let accessedRoutes
  93. // 生成异步路由表
  94. accessedRoutes = generateAsyncRouter(asyncRoutes, newData)
  95. // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true })
  96. commit('SET_ROUTES', accessedRoutes)
  97. // commit('SET_PERMISSION', recursionPermission(res.data).flat(Infinity))
  98. window.localStorage.removeItem('permission')
  99. window.localStorage.setItem('permission', tempArr)
  100. resolve(accessedRoutes)
  101. this.dispatch('app/setDotStatus')
  102. }
  103. })
  104. })
  105. },
  106. removePermission ({ commit }) {
  107. window.localStorage.removeItem('permission')
  108. commit('SET_PERMISSION', [])
  109. }
  110. }
  111. export default {
  112. namespaced: true,
  113. state,
  114. mutations,
  115. actions
  116. }