perf[permission]: add the verification of roles

This commit is contained in:
Pan 2018-05-02 17:38:00 +08:00 committed by 花裤衩
parent c3ee00472d
commit 0fea37b675
2 changed files with 9 additions and 5 deletions

View File

@ -16,10 +16,10 @@ router.beforeEach((to, from, next) => {
if (store.getters.roles.length === 0) { if (store.getters.roles.length === 0) {
store.dispatch('GetInfo').then(res => { // 拉取用户信息 store.dispatch('GetInfo').then(res => { // 拉取用户信息
next() next()
}).catch(() => { }).catch((err) => {
store.dispatch('FedLogOut').then(() => { store.dispatch('FedLogOut').then(() => {
Message.error('验证失败,请重新登录') Message.error(err || 'Verification failed, please login again')
next({ path: '/login' }) next({ path: '/' })
}) })
}) })
} else { } else {

View File

@ -44,8 +44,12 @@ const user = {
GetInfo({ commit, state }) { GetInfo({ commit, state }) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getInfo(state.token).then(response => { getInfo(state.token).then(response => {
const data = response.data const data = response
commit('SET_ROLES', data.roles) if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
commit('SET_ROLES', data.roles)
} else {
reject('getInfo: roles must be a non-null array !')
}
commit('SET_NAME', data.name) commit('SET_NAME', data.name)
commit('SET_AVATAR', data.avatar) commit('SET_AVATAR', data.avatar)
resolve(response) resolve(response)