routes.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  3. * @description 路由拦截状态管理,目前两种模式:all模式与intelligence模式,其中partialRoutes是菜单暂未使用
  4. */
  5. import { asyncRoutes, constantRoutes } from '@/router'
  6. import { getRouterList } from '@/api/router'
  7. import { convertRouter, filterAsyncRoutes } from '@/utils/handleRoutes'
  8. const state = { routes: [], partialRoutes: [] }
  9. const getters = {
  10. routes: (state) => state.routes,
  11. partialRoutes: (state) => state.partialRoutes,
  12. }
  13. const mutations = {
  14. setRoutes(state, routes) {
  15. state.routes = constantRoutes.concat(routes)
  16. },
  17. setAllRoutes(state, routes) {
  18. state.routes = constantRoutes.concat(routes)
  19. },
  20. setPartialRoutes(state, routes) {
  21. state.partialRoutes = constantRoutes.concat(routes)
  22. },
  23. }
  24. const actions = {
  25. async setRoutes({ commit }, permissions) {
  26. //开源版只过滤动态路由permissions,admin不再默认拥有全部权限
  27. const finallyAsyncRoutes = await filterAsyncRoutes(
  28. [...asyncRoutes],
  29. permissions
  30. )
  31. commit('setRoutes', finallyAsyncRoutes)
  32. return finallyAsyncRoutes
  33. },
  34. async setAllRoutes({ commit }) {
  35. let { data } = await getRouterList()
  36. data.push({ path: '*', redirect: '/404', hidden: true })
  37. let accessRoutes = convertRouter(data)
  38. commit('setAllRoutes', accessRoutes)
  39. return accessRoutes
  40. },
  41. setPartialRoutes({ commit }, accessRoutes) {
  42. commit('setPartialRoutes', accessRoutes)
  43. return accessRoutes
  44. },
  45. }
  46. export default { state, getters, mutations, actions }