routes.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @description 路由拦截状态管理,目前两种模式:all模式与intelligence模式,其中partialRoutes是菜单暂未使用
  3. */
  4. import Vue from 'vue'
  5. import { asyncRoutes, constantRoutes, resetRouter } from '@/router'
  6. import menuApi from '@/api/menu'
  7. import { convertRouter, filterRoutes } from '@/utils/routes'
  8. import { authentication, rolesControl } from '@/config'
  9. import { isArray } from '@/utils/validate'
  10. const state = () => ({
  11. routes: [],
  12. activeName: '',
  13. })
  14. const getters = {
  15. routes: (state) => state.routes,
  16. activeName: (state) => state.activeName,
  17. }
  18. const mutations = {
  19. /**
  20. * @description 多模式设置路由
  21. * @param {*} state
  22. * @param {*} routes
  23. */
  24. setRoutes(state, routes) {
  25. state.routes = routes
  26. },
  27. /**
  28. * @description 修改Meta
  29. * @param {*} state
  30. * @param options
  31. */
  32. changeMenuMeta(state, options) {
  33. function handleRoutes(routes) {
  34. return routes.map((route) => {
  35. if (route.name === options.name) Object.assign(route.meta, options.meta)
  36. if (route.children && route.children.length)
  37. route.children = handleRoutes(route.children)
  38. return route
  39. })
  40. }
  41. state.routes = handleRoutes(state.routes)
  42. },
  43. /**
  44. * @description 修改 activeName
  45. * @param {*} state
  46. * @param activeName 当前激活菜单
  47. */
  48. changeActiveName(state, activeName) {
  49. state.activeName = activeName
  50. },
  51. }
  52. const actions = {
  53. /**
  54. * @description 多模式设置路由
  55. * @param {*} { commit }
  56. * @param mode
  57. * @returns
  58. */
  59. async setRoutes({ commit }, mode = 'none') {
  60. // 默认前端路由
  61. let routes = [...asyncRoutes]
  62. // 设置游客路由关闭路由拦截(不需要可以删除)
  63. const control = mode === 'visit' ? false : rolesControl
  64. // 设置后端路由(不需要可以删除)
  65. if (authentication === 'all') {
  66. const { data: data } = await menuApi.getTree()
  67. let list = data
  68. if (!isArray(list))
  69. Vue.prototype.$baseMessage(
  70. '路由格式返回有误!',
  71. 'error',
  72. 'vab-hey-message-error'
  73. )
  74. if (list[list.length - 1].path !== '*')
  75. list.push({ path: '*', redirect: '/404', meta: { hidden: true } })
  76. routes = convertRouter(list)
  77. }
  78. // 根据权限和rolesControl过滤路由
  79. const accessRoutes = filterRoutes([...constantRoutes, ...routes], control)
  80. // 设置菜单所需路由
  81. commit('setRoutes', JSON.parse(JSON.stringify(accessRoutes)))
  82. // 根据可访问路由重置Vue Router
  83. await resetRouter(accessRoutes)
  84. },
  85. /**
  86. * @description 修改Route Meta
  87. * @param {*} { commit }
  88. * @param options
  89. */
  90. changeMenuMeta({ commit }, options = {}) {
  91. commit('changeMenuMeta', options)
  92. },
  93. /**
  94. * @description 修改 activeName
  95. * @param {*} { commit }
  96. * @param activeName 当前激活菜单
  97. */
  98. changeActiveName({ commit }, activeName) {
  99. commit('changeActiveName', activeName)
  100. },
  101. }
  102. export default { state, getters, mutations, actions }