Files
aiot-platform-ui/apps/web-antd/src/views/system/user/index.vue

301 lines
8.4 KiB
Vue
Raw Normal View History

2025-04-03 09:02:44 +08:00
<script lang="ts" setup>
2025-05-15 14:47:19 +08:00
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemDeptApi } from '#/api/system/dept';
2025-04-22 11:25:11 +08:00
import type { SystemUserApi } from '#/api/system/user';
import { ref } from 'vue';
2025-04-03 09:02:44 +08:00
import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui';
2025-09-05 12:00:24 +08:00
import { DICT_TYPE } from '@vben/constants';
import { getDictLabel } from '@vben/hooks';
import { downloadFileFromBlobPart, isEmpty } from '@vben/utils';
2025-04-22 11:25:11 +08:00
2025-06-24 17:35:43 +08:00
import { Card, message } from 'ant-design-vue';
2025-04-03 09:02:44 +08:00
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
2025-04-22 11:25:11 +08:00
import {
deleteUser,
deleteUserList,
2025-04-22 11:25:11 +08:00
exportUser,
getUserPage,
updateUserStatus,
} from '#/api/system/user';
import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
2025-04-22 11:25:11 +08:00
import AssignRoleForm from './modules/assign-role-form.vue';
import DeptTree from './modules/dept-tree.vue';
2025-04-22 11:25:11 +08:00
import Form from './modules/form.vue';
import ImportForm from './modules/import-form.vue';
import ResetPasswordForm from './modules/reset-password-form.vue';
2025-04-03 09:02:44 +08:00
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
const [ResetPasswordModal, resetPasswordModalApi] = useVbenModal({
connectedComponent: ResetPasswordForm,
destroyOnClose: true,
});
const [AssignRoleModal, assignRoleModalApi] = useVbenModal({
connectedComponent: AssignRoleForm,
destroyOnClose: true,
});
const [ImportModal, importModalApi] = useVbenModal({
connectedComponent: ImportForm,
destroyOnClose: true,
});
2025-04-03 09:02:44 +08:00
/** 刷新表格 */
function handleRefresh() {
2025-04-03 09:02:44 +08:00
gridApi.query();
}
/** 导出表格 */
async function handleExport() {
const data = await exportUser(await gridApi.formApi.getValues());
downloadFileFromBlobPart({ fileName: '用户.xls', source: data });
}
/** 选择部门 */
const searchDeptId = ref<number | undefined>(undefined);
async function handleDeptSelect(dept: SystemDeptApi.Dept) {
searchDeptId.value = dept.id;
handleRefresh();
}
2025-04-03 09:02:44 +08:00
/** 创建用户 */
2025-05-20 11:23:02 +08:00
function handleCreate() {
2025-04-03 09:02:44 +08:00
formModalApi.setData(null).open();
}
/** 导入用户 */
function handleImport() {
importModalApi.open();
}
2025-04-03 09:02:44 +08:00
/** 编辑用户 */
2025-05-20 11:23:02 +08:00
function handleEdit(row: SystemUserApi.User) {
2025-04-03 09:02:44 +08:00
formModalApi.setData(row).open();
}
/** 删除用户 */
2025-05-20 11:23:02 +08:00
async function handleDelete(row: SystemUserApi.User) {
const hideLoading = message.loading({
2025-04-03 09:02:44 +08:00
content: $t('ui.actionMessage.deleting', [row.username]),
duration: 0,
2025-04-03 09:02:44 +08:00
});
2025-05-20 11:23:02 +08:00
try {
await deleteUser(row.id as number);
message.success($t('ui.actionMessage.deleteSuccess', [row.username]));
handleRefresh();
} finally {
hideLoading();
}
}
/** 批量删除用户 */
async function handleDeleteBatch() {
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
const hideLoading = message.loading({
content: $t('ui.actionMessage.deletingBatch'),
duration: 0,
});
try {
await deleteUserList(checkedIds.value);
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
2025-05-20 11:23:02 +08:00
} finally {
hideLoading();
}
2025-04-03 09:02:44 +08:00
}
const checkedIds = ref<number[]>([]);
function handleRowCheckboxChange({
records,
}: {
records: SystemUserApi.User[];
}) {
checkedIds.value = records.map((item) => item.id!);
}
/** 重置密码 */
2025-05-20 11:23:02 +08:00
function handleResetPassword(row: SystemUserApi.User) {
resetPasswordModalApi.setData(row).open();
}
/** 分配角色 */
2025-05-20 11:23:02 +08:00
function handleAssignRole(row: SystemUserApi.User) {
assignRoleModalApi.setData(row).open();
}
/** 更新用户状态 */
2025-05-20 11:23:02 +08:00
async function handleStatusChange(
2025-04-22 11:25:11 +08:00
newStatus: number,
2025-04-22 22:10:33 +08:00
row: SystemUserApi.User,
2025-04-22 11:25:11 +08:00
): Promise<boolean | undefined> {
return new Promise((resolve, reject) => {
confirm({
content: `你要将${row.username}的状态切换为【${getDictLabel(DICT_TYPE.COMMON_STATUS, newStatus)}】吗?`,
})
.then(async () => {
// 更新用户状态
const res = await updateUserStatus(row.id as number, newStatus);
if (res) {
// 提示并返回成功
message.success($t('ui.actionMessage.operationSuccess'));
resolve(true);
} else {
reject(new Error('更新失败'));
}
})
.catch(() => {
reject(new Error('取消操作'));
});
});
}
2025-04-03 09:02:44 +08:00
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
2025-05-20 11:23:02 +08:00
columns: useGridColumns(handleStatusChange),
2025-04-03 09:02:44 +08:00
height: 'auto',
keepSource: true,
2025-04-03 09:02:44 +08:00
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getUserPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
deptId: searchDeptId.value,
2025-04-03 09:02:44 +08:00
});
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
2025-04-03 09:02:44 +08:00
},
toolbarConfig: {
2025-07-19 17:15:39 +08:00
refresh: true,
2025-04-03 09:02:44 +08:00
search: true,
},
2025-04-22 22:10:33 +08:00
} as VxeTableGridOptions<SystemUserApi.User>,
gridEvents: {
checkboxAll: handleRowCheckboxChange,
checkboxChange: handleRowCheckboxChange,
},
2025-04-03 09:02:44 +08:00
});
</script>
<template>
<Page auto-content-height>
<template #doc>
<DocAlert title="用户体系" url="https://doc.iocoder.cn/user-center/" />
<DocAlert title="三方登陆" url="https://doc.iocoder.cn/social-user/" />
<DocAlert
title="Excel 导入导出"
url="https://doc.iocoder.cn/excel-import-and-export/"
/>
</template>
<FormModal @success="handleRefresh" />
<ResetPasswordModal @success="handleRefresh" />
<AssignRoleModal @success="handleRefresh" />
<ImportModal @success="handleRefresh" />
<div class="flex h-full w-full">
<!-- 左侧部门树 -->
2025-06-24 17:35:43 +08:00
<Card class="mr-4 h-full w-1/6">
<DeptTree @select="handleDeptSelect" />
2025-06-24 17:35:43 +08:00
</Card>
<!-- 右侧用户列表 -->
<div class="w-5/6">
<Grid table-title="用户列表">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['用户']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['system:user:create'],
onClick: handleCreate,
},
{
label: $t('ui.actionTitle.export'),
type: 'primary',
icon: ACTION_ICON.DOWNLOAD,
auth: ['system:user:export'],
onClick: handleExport,
},
{
label: $t('ui.actionTitle.import', ['用户']),
type: 'primary',
icon: ACTION_ICON.UPLOAD,
auth: ['system:user:import'],
onClick: handleImport,
},
{
label: $t('ui.actionTitle.deleteBatch'),
type: 'primary',
danger: true,
icon: ACTION_ICON.DELETE,
disabled: isEmpty(checkedIds),
auth: ['system:user:delete'],
onClick: handleDeleteBatch,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['system:user:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['system:user:delete'],
popConfirm: {
2025-06-26 14:32:49 +08:00
title: $t('ui.actionMessage.deleteConfirm', [row.username]),
confirm: handleDelete.bind(null, row),
},
},
]"
:drop-down-actions="[
{
label: '分配角色',
type: 'link',
auth: ['system:permission:assign-user-role'],
onClick: handleAssignRole.bind(null, row),
},
{
label: '重置密码',
type: 'link',
auth: ['system:user:update-password'],
onClick: handleResetPassword.bind(null, row),
},
]"
/>
</template>
</Grid>
</div>
</div>
2025-04-03 09:02:44 +08:00
</Page>
</template>