mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 22:29:19 +08:00
feat(auth): 超管切换账户迁至 SuperAuthController,新增多公司切换与 companyCount 并收紧 auth 白名单
This commit is contained in:
@@ -117,8 +117,19 @@ export async function getAccessCodesApi() {
|
||||
/**
|
||||
* 切换账户(超级管理员功能)
|
||||
*/
|
||||
export async function switchAccountApi(userId: number) {
|
||||
return requestClient.post<AuthApi.LoginResult>(`/auth/switch/${userId}`);
|
||||
export async function switchAccountApi(companyId: number, userId: number) {
|
||||
return requestClient.post<AuthApi.LoginResult>(
|
||||
`/super/auth/switch/company/${companyId}/user/${userId}`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换公司(重新签发 token)
|
||||
*/
|
||||
export async function switchCompanyApi(companyId: number) {
|
||||
return requestClient.post<AuthApi.LoginResult>(
|
||||
`/auth/switch-company/${companyId}`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,13 @@ export async function getUserInfoApi() {
|
||||
return requestClient.get<UserVO.UserInfoVO>('/user/info');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户可切换的可用公司列表
|
||||
*/
|
||||
export async function getAvailableCompaniesApi() {
|
||||
return requestClient.get<Array<{ id: number; name: string }>>('/user/companies');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,8 @@ export namespace UserVO {
|
||||
id: number;
|
||||
name: string;
|
||||
};
|
||||
/** 可切换的可用公司数量 */
|
||||
companyCount?: number;
|
||||
/** 部门id */
|
||||
departmentId?: number;
|
||||
/** 岗位id */
|
||||
|
||||
@@ -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>
|
||||
@@ -18,6 +18,7 @@ import { useAccessStore, useUserStore } from '@vben/stores';
|
||||
|
||||
import ImportTaskButton from '#/components/ImportTaskButton.vue';
|
||||
import SwitchAccountModal from '#/components/SwitchAccountModal.vue';
|
||||
import SwitchCompanyModal from '#/components/SwitchCompanyModal.vue';
|
||||
import { useAuthStore, useImportTaskStore } from '#/store';
|
||||
import LoginForm from '#/views/_core/authentication/login.vue';
|
||||
|
||||
@@ -70,7 +71,10 @@ onMounted(() => {
|
||||
|
||||
// 切换账户模态框引用
|
||||
const switchAccountModalRef = ref();
|
||||
const switchCompanyModalRef = ref();
|
||||
|
||||
// 切换公司图标
|
||||
const SwitchCompanyIcon = createIconifyIcon('lucide:building-2');
|
||||
// 切换账户图标
|
||||
const SwitchAccountIcon = createIconifyIcon('lucide:arrow-left-right');
|
||||
// 用户图标
|
||||
@@ -78,6 +82,12 @@ const UserIcon = createIconifyIcon('lucide:user');
|
||||
// 锁图标
|
||||
const LockIcon = createIconifyIcon('lucide:lock');
|
||||
|
||||
/** 当前用户可切换的可用公司数 */
|
||||
const companyCount = computed(() => {
|
||||
const info = userStore.userInfo as null | { companyCount?: number };
|
||||
return info?.companyCount ?? 0;
|
||||
});
|
||||
|
||||
const menus = computed(() => {
|
||||
const menuItems = [];
|
||||
|
||||
@@ -98,6 +108,17 @@ const menus = computed(() => {
|
||||
text: '修改密码',
|
||||
});
|
||||
|
||||
// 可切换公司数大于 1 时显示切换公司
|
||||
if (companyCount.value > 1) {
|
||||
menuItems.push({
|
||||
handler: () => {
|
||||
switchCompanyModalRef.value?.open();
|
||||
},
|
||||
icon: SwitchCompanyIcon,
|
||||
text: '切换公司',
|
||||
});
|
||||
}
|
||||
|
||||
// 只有超级管理员才显示切换账户功能
|
||||
if (accessStore.userRole === 'super') {
|
||||
menuItems.push({
|
||||
@@ -197,6 +218,8 @@ watch(
|
||||
</template>
|
||||
</BasicLayout>
|
||||
|
||||
<!-- 切换公司模态框 -->
|
||||
<SwitchCompanyModal ref="switchCompanyModalRef" />
|
||||
<!-- 切换账户模态框 -->
|
||||
<SwitchAccountModal ref="switchAccountModalRef" />
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user