feat(@vben/web-antd): 新增部门管理功能

This commit is contained in:
Kven 2025-05-08 21:19:01 +08:00
parent 8aa7411f69
commit cf11f2cdf5
9 changed files with 436 additions and 5 deletions

View File

@ -0,0 +1,76 @@
import { requestClient } from '#/api/request';
export namespace DeptApi {
export interface Dept {
remark: string;
createTime: string;
name: string;
enabled: string;
id: string;
children?: Dept[];
}
export interface DeptRecord extends Dept {
id: string;
}
export interface DeptTreeData {
id: number;
label: string;
children?: DeptTreeData[];
}
}
// 添加区域
export function createDept(data: DeptApi.Dept) {
return requestClient.post(`/api/rest/dept`, data);
}
// 更新区域信息
export function updateDept(data: DeptApi.DeptRecord) {
return requestClient.patch(`/api/rest/dept/${data.id}`, data);
}
// 删除区域
export function removeDept(id: string) {
return requestClient.delete(`/api/rest/dept/${id}`);
}
// 模糊查询获取区域列表
export function queryDeptList(data: DeptApi.DeptRecord) {
return requestClient.get('/api/rest/dept', { data });
}
// 获取区域列表
export function deptList() {
return requestClient.get(`/api/rest/dept`);
}
// 启用状态
export function enabledDept(id: string) {
return requestClient.patch(`/api/rest/dept/toggle/${id}`, id);
}
export function getAllDeptTree(id?: number | string) {
return requestClient.get('/rest/dept/tree', { params: { id } });
}
// export function getDeptTree(params?: Partial<DeptRecord>) {
// return axios.get<DeptRecord[]>(`/api/dept/trees`, {
// params,
// paramsSerializer: (obj) => {
// return qs.stringify(obj);
// },
// });
// }
// export function listDepts(params?: ListParams<Partial<DeptRecord>>) {
// return axios.get<DeptRecord[]>(`/api/dept`, {
// params,
// paramsSerializer: (obj) => {
// return qs.stringify(obj);
// },
// });
// }
// export function queryDepts(params?: ListParams<Partial<DeptRecord>>) {
// return queryList(`/api/dept`, params);
// }

View File

@ -1,5 +1,6 @@
export * from './auth';
export * from './chatflow';
export * from './dept';
export * from './menu';
export * from './server';
export * from './user';

View File

@ -163,7 +163,3 @@ export function deptAudit(id: string, roleId: string) {
export function getUserDetail(id: number) {
return requestClient.get<UserApi.UserState>(`/user/${id}`);
}
export function getAllDeptTree(id?: number | string) {
return requestClient.get('/rest/dept/tree', { params: { id } });
}

View File

@ -0,0 +1,18 @@
import type { RouteRecordRaw } from 'vue-router';
import { RiDept } from '@vben/icons';
const routes: RouteRecordRaw[] = [
{
name: 'dept',
path: '/dept',
component: () => import('#/views/dept/list.vue'),
meta: {
icon: RiDept,
title: '部门管理',
order: 5,
},
},
];
export default routes;

View File

@ -0,0 +1,122 @@
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { DeptApi } from '#/api';
import { z } from '#/adapter/form';
import { getAllDeptTree } from '#/api';
import { $t } from '#/locales';
/**
* 使export一个数组常量
*/
export function useSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'name',
label: '部门名称',
rules: z.string(),
},
{
component: 'ApiTreeSelect',
componentProps: {
allowClear: true,
api: getAllDeptTree(1),
class: 'w-full',
labelField: 'name',
valueField: 'id',
childrenField: 'children',
},
fieldName: 'pid',
label: '上级部门',
},
{
component: 'RadioGroup',
componentProps: {
buttonStyle: 'solid',
options: [
{ label: $t('common.enabled'), value: 1 },
{ label: $t('common.disabled'), value: 0 },
],
optionType: 'button',
},
defaultValue: 1,
fieldName: 'status',
label: '状态',
},
{
component: 'Textarea',
componentProps: {
maxLength: 50,
rows: 3,
showCount: true,
},
fieldName: 'remark',
label: '备注',
rules: z.string().optional(),
},
];
}
/**
*
* @description 使export一个Array常量
* @param onActionClick
*/
export function useColumns(
onActionClick?: OnActionClickFn<DeptApi.Dept>,
): VxeTableGridOptions<DeptApi.Dept>['columns'] {
return [
{
align: 'left',
field: 'name',
fixed: 'left',
title: '部门名称',
treeNode: true,
width: 150,
},
{
cellRender: { name: 'CellTag' },
field: 'enable',
title: '状态',
width: 100,
},
{
field: 'createTime',
title: '创建时间',
width: 180,
},
{
align: 'right',
cellRender: {
attrs: {
nameField: 'name',
nameTitle: '部门',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'append',
text: '新增下级',
},
'edit', // 默认的编辑按钮
{
code: 'delete', // 默认的删除按钮
disabled: (row: DeptApi.Dept) => {
return !!(row.children && row.children.length > 0);
},
},
],
},
field: 'operation',
fixed: 'right',
headerAlign: 'center',
showOverflow: false,
title: '操作',
width: 200,
},
];
}

