2024-07-13 08:35:47 +00:00
|
|
|
|
<!--
|
|
|
|
|
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 []
|
2024-07-13 08:35:47 +00:00
|
|
|
|
*/
|
2024-07-20 00:31:05 +00:00
|
|
|
|
codes?: string[];
|
2024-07-13 08:35:47 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2024-07-18 13:31:34 +00:00
|
|
|
|
* 通过什么方式来控制组件,如果是 role,则传入角色,如果是 code,则传入权限码
|
|
|
|
|
* @default 'role'
|
2024-07-13 08:35:47 +00:00
|
|
|
|
*/
|
2024-07-18 13:31:34 +00:00
|
|
|
|
type?: 'code' | 'role';
|
2024-07-13 08:35:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
|
name: 'AccessControl',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
2024-07-20 00:31:05 +00:00
|
|
|
|
codes: () => [],
|
2024-07-13 08:35:47 +00:00
|
|
|
|
type: 'role',
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-18 13:31:34 +00:00
|
|
|
|
const { hasAccessByCodes, hasAccessByRoles } = useAccess();
|
2024-07-13 08:35:47 +00:00
|
|
|
|
|
|
|
|
|
const hasAuth = computed(() => {
|
2024-07-20 00:31:05 +00:00
|
|
|
|
const { codes, type } = props;
|
|
|
|
|
return type === 'role' ? hasAccessByRoles(codes) : hasAccessByCodes(codes);
|
2024-07-13 08:35:47 +00:00
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2024-07-20 00:31:05 +00:00
|
|
|
|
<slot v-if="!codes"></slot>
|
2024-07-13 08:35:47 +00:00
|
|
|
|
<slot v-else-if="hasAuth"></slot>
|
|
|
|
|
</template>
|