瀏覽代碼

perf[permission]: add the verification of roles

Pan 7 年之前
父節點
當前提交
0fea37b675
共有 2 個文件被更改,包括 9 次插入5 次删除
  1. 3 3
      src/permission.js
  2. 6 2
      src/store/modules/user.js

+ 3 - 3
src/permission.js

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

+ 6 - 2
src/store/modules/user.js

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