permission.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { asyncRoutes, constantRoutes } from '@/router'
  2. import { getModule } from '@/api/goods'
  3. /**
  4. * Use meta.role to determine if the current user has permission
  5. * @param roles
  6. * @param route
  7. */
  8. function hasPermission(roles, route) {
  9. if (route.meta && route.meta.roles) {
  10. return roles.some(role => route.meta.roles.includes(role))
  11. } else {
  12. return true
  13. }
  14. }
  15. /**
  16. * Filter asynchronous routing tables by recursion
  17. * @param routes asyncRoutes
  18. * @param roles
  19. */
  20. export function filterAsyncRoutes(routes, roles) {
  21. const res = []
  22. routes.forEach(route => {
  23. const tmp = { ...route }
  24. if (hasPermission(roles, tmp)) {
  25. if (tmp.children) {
  26. tmp.children = filterAsyncRoutes(tmp.children, roles)
  27. }
  28. res.push(tmp)
  29. }
  30. })
  31. return res
  32. }
  33. const state = {
  34. routes: [],
  35. addRoutes: []
  36. }
  37. const mutations = {
  38. SET_ROUTES: (state, routes) => {
  39. state.addRoutes = routes
  40. state.routes = constantRoutes.concat(routes)
  41. }
  42. }
  43. const actions = {
  44. generateRoutes({ commit }, userInfo) {
  45. return new Promise(resolve => {
  46. let accessedRoutes
  47. if (userInfo.roles.includes('admin')) {
  48. accessedRoutes = asyncRoutes || []
  49. } else {
  50. accessedRoutes = filterAsyncRoutes(asyncRoutes, userInfo.roles)
  51. }
  52. getModule({ organizeID: userInfo.organizeID }).then(response => {
  53. const modules = response.data[0]
  54. accessedRoutes.forEach(routeItem => {
  55. if (routeItem.name === 'Goods') {
  56. if (modules && modules.firstModulesName) {
  57. accessedRoutes[0].children.unshift({
  58. path: '/goods/list/preferred',
  59. name: 'PreferredProduct',
  60. component: () => import('@/views/goods/list-preferred'),
  61. meta: { title: modules.firstModulesName, noCache: true, activeMenu: '/goods/list' },
  62. hidden: true
  63. })
  64. }
  65. if (modules && modules.secondModulesName) {
  66. accessedRoutes[0].children.unshift({
  67. path: '/goods/list/preferential',
  68. name: 'PreferentialProduct',
  69. component: () => import('@/views/goods/list-preferential'),
  70. meta: { title: modules.secondModulesName, noCache: true, activeMenu: '/goods/list' },
  71. hidden: true
  72. })
  73. }
  74. if (modules && modules.thirdModulesName) {
  75. accessedRoutes[0].children.unshift({
  76. path: '/goods/list/commonly',
  77. name: 'CommonlyProduct',
  78. component: () => import('@/views/goods/list-commonly'),
  79. meta: { title: modules.thirdModulesName, noCache: true, activeMenu: '/goods/list' },
  80. hidden: true
  81. })
  82. }
  83. }
  84. })
  85. commit('SET_ROUTES', accessedRoutes)
  86. resolve(accessedRoutes)
  87. }).catch(() => {
  88. commit('SET_ROUTES', accessedRoutes)
  89. resolve(accessedRoutes)
  90. })
  91. })
  92. }
  93. }
  94. export default {
  95. namespaced: true,
  96. state,
  97. mutations,
  98. actions
  99. }