permission.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { asyncRoutes, constantRoutes } from '@/router'
  2. import { getSilder } from '@/api/silder'
  3. import { removeToken } from '@/utils/auth'
  4. import Layout from '@/layout'
  5. /**
  6. * 遍历接口菜单添加页面
  7. * @param asyncRoutes
  8. * @param getMenu
  9. */
  10. function generateAsyncRouter (asyncRoutes, data) {
  11. if (!data) {
  12. return []
  13. }
  14. data.forEach((item, index) => {
  15. item.component = asyncRoutes[item.component]
  16. if (item.children && item.children.length > 0) {
  17. generateAsyncRouter(asyncRoutes, item.children)
  18. }
  19. })
  20. return data
  21. }
  22. /**
  23. * 判断平台端添加首页
  24. * @param type
  25. */
  26. // const type = getters.type
  27. const state = {
  28. routes: [],
  29. addRoutes: [],
  30. type: '' // 登录的平台类型
  31. }
  32. const mutations = {
  33. SET_ROUTES: (state, routes) => {
  34. state.addRoutes = routes
  35. state.routes = constantRoutes.concat(routes)
  36. }
  37. }
  38. // 递归遍历数组
  39. function recursionRouter (arr) {
  40. if (arr.length > 0) {
  41. let newArr = arr.map(item => {
  42. let obj = {};
  43. obj.component = item.component;
  44. item.hid == 0 ? obj.hidden = false : obj.hidden = true
  45. obj.path = item.path;
  46. obj.meta = { 'title': item.name, 'icon': item.icon }
  47. if (item.sysMenus && item.sysMenus.length > 0) {
  48. obj.children = recursionRouter(item.sysMenus);
  49. }
  50. return obj
  51. })
  52. return newArr
  53. }
  54. }
  55. const actions = {
  56. generateRoutes ({ commit }, roles) {
  57. return new Promise(resolve => {
  58. // 获取接口返回的权限菜单
  59. getSilder().then(res => {
  60. if (res.code == 200) {
  61. let newData = recursionRouter(res.data);
  62. var accessedRoutes
  63. // 生成异步路由表
  64. accessedRoutes = generateAsyncRouter(asyncRoutes, newData)
  65. // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true })
  66. commit('SET_ROUTES', accessedRoutes)
  67. resolve(accessedRoutes)
  68. }
  69. })
  70. })
  71. }
  72. }
  73. export default {
  74. namespaced: true,
  75. state,
  76. mutations,
  77. actions
  78. }