From 75d0bdcc498a278fc894a8d26a352a68486ccd24 Mon Sep 17 00:00:00 2001 From: shizhong <124974919@qq.com> Date: Thu, 21 Mar 2024 15:09:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A9=BA=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/directives/permission/hasPermi.ts | 23 ++++++++++++++++++++++- src/directives/permission/hasRole.ts | 2 +- src/utils/permission.ts | 4 ++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/directives/permission/hasPermi.ts b/src/directives/permission/hasPermi.ts index d86d2f54..ca4cb537 100644 --- a/src/directives/permission/hasPermi.ts +++ b/src/directives/permission/hasPermi.ts @@ -8,7 +8,7 @@ export function hasPermi(app: App) { const { wsCache } = useCache() const { value } = binding const all_permission = '*:*:*' - const permissions = wsCache.get(CACHE_KEY.USER).permissions + const permissions = wsCache.get(CACHE_KEY.USER)?.permissions ? wsCache.get(CACHE_KEY.USER)?.permissions : [] if (value && value instanceof Array && value.length > 0) { const permissionFlag = value @@ -25,3 +25,24 @@ export function hasPermi(app: App) { } }) } + +export function hasPermiEvery(app: App) { + app.directive('hasPermiEvery', (el, binding) => { + const { wsCache } = useCache() + const { value } = binding + const all_permission = '*:*:*' + const permissions = wsCache.get(CACHE_KEY.USER)?.permissions ? wsCache.get(CACHE_KEY.USER)?.permissions : [] + if (value && value instanceof Array && value.length > 0) { + const permissionFlag = value + const hasPermissions = permissionFlag.every((permission: string) => { + return all_permission === permission || permissions.includes(permission) + }) + + if (!hasPermissions) { + el.parentNode && el.parentNode.removeChild(el) + } + } else { + throw new Error(t('permission.hasPermission')) + } + }) +} diff --git a/src/directives/permission/hasRole.ts b/src/directives/permission/hasRole.ts index 31a352a7..764fea1c 100644 --- a/src/directives/permission/hasRole.ts +++ b/src/directives/permission/hasRole.ts @@ -8,7 +8,7 @@ export function hasRole(app: App) { const { wsCache } = useCache() const { value } = binding const super_admin = 'admin' - const roles = wsCache.get(CACHE_KEY.USER).roles + const roles = wsCache.get(CACHE_KEY.USER)?.roles ? wsCache.get(CACHE_KEY.USER)?.roles : [] if (value && value instanceof Array && value.length > 0) { const roleFlag = value diff --git a/src/utils/permission.ts b/src/utils/permission.ts index a63ee628..ed4fe6ad 100644 --- a/src/utils/permission.ts +++ b/src/utils/permission.ts @@ -12,7 +12,7 @@ export function checkPermi(value: string[]) { const { wsCache } = useCache() const permissionDatas = value const all_permission = '*:*:*' - const permissions = wsCache.get(CACHE_KEY.USER).permissions + const permissions = wsCache.get(CACHE_KEY.USER)?.permissions ? wsCache.get(CACHE_KEY.USER)?.permissions : [] const hasPermission = permissions.some((permission) => { return all_permission === permission || permissionDatas.includes(permission) }) @@ -33,7 +33,7 @@ export function checkRole(value: string[]) { const { wsCache } = useCache() const permissionRoles = value const super_admin = 'admin' - const roles = wsCache.get(CACHE_KEY.USER).roles + const roles = wsCache.get(CACHE_KEY.USER)?.roles ? wsCache.get(CACHE_KEY.USER)?.roles : [] const hasRole = roles.some((role) => { return super_admin === role || permissionRoles.includes(role) })