新增账户管理页

This commit is contained in:
ah--desktop
2025-10-15 23:32:34 +08:00
parent b8a7c5c18e
commit 9782570163
7 changed files with 1219 additions and 23 deletions
@@ -20,7 +20,7 @@ export async function createAccountApi(data: AccountDTO.CreateAccountDTO) {
* 分页查询账户(超管)
*/
export async function getAccountPageApi(params: PageQueryDTO<AccountDTO.AccountFilterDTO>) {
return requestClient.get<AccountVO.AccountPageVO>('/account/page', { params });
return requestClient.post<PageResultVO<AccountVO.AccountInfoVO>>('/account/page', params);
}
// ==================== 管理员接口 ====================
@@ -36,7 +36,7 @@ export async function createAccountForCompanyApi(data: AccountDTO.CreateAccountD
* 分页查询账户(管理员)
*/
export async function getAccountPageForCompanyApi(params: PageQueryDTO<AccountDTO.AccountFilterDTO>) {
return requestClient.get<AccountVO.AccountPageVO>('/account/company/page', { params });
return requestClient.post<PageResultVO<AccountVO.AccountInfoVO>>('/account/company/page', params);
}
// ==================== 通用接口 ====================
@@ -49,23 +49,25 @@ export namespace AccountDTO {
/** 账户过滤条件 */
export interface AccountFilterDTO {
/** 用户名(模糊查询) */
username?: string;
/** 真实姓名(模糊查询) */
/** 登录账号(模糊查询) */
loginCode?: string;
/** 用户姓名(模糊查询) */
realName?: string;
/** 邮箱(精确查询) */
email?: string;
/** 手机号(精确查询) */
/** 手机号码(精确查询) */
phone?: string;
/** 公司ID */
companyId?: number;
/** 部门ID */
departmentId?: number;
/** 职位ID */
/** 岗位ID */
positionId?: number;
/** 角色ID */
roleId?: number;
/** 状态 */
/** 角色 */
role?: string;
/** 状态(0-正常 1-删除 2-停用 3-冻结) */
status?: number;
/** 页码 */
pageNum?: number;
/** 每页大小 */
pageSize?: number;
}
}
@@ -39,15 +39,4 @@ export namespace AccountVO {
updateTime: string;
}
/** 账户分页结果 */
export interface AccountPageVO {
/** 总记录数 */
total: number;
/** 当前页码 */
current: number;
/** 每页大小 */
size: number;
/** 账户列表 */
records: AccountInfoVO[];
}
}
@@ -0,0 +1,264 @@
<script lang="ts" setup>
import { ref, reactive, watch } from 'vue';
import { Modal, Form, FormItem, Input, Select, SelectOption, Button, message } from 'ant-design-vue';
import { createIconifyIcon } from '@vben/icons';
import {
createAccountApi,
updateAccountApi,
type AccountVO,
type AccountDTO
} from '#/api/org/account';
// 创建图标组件
const SaveIcon = createIconifyIcon('lucide:save');
const CloseIcon = createIconifyIcon('lucide:x');
defineOptions({ name: 'AccountFormModal' });
// 响应式数据
const visible = ref(false);
const loading = ref(false);
const isEdit = ref(false);
const formRef = ref();
// 表单数据
const formData = reactive<AccountDTO.CreateAccountDTO>({
username: '',
password: '',
realName: '',
email: '',
phone: '',
companyId: undefined,
departmentId: undefined,
positionId: undefined,
roleId: undefined,
status: 1,
});
// 表单规则
const formRules = {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' },
{ min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur' },
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 20, message: '密码长度为6-20个字符', trigger: 'blur' },
],
realName: [
{ required: true, message: '请输入真实姓名', trigger: 'blur' },
],
email: [
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
],
phone: [
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
],
};
// 状态选项
const statusOptions = [
{ label: '禁用', value: 0 },
{ label: '正常', value: 1 },
];
// 方法
const open = (record?: AccountVO.AccountInfoVO) => {
visible.value = true;
isEdit.value = !!record;
if (record) {
Object.assign(formData, {
username: record.username,
realName: record.realName,
email: record.email,
phone: record.phone,
companyId: record.companyId,
departmentId: record.departmentId,
positionId: record.positionId,
roleId: record.roleId,
status: record.status,
});
} else {
resetForm();
}
};
const close = () => {
visible.value = false;
resetForm();
};
const resetForm = () => {
Object.assign(formData, {
username: '',
password: '',
realName: '',
email: '',
phone: '',
companyId: undefined,
departmentId: undefined,
positionId: undefined,
roleId: undefined,
status: 1,
});
formRef.value?.resetFields();
};
const handleSubmit = async () => {
try {
await formRef.value?.validate();
loading.value = true;
if (isEdit.value) {
const { password, ...updateData } = formData;
await updateAccountApi(formData.id!, updateData);
message.success('更新成功');
} else {
await createAccountApi(formData);
message.success('创建成功');
}
close();
emit('success');
} catch (error) {
console.error('提交失败:', error);
message.error(isEdit.value ? '更新失败' : '创建失败');
} finally {
loading.value = false;
}
};
// 事件
const emit = defineEmits<{
success: [];
}>();
// 监听弹窗关闭
watch(visible, (val) => {
if (!val) {
resetForm();
}
});
</script>
<template>
<Modal
v-model:open="visible"
:title="isEdit ? '编辑账户' : '新增账户'"
width="600px"
:confirm-loading="loading"
@cancel="close"
>
<Form
ref="formRef"
:model="formData"
:rules="formRules"
layout="vertical"
class="mt-4"
>
<div class="grid grid-cols-2 gap-4">
<FormItem label="用户名" name="username">
<Input
v-model:value="formData.username"
placeholder="请输入用户名"
:disabled="isEdit"
/>
</FormItem>
<FormItem label="密码" name="password" v-if="!isEdit">
<Input
v-model:value="formData.password"
type="password"
placeholder="请输入密码"
/>
</FormItem>
<FormItem label="真实姓名" name="realName">
<Input
v-model:value="formData.realName"
placeholder="请输入真实姓名"
/>
</FormItem>
<FormItem label="邮箱" name="email">
<Input
v-model:value="formData.email"
placeholder="请输入邮箱"
/>
</FormItem>
<FormItem label="手机号" name="phone">
<Input
v-model:value="formData.phone"
placeholder="请输入手机号"
/>
</FormItem>
<FormItem label="公司ID" name="companyId">
<Input
v-model:value="formData.companyId"
type="number"
placeholder="请输入公司ID"
/>
</FormItem>
<FormItem label="部门ID" name="departmentId">
<Input
v-model:value="formData.departmentId"
type="number"
placeholder="请输入部门ID"
/>
</FormItem>
<FormItem label="职位ID" name="positionId">
<Input
v-model:value="formData.positionId"
type="number"
placeholder="请输入职位ID"
/>
</FormItem>
<FormItem label="角色ID" name="roleId">
<Input
v-model:value="formData.roleId"
type="number"
placeholder="请输入角色ID"
/>
</FormItem>
<FormItem label="状态" name="status">
<Select
v-model:value="formData.status"
placeholder="请选择状态"
>
<SelectOption
v-for="option in statusOptions"
:key="option.value"
:value="option.value"
>
{{ option.label }}
</SelectOption>
</Select>
</FormItem>
</div>
</Form>
<template #footer>
<div class="flex justify-end space-x-2">
<Button @click="close" :icon="CloseIcon">
取消
</Button>
<Button
type="primary"
:loading="loading"
:icon="SaveIcon"
@click="handleSubmit"
>
{{ isEdit ? '更新' : '创建' }}
</Button>
</div>
</template>
</Modal>
</template>
@@ -0,0 +1,346 @@
<script lang="ts" setup>
import { ref, computed, h, onMounted } from 'vue';
import { Page } from '@vben/common-ui';
import { Button, message, Space, Tag } from 'ant-design-vue';
import { createIconifyIcon } from '@vben/icons';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import type { VxeGridProps } from '#/adapter/vxe-table';
import {
deleteAccountApi,
getAccountPageApi,
type AccountVO,
type AccountDTO
} from '#/api/org/account';
import type { PageQueryDTO } from '#/types/common';
import AccountFormModal from './components/AccountFormModal.vue';
// 创建图标组件
const PlusIcon = createIconifyIcon('lucide:plus');
const EditIcon = createIconifyIcon('lucide:edit');
const TrashIcon = createIconifyIcon('lucide:trash-2');
defineOptions({ name: 'AdminAccountManagement' });
// 响应式数据
const loading = ref(false);
const accountFormModalRef = ref();
// 搜索表单
const searchForm = ref({
loginCode: '',
realName: '',
phone: '',
companyId: undefined as number | undefined,
departmentId: undefined as number | undefined,
positionId: undefined as number | undefined,
role: '',
status: undefined as number | undefined,
});
// 状态选项
const statusOptions = [
{ label: '正常', value: 0 },
{ label: '删除', value: 1 },
{ label: '停用', value: 2 },
{ label: '冻结', value: 3 },
];
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
columns: [
{
type: 'seq' as const,
minWidth: 'auto',
title: '序号',
},
{
field: 'username',
title: '用户名',
minWidth: 120,
},
{
field: 'realName',
title: '真实姓名',
minWidth: 120,
},
{
field: 'email',
title: '邮箱',
minWidth: 150,
},
{
field: 'phone',
title: '手机号',
minWidth: 120,
},
{
field: 'companyName',
title: '公司',
minWidth: 150,
},
{
field: 'departmentName',
title: '部门',
minWidth: 120,
},
{
field: 'positionName',
title: '职位',
minWidth: 120,
},
{
field: 'roleName',
title: '角色',
minWidth: 100,
},
{
field: 'status',
title: '状态',
minWidth: 80,
cellRender: ({ row }) => {
const statusMap = {
0: { text: '正常', color: 'green' },
1: { text: '删除', color: 'red' },
2: { text: '停用', color: 'orange' },
3: { text: '冻结', color: 'purple' },
};
const status = statusMap[row.status as keyof typeof statusMap];
return h(Tag, { color: status.color }, () => status.text);
},
},
{
field: 'createTime',
title: '创建时间',
minWidth: 160,
},
{
title: '操作',
width: 120,
fixed: 'right' as const,
cellRender: ({ row }) => {
return h(Space, {}, () => [
h(Button, {
type: 'link',
size: 'small',
icon: h(EditIcon),
onClick: () => handleEdit(row),
}, () => '编辑'),
h(Button, {
type: 'link',
size: 'small',
danger: true,
icon: h(TrashIcon),
onClick: () => handleDelete(row),
}, () => '删除'),
]);
},
},
],
data: [],
loading: false,
height: 'auto',
border: true,
stripe: true,
showOverflow: 'tooltip',
showHeaderOverflow: 'tooltip',
resizable: true,
keepSource: true,
id: 'admin-account-grid',
};
// 表格实例
const { gridRef, getVxeGridInstance } = useVbenVxeGrid({
gridOptions,
});
// 计算属性
const hasSelected = computed(() => {
const instance = getVxeGridInstance();
return instance?.getCheckboxRecords().length > 0;
});
// 方法
const handleSearch = async () => {
await loadData();
};
const handleReset = () => {
searchForm.value = {
loginCode: '',
realName: '',
phone: '',
companyId: undefined,
departmentId: undefined,
positionId: undefined,
role: '',
status: undefined,
};
loadData();
};
const loadData = async () => {
try {
loading.value = true;
const instance = getVxeGridInstance();
instance?.commitProxy('query');
} catch (error) {
console.error('加载数据失败:', error);
} finally {
loading.value = false;
}
};
const handleAdd = () => {
accountFormModalRef.value?.open();
};
const handleEdit = (row: AccountVO.AccountInfoVO) => {
accountFormModalRef.value?.open(row);
};
const handleDelete = async (row: AccountVO.AccountInfoVO) => {
try {
await deleteAccountApi(row.id);
message.success('删除成功');
await loadData();
} catch (error) {
console.error('删除失败:', error);
message.error('删除失败');
}
};
const handleBatchDelete = async () => {
const instance = getVxeGridInstance();
const selectedRows = instance?.getCheckboxRecords();
if (!selectedRows?.length) {
message.warning('请选择要删除的数据');
return;
}
try {
const deletePromises = selectedRows.map((row: AccountVO.AccountInfoVO) =>
deleteAccountApi(row.id)
);
await Promise.all(deletePromises);
message.success('批量删除成功');
await loadData();
} catch (error) {
console.error('批量删除失败:', error);
message.error('批量删除失败');
}
};
// 表格代理配置
const gridProxyConfig = {
ajax: {
query: async ({ page, sorts, filters }: any) => {
const params: PageQueryDTO<AccountDTO.AccountFilterDTO> = {
pageNum: page.currentPage,
pageSize: page.pageSize,
...searchForm.value,
};
try {
const response = await getAccountPageApi(params);
return {
result: response.data.records,
page: {
total: response.data.total,
},
};
} catch (error) {
console.error('查询数据失败:', error);
return {
result: [],
page: {
total: 0,
},
};
}
},
},
};
// 初始化
onMounted(() => {
const instance = getVxeGridInstance();
instance?.setProxyConfig(gridProxyConfig);
loadData();
});
</script>
<template>
<Page>
<template #title>
<div class="flex items-center justify-between">
<h1 class="text-xl font-semibold">超管账户管理</h1>
<Space>
<Button @click="handleAdd" type="primary" :icon="PlusIcon">
新增账户
</Button>
<Button
v-if="hasSelected"
danger
@click="handleBatchDelete"
>
批量删除
</Button>
</Space>
</div>
</template>
<!-- 搜索表单 -->
<div class="mb-4 p-4 bg-gray-50 rounded-lg">
<div class="grid grid-cols-4 gap-4">
<div>
<label class="block text-sm font-medium mb-1">用户名</label>
<input
v-model="searchForm.loginCode"
placeholder="请输入用户名"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label class="block text-sm font-medium mb-1">真实姓名</label>
<input
v-model="searchForm.realName"
placeholder="请输入真实姓名"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label class="block text-sm font-medium mb-1">手机号</label>
<input
v-model="searchForm.phone"
placeholder="请输入手机号"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label class="block text-sm font-medium mb-1">状态</label>
<select
v-model="searchForm.status"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="">全部</option>
<option v-for="option in statusOptions" :key="option.value" :value="option.value">
{{ option.label }}
</option>
</select>
</div>
</div>
<div class="mt-4 flex justify-end space-x-2">
<Button @click="handleReset">重置</Button>
<Button type="primary" @click="handleSearch">搜索</Button>
</div>
</div>
<!-- 表格 -->
<VbenVxeGrid ref="gridRef" v-bind="gridOptions" />
<!-- 账户表单弹窗 -->
<AccountFormModal ref="accountFormModalRef" @success="loadData" />
</Page>
</template>
@@ -0,0 +1,256 @@
<script lang="ts" setup>
import { ref, reactive, watch } from 'vue';
import { Modal, Form, FormItem, Input, Select, SelectOption, Button, message } from 'ant-design-vue';
import { createIconifyIcon } from '@vben/icons';
import {
createAccountForCompanyApi,
updateAccountApi,
type AccountVO,
type AccountDTO
} from '#/api/org/account';
// 创建图标组件
const SaveIcon = createIconifyIcon('lucide:save');
const CloseIcon = createIconifyIcon('lucide:x');
defineOptions({ name: 'CompanyAccountFormModal' });
// 响应式数据
const visible = ref(false);
const loading = ref(false);
const isEdit = ref(false);
const formRef = ref();
// 表单数据
const formData = reactive<AccountDTO.CreateAccountDTO>({
username: '',
password: '',
realName: '',
email: '',
phone: '',
companyId: undefined,
departmentId: undefined,
positionId: undefined,
roleId: undefined,
status: 1,
});
// 表单规则
const formRules = {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' },
{ min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur' },
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 20, message: '密码长度为6-20个字符', trigger: 'blur' },
],
realName: [
{ required: true, message: '请输入真实姓名', trigger: 'blur' },
],
email: [
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
],
phone: [
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
],
};
// 状态选项
const statusOptions = [
{ label: '禁用', value: 0 },
{ label: '正常', value: 1 },
];
// 方法
const open = (record?: AccountVO.AccountInfoVO) => {
visible.value = true;
isEdit.value = !!record;
if (record) {
Object.assign(formData, {
username: record.username,
realName: record.realName,
email: record.email,
phone: record.phone,
companyId: record.companyId,
departmentId: record.departmentId,
positionId: record.positionId,
roleId: record.roleId,
status: record.status,
});
} else {
resetForm();
}
};
const close = () => {
visible.value = false;
resetForm();
};
const resetForm = () => {
Object.assign(formData, {
username: '',
password: '',
realName: '',
email: '',
phone: '',
companyId: undefined,
departmentId: undefined,
positionId: undefined,
roleId: undefined,
status: 1,
});
formRef.value?.resetFields();
};
const handleSubmit = async () => {
try {
await formRef.value?.validate();
loading.value = true;
if (isEdit.value) {
const { password, ...updateData } = formData;
await updateAccountApi(formData.id!, updateData);
message.success('更新成功');
} else {
await createAccountForCompanyApi(formData);
message.success('创建成功');
}
close();
emit('success');
} catch (error) {
console.error('提交失败:', error);
message.error(isEdit.value ? '更新失败' : '创建失败');
} finally {
loading.value = false;
}
};
// 事件
const emit = defineEmits<{
success: [];
}>();
// 监听弹窗关闭
watch(visible, (val) => {
if (!val) {
resetForm();
}
});
</script>
<template>
<Modal
v-model:open="visible"
:title="isEdit ? '编辑账户' : '新增账户'"
width="600px"
:confirm-loading="loading"
@cancel="close"
>
<Form
ref="formRef"
:model="formData"
:rules="formRules"
layout="vertical"
class="mt-4"
>
<div class="grid grid-cols-2 gap-4">
<FormItem label="用户名" name="username">
<Input
v-model:value="formData.username"
placeholder="请输入用户名"
:disabled="isEdit"
/>
</FormItem>
<FormItem label="密码" name="password" v-if="!isEdit">
<Input
v-model:value="formData.password"
type="password"
placeholder="请输入密码"
/>
</FormItem>
<FormItem label="真实姓名" name="realName">
<Input
v-model:value="formData.realName"
placeholder="请输入真实姓名"
/>
</FormItem>
<FormItem label="邮箱" name="email">
<Input
v-model:value="formData.email"
placeholder="请输入邮箱"
/>
</FormItem>
<FormItem label="手机号" name="phone">
<Input
v-model:value="formData.phone"
placeholder="请输入手机号"
/>
</FormItem>
<FormItem label="部门ID" name="departmentId">
<Input
v-model:value="formData.departmentId"
type="number"
placeholder="请输入部门ID"
/>
</FormItem>
<FormItem label="职位ID" name="positionId">
<Input
v-model:value="formData.positionId"
type="number"
placeholder="请输入职位ID"
/>
</FormItem>
<FormItem label="角色ID" name="roleId">
<Input
v-model:value="formData.roleId"
type="number"
placeholder="请输入角色ID"
/>
</FormItem>
<FormItem label="状态" name="status">
<Select
v-model:value="formData.status"
placeholder="请选择状态"
>
<SelectOption
v-for="option in statusOptions"
:key="option.value"
:value="option.value"
>
{{ option.label }}
</SelectOption>
</Select>
</FormItem>
</div>
</Form>
<template #footer>
<div class="flex justify-end space-x-2">
<Button @click="close" :icon="CloseIcon">
取消
</Button>
<Button
type="primary"
:loading="loading"
:icon="SaveIcon"
@click="handleSubmit"
>
{{ isEdit ? '更新' : '创建' }}
</Button>
</div>
</template>
</Modal>
</template>
@@ -0,0 +1,339 @@
<script lang="ts" setup>
import { ref, computed, h, onMounted } from 'vue';
import { Page } from '@vben/common-ui';
import { Button, message, Space, Tag } from 'ant-design-vue';
import { createIconifyIcon } from '@vben/icons';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import type { VxeGridProps } from '#/adapter/vxe-table';
import {
deleteAccountApi,
getAccountPageForCompanyApi,
type AccountVO,
type AccountDTO
} from '#/api/org/account';
import type { PageQueryDTO } from '#/types/common';
import AccountFormModal from './components/AccountFormModal.vue';
// 创建图标组件
const PlusIcon = createIconifyIcon('lucide:plus');
const EditIcon = createIconifyIcon('lucide:edit');
const TrashIcon = createIconifyIcon('lucide:trash-2');
defineOptions({ name: 'CompanyAccountManagement' });
// 响应式数据
const loading = ref(false);
const accountFormModalRef = ref();
// 搜索表单
const searchForm = ref({
loginCode: '',
realName: '',
phone: '',
departmentId: undefined as number | undefined,
positionId: undefined as number | undefined,
role: '',
status: undefined as number | undefined,
});
// 状态选项
const statusOptions = [
{ label: '正常', value: 0 },
{ label: '删除', value: 1 },
{ label: '停用', value: 2 },
{ label: '冻结', value: 3 },
];
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
columns: [
{
type: 'seq' as const,
minWidth: 'auto',
title: '序号',
},
{
field: 'username',
title: '用户名',
minWidth: 120,
},
{
field: 'realName',
title: '真实姓名',
minWidth: 120,
},
{
field: 'email',
title: '邮箱',
minWidth: 150,
},
{
field: 'phone',
title: '手机号',
minWidth: 120,
},
{
field: 'departmentName',
title: '部门',
minWidth: 120,
},
{
field: 'positionName',
title: '职位',
minWidth: 120,
},
{
field: 'roleName',
title: '角色',
minWidth: 100,
},
{
field: 'status',
title: '状态',
minWidth: 80,
cellRender: ({ row }) => {
const statusMap = {
0: { text: '正常', color: 'green' },
1: { text: '删除', color: 'red' },
2: { text: '停用', color: 'orange' },
3: { text: '冻结', color: 'purple' },
};
const status = statusMap[row.status as keyof typeof statusMap];
return h(Tag, { color: status.color }, () => status.text);
},
},
{
field: 'createTime',
title: '创建时间',
minWidth: 160,
},
{
title: '操作',
width: 120,
fixed: 'right' as const,
cellRender: ({ row }) => {
return h(Space, {}, () => [
h(Button, {
type: 'link',
size: 'small',
icon: h(EditIcon),
onClick: () => handleEdit(row),
}, () => '编辑'),
h(Button, {
type: 'link',
size: 'small',
danger: true,
icon: h(TrashIcon),
onClick: () => handleDelete(row),
}, () => '删除'),
]);
},
},
],
data: [],
loading: false,
height: 'auto',
border: true,
stripe: true,
showOverflow: 'tooltip',
showHeaderOverflow: 'tooltip',
resizable: true,
keepSource: true,
id: 'company-account-grid',
};
// 表格实例
const { gridRef, getVxeGridInstance } = useVbenVxeGrid({
gridOptions,
});
// 计算属性
const hasSelected = computed(() => {
const instance = getVxeGridInstance();
return instance?.getCheckboxRecords().length > 0;
});
// 方法
const handleSearch = async () => {
await loadData();
};
const handleReset = () => {
searchForm.value = {
loginCode: '',
realName: '',
phone: '',
departmentId: undefined,
positionId: undefined,
role: '',
status: undefined,
};
loadData();
};
const loadData = async () => {
try {
loading.value = true;
const instance = getVxeGridInstance();
instance?.commitProxy('query');
} catch (error) {
console.error('加载数据失败:', error);
} finally {
loading.value = false;
}
};
const handleAdd = () => {
accountFormModalRef.value?.open();
};
const handleEdit = (row: AccountVO.AccountInfoVO) => {
accountFormModalRef.value?.open(row);
};
const handleDelete = async (row: AccountVO.AccountInfoVO) => {
try {
await deleteAccountApi(row.id);
message.success('删除成功');
await loadData();
} catch (error) {
console.error('删除失败:', error);
message.error('删除失败');
}
};
const handleBatchDelete = async () => {
const instance = getVxeGridInstance();
const selectedRows = instance?.getCheckboxRecords();
if (!selectedRows?.length) {
message.warning('请选择要删除的数据');
return;
}
try {
const deletePromises = selectedRows.map((row: AccountVO.AccountInfoVO) =>
deleteAccountApi(row.id)
);
await Promise.all(deletePromises);
message.success('批量删除成功');
await loadData();
} catch (error) {
console.error('批量删除失败:', error);
message.error('批量删除失败');
}
};
// 表格代理配置
const gridProxyConfig = {
ajax: {
query: async ({ page, sorts, filters }: any) => {
const params: PageQueryDTO<AccountDTO.AccountFilterDTO> = {
pageNum: page.currentPage,
pageSize: page.pageSize,
...searchForm.value,
};
try {
const response = await getAccountPageForCompanyApi(params);
return {
result: response.data.records,
page: {
total: response.data.total,
},
};
} catch (error) {
console.error('查询数据失败:', error);
return {
result: [],
page: {
total: 0,
},
};
}
},
},
};
// 初始化
onMounted(() => {
const instance = getVxeGridInstance();
instance?.setProxyConfig(gridProxyConfig);
loadData();
});
</script>
<template>
<Page>
<template #title>
<div class="flex items-center justify-between">
<h1 class="text-xl font-semibold">公司账户管理</h1>
<Space>
<Button @click="handleAdd" type="primary" :icon="PlusIcon">
新增账户
</Button>
<Button
v-if="hasSelected"
danger
@click="handleBatchDelete"
>
批量删除
</Button>
</Space>
</div>
</template>
<!-- 搜索表单 -->
<div class="mb-4 p-4 bg-gray-50 rounded-lg">
<div class="grid grid-cols-4 gap-4">
<div>
<label class="block text-sm font-medium mb-1">用户名</label>
<input
v-model="searchForm.loginCode"
placeholder="请输入用户名"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label class="block text-sm font-medium mb-1">真实姓名</label>
<input
v-model="searchForm.realName"
placeholder="请输入真实姓名"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label class="block text-sm font-medium mb-1">手机号</label>
<input
v-model="searchForm.phone"
placeholder="请输入手机号"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div>
<label class="block text-sm font-medium mb-1">状态</label>
<select
v-model="searchForm.status"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="">全部</option>
<option v-for="option in statusOptions" :key="option.value" :value="option.value">
{{ option.label }}
</option>
</select>
</div>
</div>
<div class="mt-4 flex justify-end space-x-2">
<Button @click="handleReset">重置</Button>
<Button type="primary" @click="handleSearch">搜索</Button>
</div>
</div>
<!-- 表格 -->
<VbenVxeGrid ref="gridRef" v-bind="gridOptions" />
<!-- 账户表单弹窗 -->
<AccountFormModal ref="accountFormModalRef" @success="loadData" />
</Page>
</template>