添加用户管理

This commit is contained in:
xie7654
2025-07-01 15:08:26 +08:00
parent 57429ba359
commit 36b6a8437b
19 changed files with 686 additions and 125 deletions

View File

@@ -43,7 +43,12 @@ export function useColumns(
},
{
field: 'auth_code',
title: $t('system.menu.authCode'),
title: $t('system.menu.auth_code'),
width: 200,
},
{
field: 'sort',
title: $t('system.menu.sort'),
width: 200,
},
{

View File

@@ -254,6 +254,17 @@ const schema: VbenFormSchema[] = [
fieldName: 'auth_code',
label: $t('system.menu.authCode'),
},
{
component: 'InputNumber',
dependencies: {
rules: () => {
return 'required';
},
triggerFields: ['type'],
},
fieldName: 'sort',
label: $t('system.menu.sort'),
},
{
component: 'RadioGroup',
componentProps: {

View File

@@ -0,0 +1,230 @@
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/models/system/user';
import { z } from '#/adapter/form';
import { getDeptList, getRoleList } from '#/api/system';
import { $t } from '#/locales';
import { SystemPostModel } from '#/models/system/post';
import { format_datetime } from '#/utils/date';
const systemPost = new SystemPostModel();
/**
* 获取编辑表单的字段配置
*/
export function useSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'username',
label: '用户名',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['用户名']))
.max(100, $t('ui.formRules.maxLength', ['用户名', 100])),
},
{
component: 'Input',
fieldName: 'email',
label: '电子邮件地址',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['电子邮件地址']))
.max(100, $t('ui.formRules.maxLength', ['电子邮件地址', 100])),
},
{
component: 'ApiTreeSelect',
componentProps: {
allowClear: true,
multiple: true, // 允许多选
api: getDeptList,
class: 'w-full',
resultField: 'items',
labelField: 'name',
valueField: 'id',
childrenField: 'children',
},
fieldName: 'dept',
label: $t('system.dept.name'),
},
{
component: 'ApiSelect',
componentProps: {
allowClear: true,
mode: 'multiple', // 允许多选
api: getRoleList,
class: 'w-full',
resultField: 'items',
labelField: 'name',
valueField: 'id',
},
fieldName: 'role',
label: $t('system.role.name'),
},
{
component: 'ApiSelect',
componentProps: {
allowClear: true,
mode: 'multiple', // 允许多选
api: () => systemPost.list(),
class: 'w-full',
resultField: 'items',
labelField: 'name',
valueField: 'id',
childrenField: 'children',
},
fieldName: 'post',
label: $t('system.post.name'),
},
{
component: 'Input',
fieldName: 'mobile',
label: '手机号',
},
{
component: 'Input',
fieldName: 'nickname',
label: '昵称',
},
{
component: 'InputPassword',
fieldName: 'password',
label: '密码',
},
{
component: 'Input',
fieldName: 'city',
label: '城市',
},
{
component: 'Input',
fieldName: 'province',
label: '省份',
},
{
component: 'Input',
fieldName: 'country',
label: '国家',
},
{
component: 'RadioGroup',
componentProps: {
buttonStyle: 'solid',
options: [
{ label: '开启', value: 1 },
{ label: '关闭', value: 0 },
],
optionType: 'button',
},
defaultValue: 1,
fieldName: 'status',
label: $t('system.status'),
},
{
component: 'Input',
fieldName: 'remark',
label: $t('system.remark'),
},
];
}
/**
* 获取表格列配置
* @description 使用函数的形式返回列数据而不是直接export一个Array常量是为了响应语言切换时重新翻译表头
* @param onActionClick 表格操作按钮点击事件
*/
export function useColumns(
onActionClick?: OnActionClickFn<SystemUserApi.SystemUser>,
): VxeTableGridOptions<SystemUserApi.SystemUser>['columns'] {
return [
{
field: 'id',
title: 'ID',
},
{
field: 'username',
title: '用户名',
width: 100,
},
{
field: 'is_superuser',
title: '超级用户状态',
},
{
field: 'date_joined',
title: '加入日期',
width: 150,
formatter: ({ cellValue }) => format_datetime(cellValue),
},
{
field: 'mobile',
title: 'mobile',
},
{
cellRender: {
name: 'CellTag',
},
field: 'status',
title: $t('system.status'),
width: 100,
},
{
field: 'login_ip',
title: 'login ip',
width: 150,
},
{
field: 'last_login',
title: '最后登录',
width: 150,
formatter: ({ cellValue }) => format_datetime(cellValue),
},
{
field: 'remark',
title: $t('system.remark'),
},
{
field: 'creator',
title: $t('system.creator'),
width: 80,
},
{
field: 'modifier',
title: $t('system.modifier'),
width: 80,
},
{
field: 'update_time',
title: $t('system.updateTime'),
width: 150,
formatter: ({ cellValue }) => format_datetime(cellValue),
},
{
field: 'create_time',
title: $t('system.createTime'),
width: 150,
formatter: ({ cellValue }) => format_datetime(cellValue),
},
{
align: 'center',
cellRender: {
attrs: {
nameField: 'name',
nameTitle: $t('system.user.name'),
onClick: onActionClick,
},
name: 'CellOperation',
options: ['edit', 'delete'],
},
field: 'action',
fixed: 'right',
title: '操作',
width: 120,
},
];
}

