From 0fea37b6758c62d4e808bb1dbd0450a33e2de97a Mon Sep 17 00:00:00 2001 From: Pan Date: Wed, 2 May 2018 17:38:00 +0800 Subject: [PATCH] perf[permission]: add the verification of roles --- src/permission.js | 6 +++--- src/store/modules/user.js | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/permission.js b/src/permission.js index 8731d0d..ab64aa2 100644 --- a/src/permission.js +++ b/src/permission.js @@ -16,10 +16,10 @@ router.beforeEach((to, from, next) => { if (store.getters.roles.length === 0) { store.dispatch('GetInfo').then(res => { // 拉取用户信息 next() - }).catch(() => { + }).catch((err) => { store.dispatch('FedLogOut').then(() => { - Message.error('验证失败,请重新登录') - next({ path: '/login' }) + Message.error(err || 'Verification failed, please login again') + next({ path: '/' }) }) }) } else { diff --git a/src/store/modules/user.js b/src/store/modules/user.js index fa9c701..7d35e0e 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -44,8 +44,12 @@ const user = { GetInfo({ commit, state }) { return new Promise((resolve, reject) => { getInfo(state.token).then(response => { - const data = response.data - commit('SET_ROLES', data.roles) + const data = response + 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_AVATAR', data.avatar) resolve(response)