Skip to content

Commit

Permalink
feat: basic logic for routes filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lbwa committed Oct 7, 2018
1 parent 6afe692 commit f6ca100
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 34 deletions.
3 changes: 2 additions & 1 deletion config/webpack.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module.exports = {
'MOCK': path.resolve(PATH.ROOT_PATH, './mock'),
'SERVICES': path.resolve(PATH.SOURCE_PATH, './services'),
'AUTH': path.resolve(PATH.SOURCE_PATH, './auth'),
'STORE': path.resolve(PATH.SOURCE_PATH, './store')
'STORE': path.resolve(PATH.SOURCE_PATH, './store'),
'ROUTER': path.resolve(PATH.SOURCE_PATH, './router')
}
},
module: {
Expand Down
11 changes: 10 additions & 1 deletion src/router/components/components.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Should be a path based on 'VIEW/'
export default [
const common = [
'Login',
'Dashboard/Analysis',
'Dashboard/Workspace',
Expand All @@ -10,3 +10,12 @@ export default [
'Form/Step/Success',
'Form/Advanced'
]

const dynamic = [
'Permission'
]

export {
common,
dynamic
}
6 changes: 4 additions & 2 deletions src/router/components/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import createImporter from './importer'
import components from './components'
import * as components from './components'

export default createImporter(components)
export const common = createImporter(components.common)

export const dynamic = createImporter(components.dynamic)
15 changes: 11 additions & 4 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ import VueRouter from 'vue-router'
import store from 'STORE'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import routes from './routes'
import { commonRoutes } from './routes'
import { getTokenFromLocal } from 'AUTH'

Vue.use(VueRouter)

const router = new VueRouter({
// mode: 'history',
routes
routes: commonRoutes
})

router.beforeEach((to, from, next) => {
nprogress.start()
if (getTokenFromLocal()) { // get token from sessionStorage

// get token from sessionStorage
if (getTokenFromLocal()) {
if (!store.getters['login/role'].length && to.path !== '/') {
store.dispatch('login/fetchUserInfo')
.then(({ role }) => {
store.dispatch('login/createRoutes', role)
// Preset dynamic routes is used to create new global routes map,
// filtered by `role` variable.
if (!Array.isArray(role)) {
throw new TypeError(`${JSON.stringify(role)} should be a array`)
}
store.dispatch('login/createExtraRoutes', { role })
})
}
}
Expand Down
22 changes: 10 additions & 12 deletions src/router/routes.js → src/router/routes/common.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
// import { createRoutes } from './utils'
// import paths from './components/components'
import components from './components'
import { common } from '../components'

export default [
{
path: '/',
component: components.login
component: common.login
},
{
path: '/dashboard/analysis',
component: components.dashboardAnalysis
component: common.dashboardAnalysis
},
{
path: '/dashboard/workspace',
component: components.dashboardWorkspace
component: common.dashboardWorkspace
},
{
path: '/form/basic',
component: components.formBasic
component: common.formBasic
},
{
path: '/form/step',
component: components.formStepIndex,
component: common.formStepIndex,
redirect: '/form/step/info',
children: [
{
path: 'info',
component: components.formStepInfo
component: common.formStepInfo
},
{
path: 'confirm',
component: components.formStepConfirm,
component: common.formStepConfirm,
beforeEnter (to, from, next) {
from.path !== '/form/step/info' ? next('/form/step/info') : next()
}
},
{
path: 'success',
component: components.formStepSuccess,
component: common.formStepSuccess,
beforeEnter (to, from, next) {
from.path !== '/form/step/confirm' ? next('/form/step/info') : next()
}
Expand All @@ -46,6 +44,6 @@ export default [
},
{
path: '/form/advanced',
component: components.formAdvanced
component: common.formAdvanced
}
]
18 changes: 18 additions & 0 deletions src/router/routes/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { dynamic } from '../components'

export default [
{
path: '/access/admin',
component: dynamic.permission,
meta: {
role: ['admin', 'user']
}
},
{
path: '/access/user',
component: dynamic.permission,
meta: {
role: ['user']
}
}
]
7 changes: 7 additions & 0 deletions src/router/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import commonRoutes from './common'
import dynamicRoutes from './dynamic'

export {
commonRoutes,
dynamicRoutes
}
20 changes: 10 additions & 10 deletions src/router/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export function createChunkName (path) {
return normalizePath.join('')
}

export function createRoutes (paths, components, routes = []) {
paths.forEach(path => {
const chunkName = createChunkName(path)
routes.push({
path: path.toLowerCase(),
component: components[chunkName]
})
})
return routes
}
// export function createRoutes (paths, components, routes = []) {
// paths.forEach(path => {
// const chunkName = createChunkName(path)
// routes.push({
// path: path.toLowerCase(),
// component: components[chunkName]
// })
// })
// return routes
// }
30 changes: 28 additions & 2 deletions src/store/modules/login/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pushLogin, fetchUserInfo } from 'SERVICES'
import { setTokenToLocal } from 'AUTH'
import { dynamicRoutes } from 'ROUTER/routes'
import types from './mutations/types'
import { Notification } from 'element-ui'

Expand Down Expand Up @@ -41,7 +42,32 @@ export default {
})
.catch(console.error)
},
createRoutes ({ commit }, role) {
console.log(role)
createExtraRoutes ({ commit }, { role }) {
let globalRoutes = role.includes('admin')
? dynamicRoutes
: filterRoutes(dynamicRoutes, role)

commit(types.SET_ROUTES, globalRoutes)
}
}

function filterRoutes (routes, role) {
const formatRoutes = []
routes.forEach(route => {
const routeCopy = { ...route } // Prevent edit original routes map
if (hasAccess(route, role)) {
if (routeCopy.children) {
routeCopy.children = filterRoutes(routeCopy, role)
}
formatRoutes.push(routeCopy)
}
})

return formatRoutes
}

function hasAccess (route, role) {
return route.meta && route.meta.role
? role.some(key => route.meta.role.includes(key))
: true // common routes in dynamic routes map
}
3 changes: 3 additions & 0 deletions src/store/modules/login/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ export default {
},
role (state) {
return state.role
},
routes (state) {
return state.routes
}
}
7 changes: 7 additions & 0 deletions src/store/modules/login/mutations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ export default {
},
[types.SET_TOKEN] (state, token) {
state.token = token
},
[types.SET_ROUTES] (state, routes) {
// store it for passing it into router.addRoutes()
state.addRoutes = routes

// store current global routes map, filtered by state.role
state.routes = [...state.routes, ...routes]
}
}
3 changes: 2 additions & 1 deletion src/store/modules/login/mutations/types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default {
SET_USERNAME: 'SET_USERNAME',
SET_ROLE: 'SET_ROLE',
SET_TOKEN: 'SET_TOKEN'
SET_TOKEN: 'SET_TOKEN',
SET_ROUTES: 'SET_ROUTES'
}
6 changes: 5 additions & 1 deletion src/store/modules/login/state.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { commonRoutes } from 'ROUTER/routes'

export default {
username: '',
role: [],
token: ''
token: '',
routes: commonRoutes, // store current global routes map
addRoutes: [] // for router.addRoutes() function
}
19 changes: 19 additions & 0 deletions src/view/Permission.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<el-main class="permission">
{{info}}
</el-main>
</template>

<script>
export default {
data () {
return {
info: 'dynamic routes'
}
}
}
</script>

<style lang="sass" scoped>
</style>

0 comments on commit f6ca100

Please sign in to comment.