View File

@ -0,0 +1,140 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { DeptApi } from '#/api';
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 { getAllDeptTree, removeDept } from '#/api';
import { $t } from '#/locales';
import { useColumns } from './data';
import Form from './modules/form.vue';
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
/**
* 编辑部门
* @param row
*/
function onEdit(row: DeptApi.Dept) {
formModalApi.setData(row).open();
}
/**
* 添加下级部门
* @param row
*/
function onAppend(row: DeptApi.Dept) {
formModalApi.setData({ pid: row.id }).open();
}
/**
* 创建新部门
*/
function onCreate() {
formModalApi.setData(null).open();
}
/**
* 删除部门
* @param row
*/
function onDelete(row: DeptApi.Dept) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0,
key: 'action_process_msg',
});
removeDept(row.id)
.then(() => {
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
key: 'action_process_msg',
});
refreshGrid();
})
.catch(() => {
hideLoading();
});
}
/**
* 表格操作按钮的回调函数
*/
function onActionClick({ code, row }: OnActionClickParams<DeptApi.Dept>) {
switch (code) {
case 'append': {
onAppend(row);
break;
}
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: false,
},
proxyConfig: {
ajax: {
query: async (_params) => {
return await getAllDeptTree(1);
},
},
},
toolbarConfig: {
custom: true,
export: false,
refresh: { code: 'query' },
zoom: true,
},
treeConfig: {
parentField: 'pid',
rowField: 'id',
transform: false,
},
} 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', ['部门']) }}
</Button>
</template>
</Grid>
</Page>
</template>

View File

@ -0,0 +1,78 @@
<script lang="ts" setup>
import type { DeptApi } from '#/api';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { Button } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { createDept, updateDept } from '#/api';
import { $t } from '#/locales';
import { useSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<DeptApi.Dept>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', ['部门'])
: $t('ui.actionTitle.create', ['部门']);
});
const [Form, formApi] = useVbenForm({
layout: 'vertical',
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
? updateDept(formData.value.id, data)
: createDept(data));
modalApi.close();
emit('success');
} finally {
modalApi.lock(false);
}
}
},
onOpenChange(isOpen) {
if (isOpen) {
const data = modalApi.getData<DeptApi.Dept>();
if (data) {
if (data.pid === 0) {
data.pid = undefined;
}
formData.value = data;
formApi.setValues(formData.value);
}
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form class="mx-4" />
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
</div>
</template>
</Modal>
</template>

View File

@ -5,5 +5,4 @@ export * from './fallback';
export * from './home';
export * from './ppt';
export * from './spider';
export * from './user';
export * from './word';

View File

@ -13,4 +13,5 @@ export const MdiGoogle = createIconifyIcon('mdi:google');
export const MdiChevron = createIconifyIcon('tabler:chevron-right');
export const MdiUser = createIconifyIcon('mdi:user');
export const MageRobot = createIconifyIcon('mage:robot');
export const RiDept = createIconifyIcon('ri:door-open-line');
// export const MdiUser = createIconifyIcon('mdi:user');