Files
ecom-dmb/ecom-dmb-frontend/apps/web-antd/src/views/org/account/index.vue
T

427 lines
11 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { AccountDTO, AccountVO } from '#/api/company/account';
import type { PageQueryDTO } from '#/types/common';
import { computed, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { Button, message, Popconfirm, Space, Tag } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
admin_deleteAccountApi,
admin_getAccountPageApi,
} from '#/api/company/account';
import { PermissionGate } from '#/components/permission';
import {
syncVxeGridColumns,
useActionColumnVisible,
} from '#/composables/use-vxe-action-column';
import AccountFormModal from './components/AccountFormModal.vue';
defineOptions({ name: 'CompanyAccountManagement' });
// 创建图标组件
const PlusIcon = createIconifyIcon('lucide:plus');
// 响应式数据
const loading = ref(false);
const accountFormModalRef = ref();
// 搜索表单
const searchForm = ref({
loginCode: undefined as string | undefined,
realName: undefined as string | undefined,
phone: undefined as string | undefined,
companyId: undefined as number | undefined,
departmentId: undefined as number | undefined,
positionId: undefined as number | undefined,
role: undefined as string | undefined,
status: undefined as number | undefined,
});
// 公司成员角色选项(org_company_member.role)
const companyRoleOptions = [
{ label: '公司拥有者', value: 'owner' },
{ label: '成员', value: 'member' },
];
// 状态选项(sys_user.status:0=停用,1=正常)
const statusOptions = [
{ label: '正常', value: 1 },
{ label: '停用', value: 0 },
];
const accountActionCodes = [
'org:account:create',
'org:account:update',
'org:account:delete',
];
const showActionColumn = useActionColumnVisible(accountActionCodes);
// 搜索表单配置
const gridReloadRef = ref<(() => void) | null>(null);
const formOptions = computed(() => ({
schemas: [
{
fieldName: 'loginCode',
label: '登录账号',
component: 'Input',
componentProps: {
placeholder: '请输入登录账号(回车搜索)',
onPressEnter: () => gridReloadRef.value?.(),
},
},
{
fieldName: 'realName',
label: '真实姓名',
component: 'Input',
componentProps: {
placeholder: '请输入真实姓名(回车搜索)',
onPressEnter: () => gridReloadRef.value?.(),
},
},
{
fieldName: 'phone',
label: '手机号',
component: 'Input',
componentProps: {
placeholder: '请输入手机号(回车搜索)',
onPressEnter: () => gridReloadRef.value?.(),
},
},
{
fieldName: 'role',
label: '成员角色',
component: 'Select',
componentProps: {
options: companyRoleOptions,
placeholder: '请选择成员角色',
onChange: () => gridReloadRef.value?.(),
},
},
{
fieldName: 'status',
label: '状态',
component: 'Select',
componentProps: {
options: statusOptions,
placeholder: '请选择状态',
onChange: () => gridReloadRef.value?.(),
},
},
],
showDefaultActions: true,
collapsed: true,
collapsedRows: 1,
}));
const accountTableBaseColumns: VxeGridProps<any>['columns'] = [
{
type: 'seq' as const,
minWidth: 'auto',
title: '序号',
},
{
field: 'loginCode',
title: '登录账号',
minWidth: 120,
},
{
field: 'realName',
title: '真实姓名',
minWidth: 120,
},
{
field: 'phone',
title: '手机号',
minWidth: 120,
},
{
field: 'companyName',
title: '公司',
minWidth: 150,
},
{
field: 'departmentName',
title: '部门',
minWidth: 120,
},
{
field: 'positionName',
title: '职位',
minWidth: 120,
},
{
field: 'role',
title: '成员角色',
minWidth: 110,
slots: { default: 'role' },
},
{
field: 'status',
title: '状态',
minWidth: 80,
slots: { default: 'status' },
},
{
field: 'createTime',
title: '创建时间',
minWidth: 160,
},
];
const accountActionColumn: NonNullable<VxeGridProps<any>['columns']>[number] = {
title: '操作',
width: 120,
fixed: 'right' as const,
slots: { default: 'action' },
};
const accountTableColumns = computed(() =>
showActionColumn.value
? [...(accountTableBaseColumns ?? []), accountActionColumn]
: (accountTableBaseColumns ?? []),
);
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
columns: accountTableColumns.value,
height: 'auto',
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
toolbarConfig: {
custom: true,
refresh: true,
zoom: true,
},
data: [],
pagerConfig: {
enabled: true,
pageSizes: [10, 20, 50, 100],
layouts: [
'PrevJump',
'PrevPage',
'Number',
'NextPage',
'NextJump',
'Sizes',
'FullJump',
'Total',
] as (
| 'FullJump'
| 'NextJump'
| 'NextPage'
| 'Number'
| 'PrevJump'
| 'PrevPage'
| 'Sizes'
| 'Total'
)[],
},
// 配置远程加载
proxyConfig: {
ajax: {
query: async ({ page }) => {
try {
loading.value = true;
const params: PageQueryDTO<AccountDTO.AccountFilterDTO> = {
page: page.currentPage,
pageSize: page.pageSize,
...searchForm.value,
};
const response = await admin_getAccountPageApi(params);
return {
items: response.records,
total: response.total,
};
} catch (error: any) {
console.error('查询数据失败:', error);
const errorMessage =
error?.response?.data?.message || error?.message || '查询数据失败';
message.error(errorMessage);
return {
items: [],
total: 0,
};
} finally {
loading.value = false;
}
},
},
},
};
// 表格实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
formOptions: formOptions.value,
showSearchForm: true,
tableTitle: '账户管理',
});
gridReloadRef.value = () => gridApi.reload();
syncVxeGridColumns(gridApi, accountTableColumns);
// 计算属性
const hasSelected = computed(() => {
const vxeTable = (gridApi as any).grid;
return vxeTable?.getCheckboxRecords?.()?.length > 0;
});
// 刷新表格数据
function refreshTable() {
if (gridApi) {
gridApi.reload();
}
}
const handleAdd = () => {
accountFormModalRef.value?.open();
};
const handleEdit = (row: AccountVO.AccountInfoVO) => {
accountFormModalRef.value?.open(row);
};
const handleDelete = async (row: AccountVO.AccountInfoVO) => {
try {
await admin_deleteAccountApi(row.id);
message.success('删除成功');
refreshTable();
} catch (error: any) {
console.error('删除失败:', error);
const errorMessage =
error?.response?.data?.message || error?.message || '删除失败';
message.error(errorMessage);
}
};
const handleBatchDelete = async () => {
const vxeTable = (gridApi as any).grid;
const selectedRows = vxeTable?.getCheckboxRecords?.();
if (!selectedRows?.length) {
message.warning('请选择要删除的数据');
return;
}
try {
const deletePromises = selectedRows.map((row: AccountVO.AccountInfoVO) =>
admin_deleteAccountApi(row.id),
);
await Promise.all(deletePromises);
message.success(`成功删除 ${selectedRows.length} 条记录`);
refreshTable();
} catch (error: any) {
console.error('批量删除失败:', error);
const errorMessage =
error?.response?.data?.message || error?.message || '批量删除失败';
message.error(errorMessage);
}
};
// 获取公司成员角色文本
const getRoleText = (role?: string, roleName?: string) => {
if (roleName) return roleName;
const roleMap: Record<string, string> = {
owner: '公司拥有者',
member: '成员',
};
return role ? roleMap[role] || role : '-';
};
// 获取状态文本
const getStatusText = (status: number, statusName?: string) => {
if (statusName) return statusName;
const statusMap: Record<number, string> = {
0: '停用',
1: '正常',
};
return statusMap[status] || '未知';
};
// 获取状态颜色
const getStatusColor = (status: number) => {
const colorMap: Record<number, string> = {
0: 'orange',
1: 'green',
};
return colorMap[status] || 'default';
};
// 处理模态框成功回调
function handleModalSuccess() {
refreshTable();
}
</script>
<template>
<Page title="账户管理" auto-content-height>
<template #description>
<div class="text-foreground/80 mt-2">
管理公司账户信息,支持账户的增删改查操作
</div>
</template>
<!-- VbenVxeGrid 表格 -->
<Grid :loading="loading">
<!-- 工具栏工具 -->
<template #toolbar-tools>
<Space>
<PermissionGate code="org:account:create">
<Button type="primary" @click="handleAdd">
<PlusIcon />
添加账户
</Button>
</PermissionGate>
<PermissionGate code="org:account:delete">
<Button v-if="hasSelected" danger @click="handleBatchDelete">
批量删除
</Button>
</PermissionGate>
</Space>
</template>
<!-- 成员角色列 -->
<template #role="{ row }">
<Tag :color="row.role === 'owner' ? 'gold' : 'blue'">
{{ getRoleText(row.role, row.roleName) }}
</Tag>
</template>
<!-- 状态列 -->
<template #status="{ row }">
<Tag :color="getStatusColor(row.status)">
{{ getStatusText(row.status, row.statusName) }}
</Tag>
</template>
<!-- 操作列 -->
<template v-if="showActionColumn" #action="{ row }">
<Space>
<PermissionGate code="org:account:update">
<Button size="small" @click="handleEdit(row)"> 编辑 </Button>
</PermissionGate>
<PermissionGate code="org:account:delete">
<Popconfirm
title="确定要删除这个账户吗?"
@confirm="handleDelete(row)"
>
<Button size="small" danger> 删除 </Button>
</Popconfirm>
</PermissionGate>
</Space>
</template>
</Grid>
<!-- 账户表单弹窗 -->
<AccountFormModal ref="accountFormModalRef" @success="handleModalSuccess" />
</Page>
</template>