diff --git a/src/directives/permission/hasPermi.ts b/src/directives/permission/hasPermi.ts index d86d2f54..931f44b3 100644 --- a/src/directives/permission/hasPermi.ts +++ b/src/directives/permission/hasPermi.ts @@ -8,7 +8,8 @@ export function hasPermi(app: App) { const { wsCache } = useCache() const { value } = binding const all_permission = '*:*:*' - const permissions = wsCache.get(CACHE_KEY.USER).permissions + const userInfo = wsCache.get(CACHE_KEY.USER) + const permissions = userInfo?.permissions || [] if (value && value instanceof Array && value.length > 0) { const permissionFlag = value diff --git a/src/directives/permission/hasRole.ts b/src/directives/permission/hasRole.ts index a5de2b8d..a5128112 100644 --- a/src/directives/permission/hasRole.ts +++ b/src/directives/permission/hasRole.ts @@ -8,7 +8,8 @@ export function hasRole(app: App) { const { wsCache } = useCache() const { value } = binding const super_admin = 'super_admin' - const roles = wsCache.get(CACHE_KEY.USER).roles + const userInfo = wsCache.get(CACHE_KEY.USER) + const roles = userInfo?.roles || [] if (value && value instanceof Array && value.length > 0) { const roleFlag = value diff --git a/src/store/modules/permission.ts b/src/store/modules/permission.ts index fc927f42..2ff8111a 100644 --- a/src/store/modules/permission.ts +++ b/src/store/modules/permission.ts @@ -35,8 +35,9 @@ export const usePermissionStore = defineStore('permission', { return new Promise(async (resolve) => { // 获得菜单列表,它在登录的时候,setUserInfoAction 方法中已经进行获取 let res: AppCustomRouteRecordRaw[] = [] - if (wsCache.get(CACHE_KEY.ROLE_ROUTERS)) { - res = wsCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[] + const roleRouters = wsCache.get(CACHE_KEY.ROLE_ROUTERS) + if (roleRouters) { + res = roleRouters as AppCustomRouteRecordRaw[] } const routerMap: AppRouteRecordRaw[] = generateRoute(res) // 动态路由,404一定要放到最后面 diff --git a/src/utils/auth.ts b/src/utils/auth.ts index c68a67a9..d9e85650 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -10,7 +10,8 @@ const RefreshTokenKey = 'REFRESH_TOKEN' // 获取token export const getAccessToken = () => { // 此处与TokenKey相同,此写法解决初始化时Cookies中不存在TokenKey报错 - return wsCache.get(AccessTokenKey) ? wsCache.get(AccessTokenKey) : wsCache.get('ACCESS_TOKEN') + const accessToken = wsCache.get(AccessTokenKey) + return accessToken ? accessToken : wsCache.get('ACCESS_TOKEN') } // 刷新token diff --git a/src/utils/permission.ts b/src/utils/permission.ts index f6077d1a..3bf67b61 100644 --- a/src/utils/permission.ts +++ b/src/utils/permission.ts @@ -12,8 +12,9 @@ export function checkPermi(value: string[]) { const { wsCache } = useCache() const permissionDatas = value const all_permission = '*:*:*' - const permissions = wsCache.get(CACHE_KEY.USER).permissions - const hasPermission = permissions.some((permission) => { + const userInfo = wsCache.get(CACHE_KEY.USER) + const permissions = userInfo?.permissions || [] + const hasPermission = permissions.some((permission: string) => { return all_permission === permission || permissionDatas.includes(permission) }) return !!hasPermission @@ -33,8 +34,9 @@ export function checkRole(value: string[]) { const { wsCache } = useCache() const permissionRoles = value const super_admin = 'super_admin' - const roles = wsCache.get(CACHE_KEY.USER).roles - const hasRole = roles.some((role) => { + const userInfo = wsCache.get(CACHE_KEY.USER) + const roles = userInfo?.roles || [] + const hasRole = roles.some((role: string) => { return super_admin === role || permissionRoles.includes(role) }) return !!hasRole