Authority.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <!--
  2. Access control component for fine-grained access control.
  3. -->
  4. <script lang="ts">
  5. import type { PropType } from 'vue';
  6. import { defineComponent } from 'vue';
  7. import { RoleEnum } from '/@/enums/roleEnum';
  8. import { usePermission } from '/@/hooks/web/usePermission';
  9. import { getSlot } from '/@/utils/helper/tsxHelper';
  10. export default defineComponent({
  11. name: 'Authority',
  12. props: {
  13. /**
  14. * Specified role is visible
  15. * When the permission mode is the role mode, the value value can pass the role value.
  16. * When the permission mode is background, the value value can pass the code permission value
  17. * @default ''
  18. */
  19. value: {
  20. type: [Number, Array, String] as PropType<RoleEnum | RoleEnum[] | string | string[]>,
  21. default: '',
  22. },
  23. },
  24. setup(props, { slots }) {
  25. const { hasPermission } = usePermission();
  26. /**
  27. * Render role button
  28. */
  29. function renderAuth() {
  30. const { value } = props;
  31. if (!value) {
  32. return getSlot(slots);
  33. }
  34. return hasPermission(value) ? getSlot(slots) : null;
  35. }
  36. return () => {
  37. // Role-based value control
  38. return renderAuth();
  39. };
  40. },
  41. });
  42. </script>