diff --git a/src/hooks/usePermission.ts b/src/hooks/usePermission.ts index abb3d53..5f6c126 100644 --- a/src/hooks/usePermission.ts +++ b/src/hooks/usePermission.ts @@ -1,12 +1,11 @@ import { useAuthStore } from '@/store' -import { isArray, isString } from 'radash' /** 权限判断 */ export function usePermission() { const authStore = useAuthStore() function hasPermission( - permission: Entity.RoleType | Entity.RoleType[] | undefined, + permission?: Entity.RoleType[], ) { if (!permission) return true @@ -15,13 +14,9 @@ export function usePermission() { return false const { role } = authStore.userInfo - let has = role === 'super' + let has = role.includes('super') if (!has) { - if (isArray(permission)) - has = permission.includes(role) - - if (isString(permission)) - has = permission === role + has = permission.every(i => role.includes(i)) } return has } diff --git a/src/typings/api/login.d.ts b/src/typings/api/login.d.ts index 9692879..eb0e0cd 100644 --- a/src/typings/api/login.d.ts +++ b/src/typings/api/login.d.ts @@ -7,7 +7,7 @@ namespace Api { /** 用户id */ id: number /** 用户角色类型 */ - role: Entity.RoleType + role: Entity.RoleType[] /** 访问toekn */ accessToken: string /** 刷新toekn */ diff --git a/src/views/demo/permission/permission/index.vue b/src/views/demo/permission/permission/index.vue index 936d44a..84def75 100644 --- a/src/views/demo/permission/permission/index.vue +++ b/src/views/demo/permission/permission/index.vue @@ -4,7 +4,7 @@ import { useAuthStore } from '@/store' const authStore = useAuthStore() const { hasPermission } = usePermission() -const { role } = authStore.userInfo! +const { role } = authStore.userInfo const roleList: Entity.RoleType[] = ['super', 'admin', 'user'] @@ -23,7 +23,7 @@ function toggleUserRole(role: Entity.RoleType) { v-permission 指令用法 - + 仅super可见 @@ -33,10 +33,10 @@ function toggleUserRole(role: Entity.RoleType) { usePermission 函数用法 - + super可见 - + admin可见 @@ -45,5 +45,3 @@ function toggleUserRole(role: Entity.RoleType) { - -