feat(auth): 超管切换账户迁至 SuperAuthController,新增多公司切换与 companyCount 并收紧 auth 白名单

This commit is contained in:
小蟑螂
2026-05-25 22:47:25 +08:00
parent d4bfa4d257
commit 2ef5b9a41a
13 changed files with 402 additions and 19 deletions
@@ -217,11 +217,18 @@ const handleSwitchAccount = async () => {
message.warning('请选择要切换的用户');
return;
}
if (!formData.value.companyId) {
message.warning('请选择要切换的公司');
return;
}
try {
modalApi.setState({ loading: true });
const result = await switchAccountApi(formData.value.userId);
const result = await switchAccountApi(
formData.value.companyId,
formData.value.userId,
);
// 设置新的token
const accessStore = useAccessStore();
@@ -0,0 +1,161 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useVbenModal } from '@vben/common-ui';
import { useTabs } from '@vben/hooks';
import { preferences } from '@vben/preferences';
import { useAccessStore, useUserStore } from '@vben/stores';
import { message, Radio, Spin } from 'ant-design-vue';
import { getAccessCodesApi, switchCompanyApi } from '#/api/core/auth';
import { getAvailableCompaniesApi } from '#/api/system/user';
import { useAuthStore } from '#/store';
interface CompanyOption {
id: number;
name: string;
}
const userStore = useUserStore();
const accessStore = useAccessStore();
const authStore = useAuthStore();
const { closeAllTabs } = useTabs();
const router = useRouter();
const loading = ref(false);
const companyList = ref<CompanyOption[]>([]);
const selectedCompanyId = ref<number>();
const currentCompanyId = computed(() => {
const info = userStore.userInfo as null | { company?: { id?: number } };
return info?.company?.id;
});
const companyOptions = computed(() =>
companyList.value.map((company) => ({
label: company.name,
value: company.id,
disabled: company.id === currentCompanyId.value,
})),
);
async function loadCompanies() {
loading.value = true;
try {
companyList.value = await getAvailableCompaniesApi();
selectedCompanyId.value = companyList.value.find(
(item) => item.id !== currentCompanyId.value,
)?.id;
} catch (error) {
console.error('加载公司列表失败:', error);
message.error('加载公司列表失败');
} finally {
loading.value = false;
}
}
async function handleSwitchCompany() {
if (!selectedCompanyId.value) {
message.warning('请选择要切换的公司');
return;
}
if (selectedCompanyId.value === currentCompanyId.value) {
message.warning('当前已在该公司,无需切换');
return;
}
try {
modalApi.setState({ loading: true });
const result = await switchCompanyApi(selectedCompanyId.value);
accessStore.setTokens(
result.accessToken,
result.refreshToken,
result.accessTokenExpiresAt,
result.refreshTokenExpiresAt,
);
try {
const accessCodes = await getAccessCodesApi();
accessStore.setAccessCodes(accessCodes);
} catch (error) {
console.warn('获取用户权限码失败:', error);
accessStore.setAccessCodes([]);
}
await authStore.fetchUserInfo();
accessStore.setIsAccessChecked(false);
await closeAllTabs();
await router.replace(preferences.app.defaultHomePath);
message.success('切换公司成功');
modalApi.close();
window.location.reload();
} catch (error: any) {
console.error('切换公司失败:', error);
message.error(error?.message || '切换公司失败');
} finally {
modalApi.setState({ loading: false });
}
}
const [ModalComponent, modalApi] = useVbenModal({
onCancel() {
modalApi.close();
},
onConfirm: async () => {
await handleSwitchCompany();
},
});
function openModal() {
modalApi.open();
selectedCompanyId.value = undefined;
loadCompanies();
}
function closeModal() {
modalApi.close();
}
defineExpose({
open: openModal,
close: closeModal,
});
</script>
<template>
<ModalComponent title="切换公司" width="520px">
<div v-if="loading" class="py-8 text-center">
<Spin />
<div class="mt-2 text-gray-500">加载中...</div>
</div>
<div v-else-if="companyOptions.length === 0" class="py-8 text-center text-gray-400">
暂无可用公司
</div>
<Radio.Group
v-else
v-model:value="selectedCompanyId"
class="mt-4 flex w-full flex-col gap-3"
>
<Radio
v-for="option in companyOptions"
:key="option.value"
:value="option.value"
:disabled="option.disabled"
class="!mr-0 rounded-md border px-3 py-2"
>
<span>{{ option.label }}</span>
<span
v-if="option.value === currentCompanyId"
class="ml-2 text-xs text-gray-400"
>
当前
</span>
</Radio>
</Radio.Group>
</ModalComponent>
</template>