Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhigang.li@tendcloud.com committed Sep 27, 2018
1 parent bc32699 commit 222242d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
22 changes: 17 additions & 5 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const router = new Router({
})
const LOGIN_PAGE_NAME = 'login'

const turnTo = (to, access, next) => {
if (canTurnTo(to.name, access, routes)) next() // 有权限,可访问
else next({ replace: true, name: 'error_401' }) // 无权限,重定向到401页面
}

router.beforeEach((to, from, next) => {
iView.LoadingBar.start()
const token = getToken()
Expand All @@ -29,11 +34,18 @@ router.beforeEach((to, from, next) => {
name: 'home' // 跳转到home页
})
} else {
store.dispatch('getUserInfo').then(user => {
// 拉取用户信息,通过用户权限和跳转的页面的name来判断是否有权限访问;access必须是一个数组,如:['super_admin'] ['super_admin', 'admin']
if (canTurnTo(to.name, user.access, routes)) next() // 有权限,可访问
else next({ replace: true, name: 'error_401' }) // 无权限,重定向到401页面
})
if (store.state.user.hasGetInfo) {
turnTo(to, store.state.user.access, next)
} else {
store.dispatch('getUserInfo').then(user => {
// 拉取用户信息,通过用户权限和跳转的页面的name来判断是否有权限访问;access必须是一个数组,如:['super_admin'] ['super_admin', 'admin']
turnTo(to, user.access, next)
}).catch(() => {
next({
name: 'login'
})
})
}
}
})

Expand Down
31 changes: 20 additions & 11 deletions src/store/module/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default {
userId: '',
avatorImgPath: '',
token: getToken(),
access: ''
access: '',
hasGetInfo: false
},
mutations: {
setAvator (state, avatorPath) {
Expand All @@ -25,6 +26,9 @@ export default {
setToken (state, token) {
state.token = token
setToken(token)
},
setHasGetInfo (state, status) {
state.hasGetInfo = status
}
},
actions: {
Expand Down Expand Up @@ -63,16 +67,21 @@ export default {
// 获取用户相关信息
getUserInfo ({ state, commit }) {
return new Promise((resolve, reject) => {
getUserInfo(state.token).then(res => {
const data = res.data
commit('setAvator', data.avator)
commit('setUserName', data.user_name)
commit('setUserId', data.user_id)
commit('setAccess', data.access)
resolve(data)
}).catch(err => {
reject(err)
})
try {
getUserInfo(state.token).then(res => {
const data = res.data
commit('setAvator', data.avator)
commit('setUserName', data.user_name)
commit('setUserId', data.user_id)
commit('setAccess', data.access)
commit('setHasGetInfo', true)
resolve(data)
}).catch(err => {
reject(err)
})
} catch (error) {
reject(error)
}
})
}
}
Expand Down

0 comments on commit 222242d

Please sign in to comment.