mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 11:49:19 +08:00
380 lines
9.8 KiB
Vue
380 lines
9.8 KiB
Vue
<script setup lang="ts">
|
|
import type { CompanyVO } from '#/api/organization/company/types';
|
|
import type { UserVO } from '#/api/system/user/types';
|
|
import type { PageQueryDTO } from '#/types/common';
|
|
|
|
import { computed, ref, watch } 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 } from '@vben/stores';
|
|
|
|
import { Avatar, Card, Form, message, Select, Spin } from 'ant-design-vue';
|
|
|
|
import { switchAccountApi } from '#/api/core/auth';
|
|
import { getCompanyPageApi } from '#/api/organization/company';
|
|
import { getUserPageApi } from '#/api/system/user';
|
|
|
|
// 响应式数据
|
|
const formRef = ref();
|
|
const formData = ref({
|
|
companyId: undefined as number | undefined,
|
|
userId: undefined as number | undefined,
|
|
});
|
|
|
|
// 公司相关数据
|
|
const companyList = ref<CompanyVO.CompanyInfoVO[]>([]);
|
|
const companyLoading = ref(false);
|
|
const companySearchValue = ref('');
|
|
const companyPage = ref(1);
|
|
const companyPageSize = ref(20);
|
|
const companyHasMore = ref(true);
|
|
|
|
// 用户相关数据
|
|
const userList = ref<UserVO.UserInfoVO[]>([]);
|
|
const userLoading = ref(false);
|
|
const userSearchValue = ref('');
|
|
const userPage = ref(1);
|
|
const userPageSize = ref(20);
|
|
const userHasMore = ref(true);
|
|
|
|
// 选中的用户信息
|
|
const selectedUser = ref<null | UserVO.UserInfoVO>(null);
|
|
const { closeAllTabs } = useTabs();
|
|
const router = useRouter();
|
|
|
|
// 公司选项
|
|
const companyOptions = computed(() =>
|
|
companyList.value.map((company) => ({
|
|
label: company.name,
|
|
value: company.id,
|
|
company,
|
|
})),
|
|
);
|
|
|
|
// 用户选项
|
|
const userOptions = computed(() =>
|
|
userList.value.map((user) => ({
|
|
label: `${user.realName} (${user.loginCode})`,
|
|
value: user.id,
|
|
user,
|
|
})),
|
|
);
|
|
|
|
// 监听公司选择变化
|
|
watch(
|
|
() => formData.value.companyId,
|
|
(newCompanyId) => {
|
|
if (newCompanyId) {
|
|
// 重置用户数据
|
|
formData.value.userId = undefined;
|
|
userList.value = [];
|
|
userPage.value = 1;
|
|
userHasMore.value = true;
|
|
selectedUser.value = null;
|
|
// 加载该公司的用户
|
|
loadUsers();
|
|
}
|
|
},
|
|
);
|
|
|
|
// 监听用户选择变化
|
|
watch(
|
|
() => formData.value.userId,
|
|
(newUserId) => {
|
|
if (newUserId) {
|
|
const user = userList.value.find((u) => u.id === newUserId);
|
|
selectedUser.value = user || null;
|
|
} else {
|
|
selectedUser.value = null;
|
|
}
|
|
},
|
|
);
|
|
|
|
// 加载公司列表
|
|
const loadCompanies = async (reset = false) => {
|
|
if (companyLoading.value) return;
|
|
|
|
try {
|
|
companyLoading.value = true;
|
|
|
|
if (reset) {
|
|
companyList.value = [];
|
|
companyPage.value = 1;
|
|
companyHasMore.value = true;
|
|
}
|
|
|
|
const params: PageQueryDTO<{ name?: string }> = {
|
|
page: companyPage.value,
|
|
pageSize: companyPageSize.value,
|
|
filters: {
|
|
name: companySearchValue.value || undefined,
|
|
},
|
|
};
|
|
|
|
const result = await getCompanyPageApi(params);
|
|
|
|
if (reset) {
|
|
companyList.value = result.records;
|
|
} else {
|
|
companyList.value.push(...result.records);
|
|
}
|
|
|
|
companyHasMore.value = result.records.length === companyPageSize.value;
|
|
companyPage.value++;
|
|
} catch (error) {
|
|
console.error('加载公司列表失败:', error);
|
|
message.error('加载公司列表失败');
|
|
} finally {
|
|
companyLoading.value = false;
|
|
}
|
|
};
|
|
|
|
// 加载用户列表
|
|
const loadUsers = async (reset = false) => {
|
|
if (userLoading.value || !formData.value.companyId) return;
|
|
|
|
try {
|
|
userLoading.value = true;
|
|
|
|
if (reset) {
|
|
userList.value = [];
|
|
userPage.value = 1;
|
|
userHasMore.value = true;
|
|
}
|
|
|
|
const params: PageQueryDTO<{
|
|
companyId?: number;
|
|
loginCode?: string;
|
|
realName?: string;
|
|
}> = {
|
|
page: userPage.value,
|
|
pageSize: userPageSize.value,
|
|
filters: {
|
|
realName: userSearchValue.value || undefined,
|
|
loginCode: userSearchValue.value || undefined,
|
|
companyId: formData.value.companyId,
|
|
},
|
|
};
|
|
|
|
const result = await getUserPageApi(params);
|
|
|
|
if (reset) {
|
|
userList.value = result.records;
|
|
} else {
|
|
userList.value.push(...result.records);
|
|
}
|
|
|
|
userHasMore.value = result.records.length === userPageSize.value;
|
|
userPage.value++;
|
|
} catch (error) {
|
|
console.error('加载用户列表失败:', error);
|
|
message.error('加载用户列表失败');
|
|
} finally {
|
|
userLoading.value = false;
|
|
}
|
|
};
|
|
|
|
// 公司搜索
|
|
const handleCompanySearch = (value: string) => {
|
|
companySearchValue.value = value;
|
|
loadCompanies(true);
|
|
};
|
|
|
|
// 用户搜索
|
|
const handleUserSearch = (value: string) => {
|
|
userSearchValue.value = value;
|
|
loadUsers(true);
|
|
};
|
|
|
|
// 公司下拉框滚动加载更多
|
|
const handleCompanyScroll = (e: any) => {
|
|
const { target } = e;
|
|
if (
|
|
target.scrollTop + target.offsetHeight === target.scrollHeight &&
|
|
companyHasMore.value
|
|
) {
|
|
loadCompanies();
|
|
}
|
|
};
|
|
|
|
// 用户下拉框滚动加载更多
|
|
const handleUserScroll = (e: any) => {
|
|
const { target } = e;
|
|
if (
|
|
target.scrollTop + target.offsetHeight === target.scrollHeight &&
|
|
userHasMore.value
|
|
) {
|
|
loadUsers();
|
|
}
|
|
};
|
|
|
|
// 切换账户
|
|
const handleSwitchAccount = async () => {
|
|
if (!formData.value.userId) {
|
|
message.warning('请选择要切换的用户');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
modalApi.setState({ loading: true });
|
|
|
|
const result = await switchAccountApi(formData.value.userId);
|
|
|
|
// 设置新的token
|
|
const accessStore = useAccessStore();
|
|
accessStore.setTokens(
|
|
result.accessToken,
|
|
result.refreshToken,
|
|
result.accessTokenExpiresAt,
|
|
result.refreshTokenExpiresAt,
|
|
);
|
|
|
|
// 重置权限检查状态,让路由守卫重新生成菜单和路由
|
|
accessStore.setIsAccessChecked(false);
|
|
|
|
// 清空当前标签页,避免不同公司下携带旧参数(如店铺ID)的页面残留
|
|
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 handleSwitchAccount();
|
|
},
|
|
});
|
|
|
|
// 打开模态框
|
|
function openModal() {
|
|
modalApi.open();
|
|
// 重置表单数据
|
|
formData.value = {
|
|
companyId: undefined,
|
|
userId: undefined,
|
|
};
|
|
selectedUser.value = null;
|
|
// 加载公司列表
|
|
loadCompanies(true);
|
|
}
|
|
|
|
// 关闭模态框
|
|
function closeModal() {
|
|
modalApi.close();
|
|
}
|
|
|
|
// 暴露方法
|
|
defineExpose({
|
|
open: openModal,
|
|
close: closeModal,
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<ModalComponent title="切换账户" width="800px">
|
|
<Form ref="formRef" :model="formData" layout="vertical" class="mt-4">
|
|
<Form.Item label="选择公司" required>
|
|
<Select
|
|
v-model:value="formData.companyId"
|
|
placeholder="请选择公司"
|
|
show-search
|
|
:filter-option="false"
|
|
:loading="companyLoading"
|
|
@search="handleCompanySearch"
|
|
@popup-scroll="handleCompanyScroll"
|
|
>
|
|
<template #notFoundContent>
|
|
<div v-if="companyLoading" class="py-4 text-center">
|
|
<Spin size="small" />
|
|
<div class="mt-2">加载中...</div>
|
|
</div>
|
|
<div v-else class="py-4 text-center text-gray-400">暂无数据</div>
|
|
</template>
|
|
<Select.Option
|
|
v-for="option in companyOptions"
|
|
:key="option.value"
|
|
:value="option.value"
|
|
:label="option.label"
|
|
>
|
|
{{ option.label }}
|
|
</Select.Option>
|
|
</Select>
|
|
</Form.Item>
|
|
|
|
<Form.Item label="选择用户" required>
|
|
<Select
|
|
v-model:value="formData.userId"
|
|
placeholder="请选择用户"
|
|
show-search
|
|
:filter-option="false"
|
|
:loading="userLoading"
|
|
:disabled="!formData.companyId"
|
|
@search="handleUserSearch"
|
|
@popup-scroll="handleUserScroll"
|
|
>
|
|
<template #notFoundContent>
|
|
<div v-if="userLoading" class="py-4 text-center">
|
|
<Spin size="small" />
|
|
<div class="mt-2">加载中...</div>
|
|
</div>
|
|
<div
|
|
v-else-if="!formData.companyId"
|
|
class="py-4 text-center text-gray-400"
|
|
>
|
|
请先选择公司
|
|
</div>
|
|
<div v-else class="py-4 text-center text-gray-400">暂无数据</div>
|
|
</template>
|
|
<Select.Option
|
|
v-for="option in userOptions"
|
|
:key="option.value"
|
|
:value="option.value"
|
|
:label="option.label"
|
|
>
|
|
{{ option.label }}
|
|
</Select.Option>
|
|
</Select>
|
|
</Form.Item>
|
|
</Form>
|
|
|
|
<!-- 用户信息展示 -->
|
|
<Card v-if="selectedUser" title="用户信息" class="mt-4">
|
|
<div class="flex items-center space-x-4">
|
|
<Avatar :src="selectedUser.avatar" size="large">
|
|
{{ selectedUser.realName?.charAt(0) }}
|
|
</Avatar>
|
|
<div class="flex-1">
|
|
<div class="text-lg font-medium">{{ selectedUser.realName }}</div>
|
|
<div class="text-gray-500">
|
|
登录账号: {{ selectedUser.loginCode }}
|
|
</div>
|
|
<div class="text-gray-500">
|
|
手机号码: {{ selectedUser.phone || '-' }}
|
|
</div>
|
|
<div class="text-gray-500">角色: {{ selectedUser.role }}</div>
|
|
<div class="text-gray-500">
|
|
状态: {{ selectedUser.status === 1 ? '正常' : '禁用' }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Card>
|
|
</ModalComponent>
|
|
</template>
|