2024-07-13 08:35:47 +00:00
|
|
|
|
<!--
|
|
|
|
|
Access control component for fine-grained access control.
|
2024-07-30 13:05:03 +00:00
|
|
|
|
TODO: 可以扩展更完善的功能:
|
|
|
|
|
1. 支持多个权限码,只要有一个权限码满足即可 或者 多个权限码全部满足
|
|
|
|
|
2. 支持多个角色,只要有一个角色满足即可 或者 多个角色全部满足
|
|
|
|
|
3. 支持自定义权限码和角色的判断逻辑
|
2024-07-13 08:35:47 +00:00
|
|
|
|
-->
|
|
|
|
|
<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>
|