diff --git a/src/layout/components/Sidebar/index.vue b/src/layout/components/Sidebar/index.vue index fb014a2..da39034 100644 --- a/src/layout/components/Sidebar/index.vue +++ b/src/layout/components/Sidebar/index.vue @@ -12,7 +12,7 @@ :collapse-transition="false" mode="vertical" > - + @@ -28,9 +28,11 @@ export default { components: { SidebarItem, Logo }, computed: { ...mapGetters([ - 'permission_routes', 'sidebar' ]), + routes() { + return this.$router.options.routes + }, activeMenu() { const route = this.$route const { meta, path } = route diff --git a/src/permission.js b/src/permission.js index 3d08d6b..fa1ea19 100644 --- a/src/permission.js +++ b/src/permission.js @@ -26,25 +26,15 @@ router.beforeEach(async(to, from, next) => { next({ path: '/' }) NProgress.done() } else { - // determine whether the user has obtained his permission roles through getInfo - const hasRoles = store.getters.roles && store.getters.roles.length > 0 - if (hasRoles) { + const hasGetUserInfo = store.getters.name + if (hasGetUserInfo) { next() } else { try { // get user info - // note: roles must be a object array! such as: ['admin'] or ,['developer','editor'] - const { roles } = await store.dispatch('user/getInfo') + await store.dispatch('user/getInfo') - // generate accessible routes map based on roles - const accessRoutes = await store.dispatch('permission/generateRoutes', roles) - - // dynamically add accessible routes - router.addRoutes(accessRoutes) - - // hack method to ensure that addRoutes is complete - // set the replace: true, so the navigation will not leave a history record - next({ ...to, replace: true }) + next() } catch (error) { // remove token and go to login page to re-login await store.dispatch('user/resetToken') diff --git a/src/router/index.js b/src/router/index.js index b572aac..f091f0f 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -88,14 +88,8 @@ export const constantRoutes = [ meta: { title: 'Form', icon: 'form' } } ] - } -] + }, -/** - * asyncRoutes - * the routes that need to be dynamically loaded based on user roles - */ -export const asyncRoutes = [ { path: '/nested', component: Layout, diff --git a/src/store/getters.js b/src/store/getters.js index a108508..5ab7b4c 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -3,8 +3,6 @@ const getters = { device: state => state.app.device, token: state => state.user.token, avatar: state => state.user.avatar, - name: state => state.user.name, - roles: state => state.user.roles, - permission_routes: state => state.permission.routes + name: state => state.user.name } export default getters diff --git a/src/store/index.js b/src/store/index.js index 6ae5dad..6be466a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -2,7 +2,6 @@ import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' import app from './modules/app' -import permission from './modules/permission' import settings from './modules/settings' import user from './modules/user' @@ -11,7 +10,6 @@ Vue.use(Vuex) const store = new Vuex.Store({ modules: { app, - permission, settings, user }, diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js deleted file mode 100644 index aeb5ee5..0000000 --- a/src/store/modules/permission.js +++ /dev/null @@ -1,69 +0,0 @@ -import { asyncRoutes, constantRoutes } from '@/router' - -/** - * Use meta.role to determine if the current user has permission - * @param roles - * @param route - */ -function hasPermission(roles, route) { - if (route.meta && route.meta.roles) { - return roles.some(role => route.meta.roles.includes(role)) - } else { - return true - } -} - -/** - * Filter asynchronous routing tables by recursion - * @param routes asyncRoutes - * @param roles - */ -export function filterAsyncRoutes(routes, roles) { - const res = [] - - routes.forEach(route => { - const tmp = { ...route } - if (hasPermission(roles, tmp)) { - if (tmp.children) { - tmp.children = filterAsyncRoutes(tmp.children, roles) - } - res.push(tmp) - } - }) - - return res -} - -const state = { - routes: [], - addRoutes: [] -} - -const mutations = { - SET_ROUTES: (state, routes) => { - state.addRoutes = routes - state.routes = constantRoutes.concat(routes) - } -} - -const actions = { - generateRoutes({ commit }, roles) { - return new Promise(resolve => { - let accessedRoutes - if (roles.includes('admin')) { - accessedRoutes = asyncRoutes || [] - } else { - accessedRoutes = filterAsyncRoutes(asyncRoutes, roles) - } - commit('SET_ROUTES', accessedRoutes) - resolve(accessedRoutes) - }) - } -} - -export default { - namespaced: true, - state, - mutations, - actions -} diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 702f39a..cbb75a0 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -5,8 +5,7 @@ import { resetRouter } from '@/router' const state = { token: getToken(), name: '', - avatar: '', - roles: [] + avatar: '' } const mutations = { @@ -18,9 +17,6 @@ const mutations = { }, SET_AVATAR: (state, avatar) => { state.avatar = avatar - }, - SET_ROLES: (state, roles) => { - state.roles = roles } } @@ -50,14 +46,8 @@ const actions = { reject('Verification failed, please Login again.') } - const { roles, name, avatar } = data + const { name, avatar } = data - // roles must be a non-empty array - if (!roles || roles.length <= 0) { - reject('getInfo: roles must be a non-null array!') - } - - commit('SET_ROLES', roles) commit('SET_NAME', name) commit('SET_AVATAR', avatar) resolve(data) @@ -72,7 +62,6 @@ const actions = { return new Promise((resolve, reject) => { logout(state.token).then(() => { commit('SET_TOKEN', '') - commit('SET_ROLES', []) removeToken() resetRouter() resolve() @@ -86,7 +75,6 @@ const actions = { resetToken({ commit }) { return new Promise(resolve => { commit('SET_TOKEN', '') - commit('SET_ROLES', []) removeToken() resolve() }) diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index 77ae59a..33e5ab6 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -1,7 +1,6 @@ @@ -12,8 +11,7 @@ export default { name: 'Dashboard', computed: { ...mapGetters([ - 'name', - 'roles' + 'name' ]) } }