admin-vben/packages/effects/access/src/access-control.vue

44 lines
889 B
Vue
Raw Normal View History

<!--
Access control component for fine-grained access control.
-->
<script lang="ts" setup>
import { computed } from 'vue';
import { useAccess } from './use-access';
interface Props {
/**
2024-07-18 13:31:34 +00:00
* Specified codes is visible
* @default []
*/
codes?: string[];
/**
2024-07-18 13:31:34 +00:00
* 通过什么方式来控制组件如果是 role则传入角色如果是 code则传入权限码
* @default 'role'
*/
2024-07-18 13:31:34 +00:00
type?: 'code' | 'role';
}
defineOptions({
name: 'AccessControl',
});
const props = withDefaults(defineProps<Props>(), {
codes: () => [],
type: 'role',
});
2024-07-18 13:31:34 +00:00
const { hasAccessByCodes, hasAccessByRoles } = useAccess();
const hasAuth = computed(() => {
const { codes, type } = props;
return type === 'role' ? hasAccessByRoles(codes) : hasAccessByCodes(codes);
});
</script>
<template>
<slot v-if="!codes"></slot>
<slot v-else-if="hasAuth"></slot>
</template>