mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 22:29:19 +08:00
prettier: 代码格式化
This commit is contained in:
@@ -1,283 +1,286 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { Form, Select, Card, Avatar, message, Spin } from 'ant-design-vue'
|
||||
import { useVbenModal } from '@vben/common-ui'
|
||||
import { useAccessStore } from '@vben/stores'
|
||||
import { switchAccountApi } from '#/api/core/auth'
|
||||
import { getCompanyPageApi } from '#/api/organization/company'
|
||||
import { getUserPageApi } from '#/api/system/user'
|
||||
import type { CompanyVO } from '#/api/organization/company/types'
|
||||
import type { UserVO } from '#/api/system/user/types'
|
||||
import type { PageQueryDTO } from '#/types/common'
|
||||
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 { useVbenModal } from '@vben/common-ui';
|
||||
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 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 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 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<UserVO.UserInfoVO | null>(null)
|
||||
const selectedUser = ref<null | UserVO.UserInfoVO>(null);
|
||||
|
||||
// 公司选项
|
||||
const companyOptions = computed(() =>
|
||||
companyList.value.map(company => ({
|
||||
const companyOptions = computed(() =>
|
||||
companyList.value.map((company) => ({
|
||||
label: company.name,
|
||||
value: company.id,
|
||||
company
|
||||
}))
|
||||
)
|
||||
company,
|
||||
})),
|
||||
);
|
||||
|
||||
// 用户选项
|
||||
const userOptions = computed(() =>
|
||||
userList.value.map(user => ({
|
||||
const userOptions = computed(() =>
|
||||
userList.value.map((user) => ({
|
||||
label: `${user.realName} (${user.loginCode})`,
|
||||
value: user.id,
|
||||
user
|
||||
}))
|
||||
)
|
||||
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.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
|
||||
}
|
||||
})
|
||||
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
|
||||
|
||||
if (companyLoading.value) return;
|
||||
|
||||
try {
|
||||
companyLoading.value = true
|
||||
|
||||
companyLoading.value = true;
|
||||
|
||||
if (reset) {
|
||||
companyList.value = []
|
||||
companyPage.value = 1
|
||||
companyHasMore.value = true
|
||||
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)
|
||||
|
||||
},
|
||||
};
|
||||
|
||||
const result = await getCompanyPageApi(params);
|
||||
|
||||
if (reset) {
|
||||
companyList.value = result.records
|
||||
companyList.value = result.records;
|
||||
} else {
|
||||
companyList.value.push(...result.records)
|
||||
companyList.value.push(...result.records);
|
||||
}
|
||||
|
||||
companyHasMore.value = result.records.length === companyPageSize.value
|
||||
companyPage.value++
|
||||
|
||||
|
||||
companyHasMore.value = result.records.length === companyPageSize.value;
|
||||
companyPage.value++;
|
||||
} catch (error) {
|
||||
console.error('加载公司列表失败:', error)
|
||||
message.error('加载公司列表失败')
|
||||
console.error('加载公司列表失败:', error);
|
||||
message.error('加载公司列表失败');
|
||||
} finally {
|
||||
companyLoading.value = false
|
||||
companyLoading.value = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 加载用户列表
|
||||
const loadUsers = async (reset = false) => {
|
||||
if (userLoading.value || !formData.value.companyId) return
|
||||
|
||||
if (userLoading.value || !formData.value.companyId) return;
|
||||
|
||||
try {
|
||||
userLoading.value = true
|
||||
|
||||
userLoading.value = true;
|
||||
|
||||
if (reset) {
|
||||
userList.value = []
|
||||
userPage.value = 1
|
||||
userHasMore.value = true
|
||||
userList.value = [];
|
||||
userPage.value = 1;
|
||||
userHasMore.value = true;
|
||||
}
|
||||
|
||||
const params: PageQueryDTO<{ realName?: string; loginCode?: string; companyId?: number }> = {
|
||||
|
||||
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)
|
||||
|
||||
},
|
||||
};
|
||||
|
||||
const result = await getUserPageApi(params);
|
||||
|
||||
if (reset) {
|
||||
userList.value = result.records
|
||||
userList.value = result.records;
|
||||
} else {
|
||||
userList.value.push(...result.records)
|
||||
userList.value.push(...result.records);
|
||||
}
|
||||
|
||||
userHasMore.value = result.records.length === userPageSize.value
|
||||
userPage.value++
|
||||
|
||||
|
||||
userHasMore.value = result.records.length === userPageSize.value;
|
||||
userPage.value++;
|
||||
} catch (error) {
|
||||
console.error('加载用户列表失败:', error)
|
||||
message.error('加载用户列表失败')
|
||||
console.error('加载用户列表失败:', error);
|
||||
message.error('加载用户列表失败');
|
||||
} finally {
|
||||
userLoading.value = false
|
||||
userLoading.value = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 公司搜索
|
||||
const handleCompanySearch = (value: string) => {
|
||||
companySearchValue.value = value
|
||||
loadCompanies(true)
|
||||
}
|
||||
companySearchValue.value = value;
|
||||
loadCompanies(true);
|
||||
};
|
||||
|
||||
// 用户搜索
|
||||
const handleUserSearch = (value: string) => {
|
||||
userSearchValue.value = value
|
||||
loadUsers(true)
|
||||
}
|
||||
userSearchValue.value = value;
|
||||
loadUsers(true);
|
||||
};
|
||||
|
||||
// 公司下拉框滚动加载更多
|
||||
const handleCompanyScroll = (e: any) => {
|
||||
const { target } = e
|
||||
if (target.scrollTop + target.offsetHeight === target.scrollHeight && companyHasMore.value) {
|
||||
loadCompanies()
|
||||
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 { target } = e;
|
||||
if (
|
||||
target.scrollTop + target.offsetHeight === target.scrollHeight &&
|
||||
userHasMore.value
|
||||
) {
|
||||
loadUsers();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 切换账户
|
||||
const handleSwitchAccount = async () => {
|
||||
console.log('🔄 切换账户函数被调用', { userId: formData.value.userId })
|
||||
|
||||
if (!formData.value.userId) {
|
||||
message.warning('请选择要切换的用户')
|
||||
return
|
||||
message.warning('请选择要切换的用户');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
modalApi.setState({ loading: true })
|
||||
console.log('📡 开始调用切换账户接口...')
|
||||
|
||||
const result = await switchAccountApi(formData.value.userId)
|
||||
console.log('✅ 切换账户接口调用成功', result)
|
||||
|
||||
modalApi.setState({ loading: true });
|
||||
|
||||
const result = await switchAccountApi(formData.value.userId);
|
||||
|
||||
// 设置新的token
|
||||
const accessStore = useAccessStore()
|
||||
const accessStore = useAccessStore();
|
||||
accessStore.setTokens(
|
||||
result.accessToken,
|
||||
result.refreshToken,
|
||||
result.accessTokenExpiresAt,
|
||||
result.refreshTokenExpiresAt
|
||||
)
|
||||
|
||||
result.refreshTokenExpiresAt,
|
||||
);
|
||||
|
||||
// 重置权限检查状态,让路由守卫重新生成菜单和路由
|
||||
accessStore.setIsAccessChecked(false)
|
||||
|
||||
message.success('切换账户成功')
|
||||
modalApi.close()
|
||||
|
||||
accessStore.setIsAccessChecked(false);
|
||||
|
||||
message.success('切换账户成功');
|
||||
modalApi.close();
|
||||
|
||||
// 刷新页面
|
||||
window.location.reload()
|
||||
|
||||
window.location.reload();
|
||||
} catch (error: any) {
|
||||
console.error('切换账户失败:', error)
|
||||
message.error(error?.message || '切换账户失败')
|
||||
console.error('切换账户失败:', error);
|
||||
message.error(error?.message || '切换账户失败');
|
||||
} finally {
|
||||
modalApi.setState({ loading: false })
|
||||
modalApi.setState({ loading: false });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 创建模态框
|
||||
const [ModalComponent, modalApi] = useVbenModal({
|
||||
onCancel() {
|
||||
console.log('❌ 模态框取消按钮被点击')
|
||||
modalApi.close()
|
||||
modalApi.close();
|
||||
},
|
||||
onConfirm: async () => {
|
||||
console.log('🎯 模态框确认按钮被点击')
|
||||
await handleSwitchAccount()
|
||||
await handleSwitchAccount();
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
// 打开模态框
|
||||
function openModal() {
|
||||
modalApi.open()
|
||||
modalApi.open();
|
||||
// 重置表单数据
|
||||
formData.value = {
|
||||
companyId: undefined,
|
||||
userId: undefined,
|
||||
}
|
||||
selectedUser.value = null
|
||||
};
|
||||
selectedUser.value = null;
|
||||
// 加载公司列表
|
||||
loadCompanies(true)
|
||||
loadCompanies(true);
|
||||
}
|
||||
|
||||
// 关闭模态框
|
||||
function closeModal() {
|
||||
modalApi.close()
|
||||
modalApi.close();
|
||||
}
|
||||
|
||||
// 暴露方法
|
||||
defineExpose({
|
||||
open: openModal,
|
||||
close: closeModal,
|
||||
})
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ModalComponent
|
||||
title="切换账户"
|
||||
width="800px"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
layout="vertical"
|
||||
class="mt-4"
|
||||
>
|
||||
<ModalComponent title="切换账户" width="800px">
|
||||
<Form ref="formRef" :model="formData" layout="vertical" class="mt-4">
|
||||
<Form.Item label="选择公司" required>
|
||||
<Select
|
||||
v-model:value="formData.companyId"
|
||||
@@ -289,13 +292,11 @@ defineExpose({
|
||||
@popup-scroll="handleCompanyScroll"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
<div v-if="companyLoading" class="text-center py-4">
|
||||
<div v-if="companyLoading" class="py-4 text-center">
|
||||
<Spin size="small" />
|
||||
<div class="mt-2">加载中...</div>
|
||||
</div>
|
||||
<div v-else class="text-center py-4 text-gray-400">
|
||||
暂无数据
|
||||
</div>
|
||||
<div v-else class="py-4 text-center text-gray-400">暂无数据</div>
|
||||
</template>
|
||||
<Select.Option
|
||||
v-for="option in companyOptions"
|
||||
@@ -307,7 +308,7 @@ defineExpose({
|
||||
</Select.Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
|
||||
<Form.Item label="选择用户" required>
|
||||
<Select
|
||||
v-model:value="formData.userId"
|
||||
@@ -320,16 +321,17 @@ defineExpose({
|
||||
@popup-scroll="handleUserScroll"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
<div v-if="userLoading" class="text-center py-4">
|
||||
<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="text-center py-4 text-gray-400">
|
||||
<div
|
||||
v-else-if="!formData.companyId"
|
||||
class="py-4 text-center text-gray-400"
|
||||
>
|
||||
请先选择公司
|
||||
</div>
|
||||
<div v-else class="text-center py-4 text-gray-400">
|
||||
暂无数据
|
||||
</div>
|
||||
<div v-else class="py-4 text-center text-gray-400">暂无数据</div>
|
||||
</template>
|
||||
<Select.Option
|
||||
v-for="option in userOptions"
|
||||
@@ -342,7 +344,7 @@ defineExpose({
|
||||
</Select>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
|
||||
|
||||
<!-- 用户信息展示 -->
|
||||
<Card v-if="selectedUser" title="用户信息" class="mt-4">
|
||||
<div class="flex items-center space-x-4">
|
||||
@@ -350,11 +352,17 @@ defineExpose({
|
||||
{{ selectedUser.realName?.charAt(0) }}
|
||||
</Avatar>
|
||||
<div class="flex-1">
|
||||
<div class="font-medium text-lg">{{ selectedUser.realName }}</div>
|
||||
<div class="text-gray-500">登录账号: {{ selectedUser.loginCode }}</div>
|
||||
<div class="text-gray-500">手机号码: {{ selectedUser.phone || '-' }}</div>
|
||||
<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 class="text-gray-500">
|
||||
状态: {{ selectedUser.status === 1 ? '正常' : '禁用' }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
Reference in New Issue
Block a user