435 lines
13 KiB
Vue
Raw Normal View History

2025-06-25 17:29:57 +08:00
<script setup lang="ts">
import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { computed, nextTick, ref, watch, onMounted } from 'vue';
2025-06-25 17:29:57 +08:00
import { useRoute } from 'vue-router';
import { Page } from '@vben/common-ui';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
2025-07-14 08:57:04 +08:00
import { postMetersPage, postTreeModelDeviceDataInfoPage } from '#/api-client';
import { $t } from '#/locales';
2025-06-25 17:29:57 +08:00
2025-07-10 10:28:06 +08:00
import { generateDynamicColumns } from './dynamicColumns';
2025-07-11 13:51:31 +08:00
import { querySchema } from './schema';
2025-06-25 17:29:57 +08:00
2025-07-14 08:57:04 +08:00
defineOptions({
name: 'DeviceData',
});
2025-07-11 17:41:23 +08:00
// 存储设备信息选项的完整数据
const deviceOptions = ref<any[]>([]);
// 当前选中的设备信息
const selectedDeviceInfo = ref<any>(null);
2025-07-11 17:41:23 +08:00
// 获取设备信息的完整数据用于根据设备ID获取设备信息
2025-07-11 17:41:23 +08:00
const fetchDeviceOptions = async () => {
try {
const { data } = await postMetersPage({
body: {
pageIndex: 1,
pageSize: 1000,
},
});
2025-07-14 08:57:04 +08:00
2025-07-11 17:41:23 +08:00
if (data?.items) {
deviceOptions.value = data.items;
}
} catch (error) {
console.error('获取设备信息失败:', error);
}
};
// 根据设备ID获取设备信息对象
const getDeviceInfoById = (deviceId: string) => {
2025-07-16 16:48:20 +08:00
if (!deviceId || !deviceOptions.value || deviceOptions.value.length === 0) {
return null;
}
2025-07-14 08:57:04 +08:00
return deviceOptions.value.find((device) => device.id === deviceId);
2025-07-11 17:41:23 +08:00
};
2025-06-25 17:29:57 +08:00
const route = useRoute();
const { DeviceType, DeviceId, FocusAddress, SystemName } = route.query;
2025-07-10 10:28:06 +08:00
// 动态列定义
const dynamicColumns = ref<any[]>([]);
2025-07-10 13:50:05 +08:00
// 固定列定义(始终显示)- 基于 IoTDBTreeModelDeviceDataDto 类型
2025-07-10 10:52:19 +08:00
const fixedColumns = [
2025-07-16 17:05:52 +08:00
{ title: '序号', type: 'seq', width: 50, field: 'seq', slots: {} },
{ field: 'Timestamps', title: $t('abp.IoTDBBase.Timestamps'), minWidth: 150, showOverflow: true, slots: {} },
{ field: 'SystemName', title: $t('abp.IoTDBBase.SystemName'), minWidth: 150, showOverflow: true, slots: {} },
{ field: 'ProjectId', title: $t('abp.IoTDBBase.ProjectId'), minWidth: 150, showOverflow: true, slots: {} },
{ field: 'DeviceType', title: $t('abp.IoTDBBase.DeviceType'), minWidth: 150, showOverflow: true, slots: {} },
2025-07-14 08:57:04 +08:00
{
field: 'IoTDataType',
title: $t('abp.IoTDBBase.IoTDataType'),
minWidth: 150,
2025-07-16 16:53:43 +08:00
showOverflow: true,
2025-07-16 17:05:52 +08:00
slots: {},
2025-07-14 08:57:04 +08:00
},
2025-07-16 17:05:52 +08:00
{ field: 'DeviceId', title: $t('abp.IoTDBBase.DeviceId'), minWidth: 150, showOverflow: true, slots: {} },
2025-07-10 10:52:19 +08:00
];
2025-07-10 10:28:06 +08:00
2025-07-10 11:47:50 +08:00
// 合并固定列和动态列 - 使用计算属性确保响应式
2025-07-16 16:53:43 +08:00
const allColumns = computed(() => {
2025-07-16 17:08:11 +08:00
// 如果表格未初始化,只返回固定列
if (!isGridInitialized.value) {
return [...fixedColumns];
}
2025-07-16 16:53:43 +08:00
const columns = [...fixedColumns];
// 安全地添加动态列
if (dynamicColumns.value && Array.isArray(dynamicColumns.value)) {
const validDynamicColumns = dynamicColumns.value.filter(col =>
col && typeof col === 'object' && col.field && col.title
2025-07-16 17:05:52 +08:00
).map(col => ({
...col,
slots: col.slots || {} // 确保每个列都有slots属性
}));
2025-07-16 16:53:43 +08:00
columns.push(...validDynamicColumns);
}
return columns;
});
2025-07-10 10:36:02 +08:00
// 初始化默认列(防止表格空白)
const initDefaultColumns = () => {
2025-07-16 16:53:43 +08:00
// 确保dynamicColumns是一个有效的数组
if (!Array.isArray(dynamicColumns.value)) {
2025-07-10 13:50:05 +08:00
dynamicColumns.value = [];
2025-07-10 10:36:02 +08:00
}
};
2025-07-16 17:08:11 +08:00
// 表格是否已初始化
const isGridInitialized = ref(false);
2025-07-10 10:36:02 +08:00
// 初始化默认列
initDefaultColumns();
2025-07-10 10:28:06 +08:00
2025-06-25 17:29:57 +08:00
const formOptions: VbenFormProps = {
schema: querySchema.value,
initialValues: {
FocusAddress: FocusAddress as string,
2025-07-11 17:41:23 +08:00
DeviceType: DeviceType ? Number(DeviceType) : undefined,
DeviceId: DeviceId as string,
SystemName: SystemName as string,
},
2025-07-10 10:28:06 +08:00
// 禁用表单值变化时自动提交,使用自定义处理函数
submitOnChange: false,
// 添加表单值变化的处理函数
handleValuesChange: async (values, changedFields) => {
2025-07-11 17:41:23 +08:00
// 当任何相关字段发生变化时,刷新表格数据
2025-07-14 08:57:04 +08:00
const relevantFields = new Set([
'DeviceId',
'DeviceType',
'FocusAddress',
'IoTDataType',
'SystemName',
]);
const hasRelevantChange = changedFields.some((field) =>
relevantFields.has(field),
);
// 新增DeviceId变化时同步selectedDeviceInfo
if (changedFields.includes('DeviceId')) {
const deviceId = values.DeviceId;
if (deviceId) {
// 先尝试从 deviceOptions 中查找(备用方案)
2025-07-16 16:48:20 +08:00
let device = deviceOptions.value.length > 0 ? deviceOptions.value.find(d => d.id === deviceId) : null;
// 如果没找到,尝试从 DeviceSelect 组件中获取
if (!device && gridApi?.formApi) {
try {
// 获取 DeviceSelect 组件的实例
const deviceSelectRef = gridApi.formApi.getFieldComponentRef('DeviceId');
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
device = deviceSelectRef.getSelectedDevice();
}
} catch (error) {
2025-07-16 16:31:01 +08:00
// 静默处理错误
}
}
if (device) {
selectedDeviceInfo.value = device;
} else {
// 如果还是没找到,尝试延迟获取(组件可能还没完全更新)
setTimeout(() => {
try {
const deviceSelectRef = gridApi.formApi.getFieldComponentRef('DeviceId');
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
const delayedDevice = deviceSelectRef.getSelectedDevice();
if (delayedDevice) {
selectedDeviceInfo.value = delayedDevice;
}
}
} catch (error) {
2025-07-16 16:31:01 +08:00
// 静默处理错误
}
}, 100);
}
} else {
selectedDeviceInfo.value = null;
}
}
2025-07-11 17:41:23 +08:00
if (hasRelevantChange) {
2025-07-10 10:28:06 +08:00
// 使用 setTimeout 确保表单值已经完全更新
setTimeout(async () => {
const latestValues = await gridApi.formApi.getValues();
gridApi.reload(latestValues);
}, 0);
}
},
2025-06-25 17:29:57 +08:00
};
2025-07-10 11:47:50 +08:00
2025-06-25 17:29:57 +08:00
const gridOptions: VxeGridProps<any> = {
checkboxConfig: {
highlight: true,
labelField: 'name',
},
2025-07-16 17:08:11 +08:00
columns: fixedColumns, // 初始化时只使用固定列
2025-06-25 17:29:57 +08:00
height: 'auto',
keepSource: true,
2025-07-16 17:34:58 +08:00
// 确保分页功能正常工作
pager: true,
2025-07-10 10:36:02 +08:00
pagerConfig: {
currentPage: 1,
pageSize: 20,
2025-07-10 15:33:21 +08:00
// 添加分页事件处理
onChange: (currentPage: number, pageSize: number) => {
2025-07-16 17:34:58 +08:00
console.log('分页变化:', { currentPage, pageSize });
2025-07-10 15:33:21 +08:00
// 当pageSize变化时重置到第一页
if (pageSize !== gridOptions.pagerConfig.pageSize) {
// 更新配置中的pageSize
gridOptions.pagerConfig.pageSize = pageSize;
gridOptions.pagerConfig.currentPage = 1;
}
2025-07-16 17:34:58 +08:00
// 触发数据重新加载
if (gridApi) {
console.log('触发数据重新加载');
gridApi.reload();
}
2025-07-10 15:33:21 +08:00
},
2025-07-10 10:36:02 +08:00
},
2025-06-25 17:29:57 +08:00
toolbarConfig: {
custom: true,
},
customConfig: {
storage: true,
},
2025-07-10 10:52:19 +08:00
// 添加调试配置
showOverflow: true,
showHeaderOverflow: true,
2025-06-25 17:29:57 +08:00
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
2025-07-11 17:41:23 +08:00
// 处理DeviceType和IoTDataType确保传递数字类型
const deviceTypeValue = formValues.DeviceType || DeviceType;
2025-07-14 08:57:04 +08:00
const deviceTypeNumber = deviceTypeValue
? Number(deviceTypeValue)
: undefined;
2025-07-14 10:32:02 +08:00
const ioTDataTypeValue = formValues.IoTDataType;
2025-07-14 08:57:04 +08:00
2025-07-14 09:09:44 +08:00
// 处理DeviceId当设备类型为集中器(10)时使用focusId
2025-07-16 16:48:20 +08:00
let finalDeviceId = formValues.DeviceId || DeviceId || '';
let finalFocusAddress = formValues.FocusAddress || '';
// 优先使用选中的设备信息
2025-07-16 16:48:20 +08:00
const deviceInfo = selectedDeviceInfo.value || (formValues.DeviceId && deviceOptions.value.length > 0 ? getDeviceInfoById(formValues.DeviceId) : null);
if (deviceInfo) {
2025-07-16 16:48:20 +08:00
finalFocusAddress = deviceInfo.focusAddress || '';
if (deviceTypeNumber === 10) {
// 集中器类型使用focusId
if (deviceInfo.focusId) {
finalDeviceId = deviceInfo.focusId;
}
} else {
// 其他设备类型使用meterId
if (deviceInfo.meterId) {
finalDeviceId = deviceInfo.meterId;
}
}
}
2025-07-10 10:52:19 +08:00
try {
2025-07-16 17:34:58 +08:00
console.log('API调用参数:', {
pageIndex: page.currentPage,
pageSize: page.pageSize,
DeviceType: deviceTypeNumber,
DeviceId: finalDeviceId,
FocusAddress: finalFocusAddress,
SystemName: formValues.SystemName || SystemName,
IoTDataType: ioTDataTypeValue,
});
2025-07-10 10:52:19 +08:00
const { data } = await postTreeModelDeviceDataInfoPage({
body: {
pageIndex: page.currentPage,
pageSize: page.pageSize,
2025-07-11 17:41:23 +08:00
// 优先使用表单中的值,如果没有则使用路由参数
2025-07-16 16:48:20 +08:00
DeviceType: deviceTypeNumber || undefined,
DeviceId: finalDeviceId ? finalDeviceId.toString() : '',
FocusAddress: finalFocusAddress || FocusAddress || '',
2025-07-11 17:41:23 +08:00
// 添加其他表单参数
2025-07-16 16:48:20 +08:00
SystemName: formValues.SystemName || SystemName || '',
IoTDataType: ioTDataTypeValue || undefined,
2025-07-10 10:52:19 +08:00
},
});
2025-07-11 13:51:31 +08:00
2025-07-16 16:31:01 +08:00
2025-07-11 13:51:31 +08:00
// 简化处理逻辑,直接使用接口返回的数据
2025-07-10 10:52:19 +08:00
if (data?.items && data.items.length > 0) {
2025-07-16 16:53:43 +08:00
try {
// 动态生成列定义
const generatedColumns = generateDynamicColumns(data.items);
// 更新动态列
dynamicColumns.value = generatedColumns;
// 使用setState更新整个gridOptions确保列定义能够正确更新
await nextTick();
if (gridApi && gridApi.setState) {
gridApi.setState({
gridOptions: {
...gridOptions,
columns: allColumns.value,
2025-07-16 17:34:58 +08:00
// 保持分页配置
pager: true,
pagerConfig: gridOptions.pagerConfig,
2025-07-16 16:53:43 +08:00
},
});
}
} catch (error) {
console.error('更新列定义时出错:', error);
// 如果列更新失败,使用空数组
dynamicColumns.value = [];
}
2025-07-11 13:51:31 +08:00
// 直接使用接口返回的totalCount
2025-07-11 13:51:31 +08:00
const result = {
items: data.items || [],
totalCount: data.totalCount || 0,
2025-07-11 13:51:31 +08:00
};
2025-07-14 08:57:04 +08:00
2025-07-11 13:51:31 +08:00
return result;
2025-07-10 15:33:21 +08:00
}
2025-07-10 11:47:50 +08:00
return {
items: [],
totalCount: 0,
};
2025-07-10 10:52:19 +08:00
} catch (error) {
console.error('API调用出错:', error);
throw error;
2025-07-10 10:28:06 +08:00
}
2025-06-25 17:29:57 +08:00
},
},
},
};
2025-07-10 10:28:06 +08:00
const [Grid, gridApi] = useVbenVxeGrid({ formOptions, gridOptions });
2025-07-10 15:33:21 +08:00
// 监听分页器状态变化
2025-07-11 13:51:31 +08:00
watch(
() => gridApi?.pagerApi?.pageSize,
(newSize, oldSize) => {
2025-07-16 17:34:58 +08:00
console.log('页面大小变化:', { newSize, oldSize });
2025-07-11 13:51:31 +08:00
if (newSize !== oldSize && oldSize) {
// 重置到第一页
gridApi.pagerApi.currentPage = 1;
2025-07-16 17:34:58 +08:00
// 触发数据重新加载
console.log('页面大小变化,触发重新加载');
gridApi.reload();
}
},
);
// 监听当前页变化
watch(
() => gridApi?.pagerApi?.currentPage,
(newPage, oldPage) => {
console.log('当前页变化:', { newPage, oldPage });
if (newPage !== oldPage && oldPage) {
// 触发数据重新加载
console.log('当前页变化,触发重新加载');
gridApi.reload();
2025-07-10 15:33:21 +08:00
}
2025-07-11 13:51:31 +08:00
},
);
2025-07-16 17:08:11 +08:00
// 初始化函数
const initializeGrid = async () => {
try {
// 获取设备信息数据
await fetchDeviceOptions();
// 标记表格已初始化
isGridInitialized.value = true;
// 更新表格列定义
if (gridApi && gridApi.setState) {
await nextTick();
gridApi.setState({
gridOptions: {
...gridOptions,
columns: allColumns.value,
2025-07-16 17:34:58 +08:00
// 保持分页配置
pager: true,
pagerConfig: gridOptions.pagerConfig,
2025-07-16 17:08:11 +08:00
},
});
}
// 如果有路由参数,自动触发查询
if (DeviceType || DeviceId || FocusAddress || SystemName) {
// 延迟一下确保表格已完全初始化
setTimeout(() => {
if (gridApi) {
gridApi.reload();
}
}, 300);
}
} catch (error) {
console.error('初始化表格失败:', error);
}
};
// 监听路由参数变化,当有路由参数时自动触发查询
watch(
() => [DeviceType, DeviceId, FocusAddress, SystemName],
async (newValues, oldValues) => {
// 如果有路由参数,等待设备信息加载完成后自动触发查询
2025-07-16 17:08:11 +08:00
if (newValues.some(val => val) && gridApi && isGridInitialized.value) {
// 延迟一下确保表单值已经设置
setTimeout(() => {
gridApi.reload();
}, 100);
}
},
2025-07-16 17:08:11 +08:00
{ immediate: false } // 改为false避免立即触发
);
2025-07-16 17:08:11 +08:00
// 页面初始化时,延迟初始化表格
onMounted(async () => {
2025-07-16 17:08:11 +08:00
// 延迟初始化确保VXE表格组件已完全挂载
setTimeout(async () => {
await initializeGrid();
}, 100);
});
2025-06-25 17:29:57 +08:00
</script>
<template>
<Page auto-content-height>
<Grid />
</Page>
</template>