소스 검색

feat: add reset routes

double 5 년 전
부모
커밋
77aa9b8147
5개의 변경된 파일27개의 추가작업 그리고 35개의 파일을 삭제
  1. 0 11
      mock/role/routes.js
  2. 0 11
      src/router/index.js
  3. 22 0
      src/store/modules/permission.js
  4. 5 1
      src/views/menu/index.vue
  5. 0 12
      src/views/redirect/index.vue

+ 0 - 11
mock/role/routes.js

@@ -1,17 +1,6 @@
 // Just a mock data
 
 export const constantRoutes = [
-  {
-    path: '/redirect',
-    component: 'layout/Layout',
-    hidden: true,
-    children: [
-      {
-        path: '/redirect/:path*',
-        component: 'views/redirect/index'
-      }
-    ]
-  },
   {
     path: '/login',
     component: 'views/login/index',

+ 0 - 11
src/router/index.js

@@ -39,17 +39,6 @@ import nestedRouter from './modules/nested'
  * all roles can be accessed
  */
 export const constantRoutes = [
-  {
-    path: '/redirect',
-    component: Layout,
-    hidden: true,
-    children: [
-      {
-        path: '/redirect/:path(.*)',
-        component: () => import('@/views/redirect/index')
-      }
-    ]
-  },
   {
     path: '/login',
     component: () => import('@/views/login/index'),

+ 22 - 0
src/store/modules/permission.js

@@ -2,6 +2,8 @@ import { constantRoutes } from '@/router'
 import api from '@/api'
 import path from 'path'
 import Layout from '@/layout'
+import router, { resetRouter } from '@/router'
+
 /**
  * Use meta.role to determine if the current user has permission
  * @param roles
@@ -85,6 +87,26 @@ const actions = {
       // commit('SET_ROUTES', accessedRoutes)
       // resolve(accessedRoutes)
     })
+  },
+
+  // dynamically modify routes
+  resetRoutes({ commit, dispatch }) {
+    return new Promise(async resolve => {
+      const { roles } = await dispatch('user/getInfo', null, { root: true })
+
+      resetRouter()
+
+      // generate accessible routes map based on roles
+      const accessRoutes = await dispatch('generateRoutes', roles)
+
+      // dynamically add accessible routes
+      router.addRoutes(accessRoutes)
+
+      // reset visited views and cached views
+      dispatch('tagsView/delAllViews', null, { root: true })
+
+      resolve()
+    })
   }
 }
 

+ 5 - 1
src/views/menu/index.vue

@@ -238,6 +238,7 @@ export default {
               type: 'success'
             })
             this.$refs.qtable.getData()
+            this.$store.dispatch('permission/resetRoutes')
           })
         }
       })
@@ -253,6 +254,7 @@ export default {
               type: 'success'
             })
             this.$refs.qtable.getData()
+            this.$store.dispatch('permission/resetRoutes')
           })
         }
       })
@@ -273,6 +275,7 @@ export default {
             type: 'success'
           })
           this.$refs.qtable.getData()
+          this.$store.dispatch('permission/resetRoutes')
         })
       })
     },
@@ -285,11 +288,12 @@ export default {
         type: 'warning'
       }).then(() => {
         this.$api.sysMenu.batchDel(ids).then(res => {
-          this.$refs.qtable.getData()
           this.$message({
             message: 'successfully deleted',
             type: 'success'
           })
+          this.$refs.qtable.getData()
+          this.$store.dispatch('permission/resetRoutes')
         })
       })
     },

+ 0 - 12
src/views/redirect/index.vue

@@ -1,12 +0,0 @@
-<script>
-export default {
-  created() {
-    const { params, query } = this.$route
-    const { path } = params
-    this.$router.replace({ path: '/' + path, query })
-  },
-  render: function(h) {
-    return h() // avoid warning message
-  }
-}
-</script>