perf: 优化 租户切换

pull/90/MERGE
xingyu4j 2025-05-06 15:44:31 +08:00
parent e5f33cd12b
commit bd02645e26
4 changed files with 47 additions and 45 deletions

View File

@ -1,63 +1,64 @@
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'; import type { SelectValue } from 'ant-design-vue/es/select';
import { onMounted, ref } from 'vue';
import { useAccess } from '@vben/access'; import { useAccess } from '@vben/access';
import { isTenantEnable, useRefresh } from '@vben/hooks'; import { isTenantEnable, useTabs } from '@vben/hooks';
import { useAccessStore } from '@vben/stores'; import { useAccessStore } from '@vben/stores';
import { Button, Dropdown, Menu } from 'ant-design-vue'; import { message, Select } from 'ant-design-vue';
import { getTenantList } from '#/api/system/tenant'; import { getTenantList } from '#/api/system/tenant';
import { $t } from '#/locales';
import { resetRoutes } from '#/router';
import { useAuthStore } from '#/store';
const { refresh } = useRefresh(); const { closeOtherTabs, refreshTab, closeAllTabs } = useTabs();
const { hasAccessByCodes } = useAccess(); const { hasAccessByCodes } = useAccess();
const accessStore = useAccessStore(); const accessStore = useAccessStore();
const authStore = useAuthStore();
const tenantEnable = isTenantEnable(); const tenantEnable = isTenantEnable();
const visitTenantList = computed(() => { const visitTenantList = accessStore.visitTenantId;
if (tenantEnable) {
const list = accessStore.visitTenantId.map((item) => ({
label: item.name,
id: item.id,
}));
return list;
}
return [];
});
const tenant = ref<string>( const tenant = ref<SelectValue>(accessStore.tenantId ?? 0);
visitTenantList.value.find((item) => item.id === accessStore.tenantId)
?.label || '切换租户',
);
function handleClick(id: number | undefined) { async function handleClick(id: SelectValue) {
if (id) { accessStore.setTenantId(Number(id));
accessStore.setTenantId(id); await authStore.fetchUserInfo();
refresh(); await resetRoutes();
window.location.reload(); await closeOtherTabs();
} await closeAllTabs();
await refreshTab();
message.success($t('page.tenant.success'));
} }
onMounted(async () => { onMounted(async () => {
if (tenantEnable) { if (tenantEnable) {
const resp = await getTenantList(); const resp = await getTenantList();
accessStore.setVisitTenantId(resp); accessStore.setVisitTenantId(
resp
.map((item) => ({ id: item.id, name: item.name }))
.filter((item): item is { id: number; name: string } => !!item.id),
);
} }
}); });
</script> </script>
<template> <template>
<Dropdown v-if="tenantEnable && hasAccessByCodes(['system:tenant:visit'])"> <div v-if="tenantEnable && hasAccessByCodes(['system:tenant:visit'])">
<template #overlay> <Select
<Menu> v-model:value="tenant"
<template v-for="item in visitTenantList" :key="item.key"> :field-names="{ label: 'name', value: 'id' }"
<Menu.Item @click="handleClick(item.id)"> :options="visitTenantList"
{{ item.label }} :placeholder="$t('page.tenant.placeholder')"
</Menu.Item> :dropdown-style="{ position: 'fixed', zIndex: 1666 }"
</template> allow-clear
</Menu> class="w-40"
</template> @change="handleClick"
<Button> {{ tenant }} </Button> />
</Dropdown> </div>
</template> </template>

View File

@ -24,5 +24,9 @@
"confirm": "Confirm", "confirm": "Confirm",
"reset": "Reset", "reset": "Reset",
"search": "Search" "search": "Search"
},
"tenant": {
"placeholder": "Please select tenant",
"success": "Switch tenant success"
} }
} }

View File

@ -24,5 +24,9 @@
"confirm": "确认", "confirm": "确认",
"reset": "重置", "reset": "重置",
"search": "搜索" "search": "搜索"
},
"tenant": {
"placeholder": "请选择租户",
"success": "切换租户成功"
} }
} }

View File

@ -7,15 +7,8 @@ import { acceptHMRUpdate, defineStore } from 'pinia';
type AccessToken = null | string; type AccessToken = null | string;
type VisitTenantId = { type VisitTenantId = {
accountCount: number; id: number;
contactMobile: string;
contactName: string;
expireTime: Date;
id?: number;
name: string; name: string;
packageId: number;
status: number;
website: string;
}; };
interface AccessState { interface AccessState {