feat: 增加切换租户功能

This commit is contained in:
xingyu4j
2025-05-06 14:11:46 +08:00
parent e495219c87
commit 64ee81c327
5 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue';
import { useAccess } from '@vben/access';
import { isTenantEnable, useRefresh } from '@vben/hooks';
import { useAccessStore } from '@vben/stores';
import { Button, Dropdown, Menu } from 'ant-design-vue';
import { getTenantList } from '#/api/system/tenant';
const { refresh } = useRefresh();
const { hasAccessByCodes } = useAccess();
const accessStore = useAccessStore();
const tenantEnable = isTenantEnable();
const visitTenantList = computed(() => {
if (tenantEnable) {
const list = accessStore.visitTenantId.map((item) => ({
label: item.name,
id: item.id,
}));
return list;
}
return [];
});
const tenant = ref<string>(
visitTenantList.value.find((item) => item.id === accessStore.tenantId)
?.label || '切换租户',
);
function handleClick(id: number | undefined) {
if (id) {
accessStore.setTenantId(id);
refresh();
window.location.reload();
}
}
onMounted(async () => {
if (tenantEnable) {
const resp = await getTenantList();
accessStore.setVisitTenantId(resp);
}
});
</script>
<template>
<Dropdown v-if="tenantEnable && hasAccessByCodes(['system:tenant:visit'])">
<template #overlay>
<Menu>
<template v-for="item in visitTenantList" :key="item.key">
<Menu.Item @click="handleClick(item.id)">
{{ item.label }}
</Menu.Item>
</template>
</Menu>
</template>
<Button> {{ tenant }} </Button>
</Dropdown>
</template>