Commit 1ddfc31c3c4c792c5f741f6d0f0754ffc9a6613c
1 parent
d27633fb
fix: `getUserinfo` is compatible with empty roles data
修复getUserinfo接口数据未携带roles字段时登录失败的问题
Showing
1 changed file
with
9 additions
and
3 deletions
src/store/modules/user.ts
... | ... | @@ -14,6 +14,7 @@ import { router } from '/@/router'; |
14 | 14 | import { usePermissionStore } from '/@/store/modules/permission'; |
15 | 15 | import { RouteRecordRaw } from 'vue-router'; |
16 | 16 | import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic'; |
17 | +import { isArray } from '/@/utils/is'; | |
17 | 18 | |
18 | 19 | interface UserState { |
19 | 20 | userInfo: Nullable<UserInfo>; |
... | ... | @@ -118,10 +119,15 @@ export const useUserStore = defineStore({ |
118 | 119 | }, |
119 | 120 | async getUserInfoAction(): Promise<UserInfo> { |
120 | 121 | const userInfo = await getUserInfo(); |
121 | - const { roles } = userInfo; | |
122 | - const roleList = roles.map((item) => item.value) as RoleEnum[]; | |
122 | + const { roles = [] } = userInfo; | |
123 | + if (isArray(roles)) { | |
124 | + const roleList = roles.map((item) => item.value) as RoleEnum[]; | |
125 | + this.setRoleList(roleList); | |
126 | + } else { | |
127 | + userInfo.roles = []; | |
128 | + this.setRoleList([]); | |
129 | + } | |
123 | 130 | this.setUserInfo(userInfo); |
124 | - this.setRoleList(roleList); | |
125 | 131 | return userInfo; |
126 | 132 | }, |
127 | 133 | /** | ... | ... |