diff --git a/src/components/Auth/index.vue b/src/components/Auth/index.vue index a25fe087f..369fd05c9 100755 --- a/src/components/Auth/index.vue +++ b/src/components/Auth/index.vue @@ -5,16 +5,19 @@ defineOptions({ const props = defineProps<{ value: string | string[] + all?: boolean }>() +const auth = useAuth() + function check() { - return useAuth().auth(props.value) + return props.all + ? auth.authAll(typeof props.value === 'string' ? [props.value] : props.value) + : auth.auth(props.value) } diff --git a/src/components/AuthAll/index.vue b/src/components/AuthAll/index.vue deleted file mode 100755 index 3c0f36ecc..000000000 --- a/src/components/AuthAll/index.vue +++ /dev/null @@ -1,20 +0,0 @@ - - - diff --git a/src/types/components.d.ts b/src/types/components.d.ts index 2d837636f..7629dc7de 100755 --- a/src/types/components.d.ts +++ b/src/types/components.d.ts @@ -8,7 +8,6 @@ export {} declare module 'vue' { export interface GlobalComponents { Auth: typeof import('./../components/Auth/index.vue')['default'] - AuthAll: typeof import('./../components/AuthAll/index.vue')['default'] FileUpload: typeof import('./../components/FileUpload/index.vue')['default'] FixedActionBar: typeof import('./../components/FixedActionBar/index.vue')['default'] HButton: typeof import('./../layouts/ui-kit/HButton.vue')['default'] diff --git a/src/utils/directive.ts b/src/utils/directive.ts index bbd2481a9..93d4df78e 100755 --- a/src/utils/directive.ts +++ b/src/utils/directive.ts @@ -1,19 +1,12 @@ -import type { App } from 'vue' +import type { App, DirectiveBinding } from 'vue' export default function directive(app: App) { - // 注册 v-auth 和 v-auth-all 指令 - app.directive('auth', { - mounted: (el, binding) => { - if (!useAuth().auth(binding.value)) { - el.remove() - } - }, - }) - app.directive('auth-all', { - mounted: (el, binding) => { - if (!useAuth().authAll(binding.value)) { - el.remove() - } - }, + app.directive('auth', (el: HTMLElement, binding: DirectiveBinding) => { + if (binding.modifiers.all ? useAuth().authAll(binding.value) : useAuth().auth(binding.value)) { + el.style.display = '' + } + else { + el.style.display = 'none' + } }) } diff --git a/src/views/permission_example/index.vue b/src/views/permission_example/index.vue index 6ab35451a..7c689a50d 100755 --- a/src/views/permission_example/index.vue +++ b/src/views/permission_example/index.vue @@ -14,7 +14,6 @@ const settingsStore = useSettingsStore() const userStore = useUserStore() const { auth, authAll } = useAuth() -const mainPage = useMainPage() // 模拟账号切换 async function accountChange(val: any) { @@ -23,7 +22,6 @@ async function accountChange(val: any) { password: '', }) await userStore.getPermissions() - mainPage.reload() } function goTest() { router.push({ @@ -76,9 +74,24 @@ function permissionCheck2(permissions: string[]) { 点击访问 -

鉴权组件(请对照代码查看)

+

鉴权指令(请对照代码查看)

- +
+ 如果你有 permission.browse 权限则能看到这句话 +
+
+ 如果你有 permission.create 权限则能看到这句话 +
+
+ 如果你有 permission.browse 或 permission.create 权限则能看到这句话 +
+
+ 如果你有 permission.browse 和 permission.create 权限则能看到这句话 +
+
+

鉴权组件(请对照代码查看)

+
+ 你有 permission.browse 权限 - + 你有 permission.create 权限 - + 你有 permission.browse 或 permission.create 权限 - + 你有 permission.browse 和 permission.create 权限 - -
-

鉴权指令(请对照代码查看)

-
-
- 如果你有 permission.browse 权限则能看到这句话 -
-
- 如果你有 permission.create 权限则能看到这句话 -
-
- 如果你有 permission.browse 或 permission.create 权限则能看到这句话 -
-
- 如果你有 permission.browse 和 permission.create 权限则能看到这句话 -
+

鉴权函数(请对照代码查看)