View File

@@ -0,0 +1,132 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/models/system/user';
import { Page, useVbenModal } from '@vben/common-ui';
import { Plus } from '@vben/icons';
import { Button, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { $t } from '#/locales';
import { SystemUserModel } from '#/models/system/user';
import { useColumns } from './data';
import Form from './modules/form.vue';
const formModel = new SystemUserModel();
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
/**
* 编辑用户数据
*/
function onEdit(row: SystemUserApi.SystemUser) {
formModalApi.setData(row).open();
}
/**
* 创建新用户数据
*/
function onCreate() {
formModalApi.setData(null).open();
}
/**
* 删除用户数据
*/
function onDelete(row: SystemUserApi.SystemUser) {
const hideLoading = message.loading({
content: '删除用户数据',
duration: 0,
key: 'action_process_msg',
});
formModel
.delete(row.id)
.then(() => {
message.success({
content: '删除成功',
key: 'action_process_msg',
});
refreshGrid();
})
.catch(() => {
hideLoading();
});
}
/**
* 表格操作按钮的回调函数
*/
function onActionClick({
code,
row,
}: OnActionClickParams<SystemUserApi.SystemUser>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({
gridEvents: {},
gridOptions: {
columns: useColumns(onActionClick),
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: true,
},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await formModel.list({
page: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
toolbarConfig: {
custom: true,
export: false,
refresh: { code: 'query' },
zoom: true,
},
} as VxeTableGridOptions,
});
/**
* 刷新表格
*/
function refreshGrid() {
gridApi.query();
}
</script>
<template>
<Page auto-content-height>
<FormModal @success="refreshGrid" />
<Grid table-title="用户数据">
<template #toolbar-tools>
<Button type="primary" @click="onCreate">
<Plus class="size-5" />
{{ $t('ui.actionTitle.create', [$t('system.user.name')]) }}
</Button>
</template>
</Grid>
</Page>
</template>

View File

@@ -0,0 +1,85 @@
<script lang="ts" setup>
import type { SystemUserApi } from '#/models/system/user';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { Button } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { $t } from '#/locales';
import { SystemUserModel } from '#/models/system/user';
import { useSchema } from '../data';
const emit = defineEmits(['success']);
const formModel = new SystemUserModel();
const formData = ref<SystemUserApi.SystemUser>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', [$t('system.user.name')])
: $t('ui.actionTitle.create', [$t('system.user.name')]);
});
const [Form, formApi] = useVbenForm({
layout: 'horizontal',
commonConfig: {
colon: true,
formItemClass: 'col-span-2 md:col-span-1',
},
wrapperClass: 'grid-cols-2 gap-x-4',
schema: useSchema(),
showDefaultActions: false,
});
function resetForm() {
formApi.resetForm();
formApi.setValues(formData.value || {});
}
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (valid) {
modalApi.lock();
const data = await formApi.getValues();
try {
await (formData.value?.id
? formModel.update(formData.value.id, data)
: formModel.create(data));
await modalApi.close();
emit('success');
} finally {
modalApi.lock(false);
}
}
},
onOpenChange(isOpen) {
if (isOpen) {
const data = modalApi.getData<SystemUserApi.SystemUser>();
if (data) {
formData.value = data;
formApi.setValues(formData.value);
}
}
},
});
</script>
<template>
<Modal :title="getTitle" class="w-full max-w-[800px]">
<Form />
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
</div>
</template>
</Modal>
</template>
<style lang="css" scoped></style>