添加用户管理

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

@@ -129,7 +129,7 @@ watch(
:avatar
:menus
:text="userStore.userInfo?.realName"
description="ann.vben@gmail.com"
:description="userStore.userInfo?.email"
tag-text="Pro"
@logout="handleLogout"
/>

View File

@@ -28,6 +28,7 @@
"path": "Route Path",
"component": "Component",
"status": "Status",
"sort": "sort",
"authCode": "Permission Code",
"badge": "Badge",
"operation": "Actions",
@@ -94,8 +95,14 @@
"name": "Post",
"title": "Post Management"
},
"user": {
"name": "User",
"title": "User Management"
},
"status": "Status",
"remark": "Remarks",
"creator": "creator",
"modifier": "modifier",
"createTime": "Created At",
"operation": "Actions",
"updateTime": "Updated At"

View File

@@ -14,6 +14,7 @@
"menu": {
"list": "菜单列表",
"activeIcon": "激活图标",
"sort": "排序",
"activePath": "激活路径",
"activePathHelp": "跳转到当前路由时,需要激活的菜单路径。\n当不在导航菜单中显示时需要指定激活路径",
"activePathMustExist": "该路径未能找到有效的菜单",
@@ -95,8 +96,14 @@
"name": "岗位",
"title": "岗位管理"
},
"user": {
"name": "用户",
"title": "用户管理"
},
"status": "状态",
"remark": "备注",
"creator": "创建人",
"modifier": "修改人",
"createTime": "创建时间",
"operation": "操作",
"updateTime": "更新时间"

View File

@@ -0,0 +1,40 @@
import { BaseModel } from '#/models/base';
export namespace SystemUserApi {
export interface SystemUser {
id: number;
password: string;
last_login: string;
is_superuser: boolean;
username: string;
first_name: string;
last_name: string;
email: string;
is_staff: boolean;
is_active: boolean;
date_joined: string;
remark: string;
creator: string;
modifier: string;
update_time: string;
create_time: string;
is_deleted: boolean;
mobile: string;
nickname: string;
gender: number;
language: string;
city: string;
province: string;
country: string;
avatar_url: string;
status: number;
login_date: string;
login_ip: any;
}
}
export class SystemUserModel extends BaseModel<SystemUserApi.SystemUser> {
constructor() {
super('/system/user/');
}
}

View File

@@ -12,6 +12,15 @@ const routes: RouteRecordRaw[] = [
name: 'System',
path: '/system',
children: [
{
path: '/system/user',
name: 'SystemUser',
meta: {
icon: 'mdi:account-group',
title: $t('system.user.title'),
},
component: () => import('#/views/system/user/list.vue'),
},
{
path: '/system/role',
name: 'SystemRole',

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>