feat: 【antd】【iot】设备分组代码迁移

This commit is contained in:
haohao
2025-10-26 18:26:41 +08:00
parent 60e199abf0
commit 8203f436cd
3 changed files with 38 additions and 31 deletions

View File

@@ -1,10 +1,11 @@
import type { VbenFormSchema } from '#/adapter/form'; import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { DICT_TYPE } from '@vben/constants'; import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { z } from '#/adapter/form'; import { z } from '#/adapter/form';
import { getSimpleDeviceGroupList } from '#/api/iot/device/group'; import { getRangePickerDefaultProps } from '#/utils';
/** 新增/修改设备分组的表单 */ /** 新增/修改设备分组的表单 */
export function useFormSchema(): VbenFormSchema[] { export function useFormSchema(): VbenFormSchema[] {
@@ -30,16 +31,15 @@ export function useFormSchema(): VbenFormSchema[] {
.max(64, '分组名称长度不能超过 64 个字符'), .max(64, '分组名称长度不能超过 64 个字符'),
}, },
{ {
fieldName: 'parentId', fieldName: 'status',
label: '父级分组', label: '分组状态',
component: 'ApiTreeSelect', component: 'RadioGroup',
componentProps: { componentProps: {
api: getSimpleDeviceGroupList, options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
labelField: 'name', buttonStyle: 'solid',
valueField: 'id', optionType: 'button',
placeholder: '请选择父级分组',
allowClear: true,
}, },
rules: z.number().default(CommonStatusEnum.ENABLE),
}, },
{ {
fieldName: 'description', fieldName: 'description',
@@ -65,6 +65,15 @@ export function useGridFormSchema(): VbenFormSchema[] {
allowClear: true, allowClear: true,
}, },
}, },
{
fieldName: 'createTime',
label: '创建时间',
component: 'RangePicker',
componentProps: {
...getRangePickerDefaultProps(),
allowClear: true,
},
},
]; ];
} }
@@ -72,14 +81,13 @@ export function useGridFormSchema(): VbenFormSchema[] {
export function useGridColumns(): VxeTableGridOptions['columns'] { export function useGridColumns(): VxeTableGridOptions['columns'] {
return [ return [
{ {
field: 'name', field: 'id',
title: '分组名称', title: 'ID',
minWidth: 200, minWidth: 100,
treeNode: true,
}, },
{ {
field: 'description', field: 'name',
title: '分组描述', title: '分组名称',
minWidth: 200, minWidth: 200,
}, },
{ {
@@ -92,9 +100,9 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
}, },
}, },
{ {
field: 'deviceCount', field: 'description',
title: '设备数量', title: '分组描述',
minWidth: 100, minWidth: 200,
}, },
{ {
field: 'createTime', field: 'createTime',
@@ -102,6 +110,11 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
minWidth: 180, minWidth: 180,
formatter: 'formatDateTime', formatter: 'formatDateTime',
}, },
{
field: 'deviceCount',
title: '设备数量',
minWidth: 100,
},
{ {
title: '操作', title: '操作',
width: 200, width: 200,

View File

@@ -3,7 +3,6 @@ import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { IotDeviceGroupApi } from '#/api/iot/device/group'; import type { IotDeviceGroupApi } from '#/api/iot/device/group';
import { Page, useVbenModal } from '@vben/common-ui'; import { Page, useVbenModal } from '@vben/common-ui';
import { handleTree } from '@vben/utils';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
@@ -62,24 +61,14 @@ const [Grid, gridApi] = useVbenVxeGrid({
columns: useGridColumns(), columns: useGridColumns(),
height: 'auto', height: 'auto',
keepSource: true, keepSource: true,
treeConfig: {
transform: true,
rowField: 'id',
parentField: 'parentId',
},
proxyConfig: { proxyConfig: {
ajax: { ajax: {
query: async ({ page }, formValues) => { query: async ({ page }, formValues) => {
const data = await getDeviceGroupPage({ return await getDeviceGroupPage({
pageNo: page.currentPage, pageNo: page.currentPage,
pageSize: page.pageSize, pageSize: page.pageSize,
...formValues, ...formValues,
}); });
// 转换为树形结构
return {
...data,
list: handleTree(data.list, 'id', 'parentId'),
};
}, },
}, },
}, },

View File

@@ -39,6 +39,7 @@ const [Form, formApi] = useVbenForm({
}, },
schema: useFormSchema(), schema: useFormSchema(),
showCollapseButton: false, showCollapseButton: false,
showDefaultActions: false,
}); });
const [Modal, modalApi] = useVbenModal({ const [Modal, modalApi] = useVbenModal({
@@ -70,9 +71,13 @@ const [Modal, modalApi] = useVbenModal({
async onOpenChange(isOpen: boolean) { async onOpenChange(isOpen: boolean) {
if (!isOpen) { if (!isOpen) {
formData.value = undefined; formData.value = undefined;
formApi.resetForm();
return; return;
} }
// 重置表单
await formApi.resetForm();
const data = modalApi.getData<IotDeviceGroupApi.DeviceGroup>(); const data = modalApi.getData<IotDeviceGroupApi.DeviceGroup>();
// 如果没有数据或没有 id表示是新增 // 如果没有数据或没有 id表示是新增
if (!data || !data.id) { if (!data || !data.id) {