From 7bef662db1e91aa8164e9f7a92de3fe4480a3c3b Mon Sep 17 00:00:00 2001 From: xingyu Date: Mon, 18 Jul 2022 16:02:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mock/user/index.ts | 2 +- src/directives/permission/hasPermi.ts | 39 +++++++++++----------- src/views/Example/Dialog/ExampleDialog.vue | 8 +++-- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/mock/user/index.ts b/mock/user/index.ts index efc289d..83bcd87 100644 --- a/mock/user/index.ts +++ b/mock/user/index.ts @@ -17,7 +17,7 @@ const List: { password: 'admin', role: 'admin', roleId: '1', - permissions: '*.*.*' + permissions: ['*.*.*'] }, { username: 'test', diff --git a/src/directives/permission/hasPermi.ts b/src/directives/permission/hasPermi.ts index 81f5022..0814c99 100644 --- a/src/directives/permission/hasPermi.ts +++ b/src/directives/permission/hasPermi.ts @@ -3,32 +3,33 @@ import { useI18n } from '@/hooks/web/useI18n' import { useCache } from '@/hooks/web/useCache' import { intersection } from 'lodash-es' import { isArray } from '@/utils/is' +import { useAppStoreWithOut } from '@/store/modules/app' const { t } = useI18n() const { wsCache } = useCache() -// 全部权限 -const all_permission = '*:*:*' -const permissions = wsCache.get('userInfo').permissions +const appStore = useAppStoreWithOut() +// 全部权限 +const all_permission = ['*.*.*'] +const hasPermission = (value: string | string[]): boolean => { + const permissions = wsCache.get(appStore.getUserInfo).permissions as string[] + if (!value) { + throw new Error(t('permission.hasPermission')) + } + if (!isArray(value)) { + return permissions?.includes(value as string) + } + if (all_permission[0] === permissions[0]) { + return true + } + return (intersection(value, permissions) as string[]).length > 0 +} function hasPermi(el: Element, binding: DirectiveBinding) { const value = binding.value - const hasPermission = (value: string | string[]): boolean => { - if (all_permission === permissions) return true - - if (!value) return true - - if (!isArray(value)) { - return permissions?.includes(value as string) - } - return (intersection(value, permissions) as string[]).length > 0 - } - - if (!hasPermission(value)) { + const flag = hasPermission(value) + if (!flag) { el.parentNode?.removeChild(el) - } else { - el.parentNode && el.parentNode.removeChild(el) - throw new Error(t('permission.hasPermission')) } } const mounted = (el: Element, binding: DirectiveBinding) => { @@ -39,7 +40,7 @@ const permiDirective: Directive = { mounted } -export function setupPermissionDirective(app: App) { +export const setupPermissionDirective = (app: App) => { app.directive('hasPermi', permiDirective) } diff --git a/src/views/Example/Dialog/ExampleDialog.vue b/src/views/Example/Dialog/ExampleDialog.vue index e3f5e40..c28a09c 100644 --- a/src/views/Example/Dialog/ExampleDialog.vue +++ b/src/views/Example/Dialog/ExampleDialog.vue @@ -231,10 +231,14 @@ const save = async () => { @register="register" >