225 lines
6.1 KiB
Vue
225 lines
6.1 KiB
Vue
<template>
|
|
<el-dropdown
|
|
v-if="companyName"
|
|
trigger="click"
|
|
class="tenant-switcher"
|
|
popper-class="tenant-switcher-popper"
|
|
@command="handleSwitch"
|
|
>
|
|
<span class="tenant-chip" :class="[variantClass, 'is-switchable']" :title="`当前企业:${companyName},点击切换`">
|
|
<el-icon v-if="iconVisible" class="tenant-chip-icon"><OfficeBuilding /></el-icon>
|
|
<span class="tenant-chip-text">{{ companyName }}</span>
|
|
<el-icon class="tenant-chip-caret"><ArrowDown /></el-icon>
|
|
</span>
|
|
<template #dropdown>
|
|
<el-dropdown-menu>
|
|
<el-dropdown-item
|
|
v-for="item in tenantOptions"
|
|
:key="item.tid"
|
|
:command="item.tid"
|
|
:disabled="isCurrent(item.tid)"
|
|
>
|
|
<span class="tenant-option">
|
|
<span class="tenant-option-name">{{ item.tenant_name }}</span>
|
|
<el-icon v-if="isCurrent(item.tid)" class="tenant-option-check"><Check /></el-icon>
|
|
</span>
|
|
</el-dropdown-item>
|
|
<el-dropdown-item v-if="tenantOptions.length === 1" disabled>
|
|
<span class="tenant-option-tip">当前账号未关联其他企业</span>
|
|
</el-dropdown-item>
|
|
<el-dropdown-item v-if="!tenantOptions.length" :command="RELOAD_COMMAND" :disabled="loading">
|
|
<span class="tenant-option-tip">
|
|
{{ loading ? "企业列表加载中…" : "未获取到企业列表,点击重试" }}
|
|
</span>
|
|
</el-dropdown-item>
|
|
</el-dropdown-menu>
|
|
</template>
|
|
</el-dropdown>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from "vue";
|
|
import { ElMessage } from "element-plus";
|
|
import { ArrowDown, Check, OfficeBuilding } from "@element-plus/icons-vue";
|
|
import { useAuthStore } from "@/stores/auth";
|
|
import { fetchUserInfo, resolveTenantName, switchTenant } from "@/utils/authClient";
|
|
|
|
const props = withDefaults(defineProps<{ variant?: "chip" | "plain" }>(), {
|
|
variant: "chip",
|
|
});
|
|
|
|
const authStore = useAuthStore();
|
|
|
|
const RELOAD_COMMAND = "__reload__";
|
|
|
|
const tenantOptions = ref<Array<{ tid: number | string; tenant_name: string }>>([]);
|
|
const loading = ref(false);
|
|
|
|
const companyName = computed(() => authStore.user?.tenant_name || "");
|
|
const currentTid = computed(() => authStore.user?.tid || "");
|
|
const variantClass = computed(() => (props.variant === "plain" ? "is-plain" : "is-chip"));
|
|
const iconVisible = computed(() => props.variant === "plain");
|
|
|
|
function isCurrent(tid: number | string) {
|
|
return !!tid && String(tid) === String(currentTid.value);
|
|
}
|
|
|
|
async function loadTenants() {
|
|
if (!authStore.token) return;
|
|
loading.value = true;
|
|
try {
|
|
const info = await fetchUserInfo();
|
|
const list = Array.isArray(info?.tenants) ? info.tenants : [];
|
|
tenantOptions.value = list.map((t: any) => ({
|
|
tid: t.tid,
|
|
tenant_name: t.tenant_name || `企业${t.tid}`,
|
|
}));
|
|
|
|
const name = resolveTenantName(info);
|
|
if (name && !companyName.value) {
|
|
authStore.updateUserInfo({
|
|
...authStore.user,
|
|
tid: info?.tid ?? authStore.user?.tid,
|
|
tenant_name: name,
|
|
});
|
|
}
|
|
} catch (e) {
|
|
tenantOptions.value = [];
|
|
console.error("加载可进入企业列表失败:", e);
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
function clearTenantScopedCache() {
|
|
try {
|
|
const removeKeys: string[] = [];
|
|
for (let i = 0; i < localStorage.length; i++) {
|
|
const key = localStorage.key(i);
|
|
if (key && (key.startsWith("menu_cache_") || key === "tabs_list" || key === "active_tab")) {
|
|
removeKeys.push(key);
|
|
}
|
|
}
|
|
removeKeys.forEach((key) => localStorage.removeItem(key));
|
|
sessionStorage.removeItem("tabs_list");
|
|
localStorage.removeItem("tenant");
|
|
sessionStorage.removeItem("tenant");
|
|
} catch (e) {
|
|
console.warn("清理企业级缓存失败:", e);
|
|
}
|
|
}
|
|
|
|
async function handleSwitch(tid: number | string) {
|
|
if (tid === RELOAD_COMMAND) {
|
|
await loadTenants();
|
|
return;
|
|
}
|
|
if (!tid || isCurrent(tid)) return;
|
|
const target = tenantOptions.value.find((item) => String(item.tid) === String(tid));
|
|
try {
|
|
await switchTenant(tid);
|
|
clearTenantScopedCache();
|
|
ElMessage.success(`已切换到「${target?.tenant_name || "新企业"}」`);
|
|
setTimeout(() => {
|
|
window.history.replaceState({}, document.title, `${window.location.pathname}#/home`);
|
|
window.location.reload();
|
|
}, 300);
|
|
} catch (e: any) {
|
|
ElMessage.error(e?.message || "切换企业失败");
|
|
}
|
|
}
|
|
|
|
onMounted(loadTenants);
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.tenant-chip {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 4px;
|
|
max-width: 220px;
|
|
padding: 4px 12px;
|
|
font-size: 13px;
|
|
font-weight: 500;
|
|
color: var(--el-color-primary);
|
|
background-color: var(--el-color-primary-light-9);
|
|
border: 1px solid var(--el-color-primary-light-7);
|
|
border-radius: 4px;
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
|
|
&.is-switchable {
|
|
cursor: pointer;
|
|
transition: border-color 0.2s ease, background-color 0.2s ease;
|
|
|
|
&:hover {
|
|
border-color: var(--el-color-primary);
|
|
background-color: var(--el-color-primary-light-8);
|
|
}
|
|
}
|
|
|
|
.tenant-chip-text {
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.tenant-chip-caret {
|
|
flex: 0 0 auto;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.tenant-chip-icon {
|
|
flex: 0 0 auto;
|
|
font-size: 15px;
|
|
}
|
|
}
|
|
|
|
.tenant-chip.is-chip {
|
|
height: 35px;
|
|
}
|
|
|
|
.tenant-chip.is-plain {
|
|
gap: 6px;
|
|
padding: 0;
|
|
max-width: 260px;
|
|
font-size: 15px;
|
|
color: var(--el-text-color-regular);
|
|
background-color: transparent;
|
|
border: none;
|
|
|
|
&.is-switchable:hover {
|
|
color: var(--el-color-primary);
|
|
background-color: transparent;
|
|
}
|
|
}
|
|
</style>
|
|
|
|
<style lang="less">
|
|
.tenant-switcher-popper {
|
|
.tenant-option {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 16px;
|
|
min-width: 150px;
|
|
|
|
.tenant-option-name {
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.tenant-option-check {
|
|
flex: 0 0 auto;
|
|
color: var(--el-color-primary);
|
|
}
|
|
}
|
|
|
|
.tenant-option-tip {
|
|
font-size: 12px;
|
|
color: var(--el-text-color-secondary);
|
|
}
|
|
}
|
|
</style>
|