Skip to content

Commit bf87c46

Browse files
YunaiVgitee-org
authored andcommitted
!574 【修复】开发模式下从“同时使用了用户信息和权限判断”的页面退出登录时异常问题
Merge pull request !574 from 半栈幼儿员/hotfix/user
2 parents 458242a + 9b290ae commit bf87c46

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

src/directives/permission/hasPermi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export function hasPermi(app: App<Element>) {
88
const { wsCache } = useCache()
99
const { value } = binding
1010
const all_permission = '*:*:*'
11-
const permissions = wsCache.get(CACHE_KEY.USER).permissions
11+
const userInfo = wsCache.get(CACHE_KEY.USER)
12+
const permissions = userInfo?.permissions || []
1213

1314
if (value && value instanceof Array && value.length > 0) {
1415
const permissionFlag = value

src/directives/permission/hasRole.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export function hasRole(app: App<Element>) {
88
const { wsCache } = useCache()
99
const { value } = binding
1010
const super_admin = 'super_admin'
11-
const roles = wsCache.get(CACHE_KEY.USER).roles
11+
const userInfo = wsCache.get(CACHE_KEY.USER)
12+
const roles = userInfo?.roles || []
1213

1314
if (value && value instanceof Array && value.length > 0) {
1415
const roleFlag = value

src/store/modules/permission.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ export const usePermissionStore = defineStore('permission', {
3535
return new Promise<void>(async (resolve) => {
3636
// 获得菜单列表,它在登录的时候,setUserInfoAction 方法中已经进行获取
3737
let res: AppCustomRouteRecordRaw[] = []
38-
if (wsCache.get(CACHE_KEY.ROLE_ROUTERS)) {
39-
res = wsCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[]
38+
const roleRouters = wsCache.get(CACHE_KEY.ROLE_ROUTERS)
39+
if (roleRouters) {
40+
res = roleRouters as AppCustomRouteRecordRaw[]
4041
}
4142
const routerMap: AppRouteRecordRaw[] = generateRoute(res)
4243
// 动态路由,404一定要放到最后面

src/utils/auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const RefreshTokenKey = 'REFRESH_TOKEN'
1010
// 获取token
1111
export const getAccessToken = () => {
1212
// 此处与TokenKey相同,此写法解决初始化时Cookies中不存在TokenKey报错
13-
return wsCache.get(AccessTokenKey) ? wsCache.get(AccessTokenKey) : wsCache.get('ACCESS_TOKEN')
13+
const accessToken = wsCache.get(AccessTokenKey)
14+
return accessToken ? accessToken : wsCache.get('ACCESS_TOKEN')
1415
}
1516

1617
// 刷新token

src/utils/permission.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ export function checkPermi(value: string[]) {
1212
const { wsCache } = useCache()
1313
const permissionDatas = value
1414
const all_permission = '*:*:*'
15-
const permissions = wsCache.get(CACHE_KEY.USER).permissions
16-
const hasPermission = permissions.some((permission) => {
15+
const userInfo = wsCache.get(CACHE_KEY.USER)
16+
const permissions = userInfo?.permissions || []
17+
const hasPermission = permissions.some((permission: string) => {
1718
return all_permission === permission || permissionDatas.includes(permission)
1819
})
1920
return !!hasPermission
@@ -33,8 +34,9 @@ export function checkRole(value: string[]) {
3334
const { wsCache } = useCache()
3435
const permissionRoles = value
3536
const super_admin = 'super_admin'
36-
const roles = wsCache.get(CACHE_KEY.USER).roles
37-
const hasRole = roles.some((role) => {
37+
const userInfo = wsCache.get(CACHE_KEY.USER)
38+
const roles = userInfo?.roles || []
39+
const hasRole = roles.some((role: string) => {
3840
return super_admin === role || permissionRoles.includes(role)
3941
})
4042
return !!hasRole

0 commit comments

Comments
 (0)