Compare commits

...

5 Commits

Author SHA1 Message Date
ChenYi
3d4b568a3b 修复错误 2025-07-16 17:08:11 +08:00
ChenYi
cd772d5b04 修复错误。 2025-07-16 17:05:52 +08:00
ChenYi
150c5a96f5 修复错误 2025-07-16 16:53:43 +08:00
ChenYi
ba7ebdb348 修复bug 2025-07-16 16:48:20 +08:00
ChenYi
92c14fe460 移除日志打印 2025-07-16 16:31:01 +08:00
4 changed files with 139 additions and 146 deletions

View File

@ -107,11 +107,9 @@ const handleValueChange = (value: string) => {
const selectedDevice = options.value.find(option => option.value === value);
if (selectedDevice) {
emit('device-change', selectedDevice);
console.log('DeviceSelect 发送设备信息:', selectedDevice);
}
} else {
emit('device-change', null);
console.log('DeviceSelect 清空设备信息');
}
};

View File

@ -52,20 +52,39 @@ export const generateDynamicColumns = (data: DynamicDeviceData[]): ColumnConfig[
return fields
.filter(field => !excludeFields.includes(field))
.map(field => {
const columnConfig: any = {
field,
title: fieldNameMapping[field] || field,
// 确保字段名是有效的字符串
const safeField = String(field || '').trim();
if (!safeField) return null;
const columnConfig: ColumnConfig = {
field: safeField,
title: fieldNameMapping[safeField] || safeField,
minWidth: 150,
showOverflow: true,
slots: {}, // 添加空的slots属性避免VXE表格报错
};
// 应用字段类型配置
if (fieldTypeConfig[field]) {
Object.assign(columnConfig, fieldTypeConfig[field]);
// 应用字段类型配置,确保配置是安全的
if (fieldTypeConfig[safeField]) {
const typeConfig = fieldTypeConfig[safeField];
if (typeConfig.formatter && typeof typeConfig.formatter === 'function') {
columnConfig.formatter = typeConfig.formatter;
}
if (typeConfig.width !== undefined) {
columnConfig.width = typeConfig.width;
}
if (typeConfig.minWidth !== undefined) {
columnConfig.minWidth = typeConfig.minWidth;
}
// 确保slots属性始终存在
if (!columnConfig.slots) {
columnConfig.slots = {};
}
}
return columnConfig;
});
})
.filter(Boolean) as ColumnConfig[]; // 过滤掉null值
};
// 获取所有可能的字段(用于预定义列)

View File

@ -26,7 +26,6 @@ const selectedDeviceInfo = ref<any>(null);
// ID
const fetchDeviceOptions = async () => {
try {
console.log('开始获取设备信息...');
const { data } = await postMetersPage({
body: {
pageIndex: 1,
@ -36,10 +35,6 @@ const fetchDeviceOptions = async () => {
if (data?.items) {
deviceOptions.value = data.items;
console.log('设备信息获取成功,总数:', data.items.length);
console.log('设备信息选项前3项:', deviceOptions.value.slice(0, 3));
} else {
console.log('设备信息为空');
}
} catch (error) {
console.error('获取设备信息失败:', error);
@ -48,6 +43,9 @@ const fetchDeviceOptions = async () => {
// ID
const getDeviceInfoById = (deviceId: string) => {
if (!deviceId || !deviceOptions.value || deviceOptions.value.length === 0) {
return null;
}
return deviceOptions.value.find((device) => device.id === deviceId);
};
@ -61,36 +59,58 @@ const dynamicColumns = ref<any[]>([]);
// - IoTDBTreeModelDeviceDataDto
const fixedColumns = [
{ title: '序号', type: 'seq', width: 50 },
{ field: 'Timestamps', title: $t('abp.IoTDBBase.Timestamps'), minWidth: 150 },
{ field: 'SystemName', title: $t('abp.IoTDBBase.SystemName'), minWidth: 150 },
{ field: 'ProjectId', title: $t('abp.IoTDBBase.ProjectId'), minWidth: 150 },
{ field: 'DeviceType', title: $t('abp.IoTDBBase.DeviceType'), minWidth: 150 },
{ 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: {} },
{
field: 'IoTDataType',
title: $t('abp.IoTDBBase.IoTDataType'),
minWidth: 150,
showOverflow: true,
slots: {},
},
{ field: 'DeviceId', title: $t('abp.IoTDBBase.DeviceId'), minWidth: 150 },
{ field: 'DeviceId', title: $t('abp.IoTDBBase.DeviceId'), minWidth: 150, showOverflow: true, slots: {} },
];
// - 使
const allColumns = computed(() => [...fixedColumns, ...dynamicColumns.value]);
const allColumns = computed(() => {
//
if (!isGridInitialized.value) {
return [...fixedColumns];
}
const columns = [...fixedColumns];
//
if (dynamicColumns.value && Array.isArray(dynamicColumns.value)) {
const validDynamicColumns = dynamicColumns.value.filter(col =>
col && typeof col === 'object' && col.field && col.title
).map(col => ({
...col,
slots: col.slots || {} // slots
}));
columns.push(...validDynamicColumns);
}
return columns;
});
//
const initDefaultColumns = () => {
if (dynamicColumns.value.length === 0) {
//
// dynamicColumns
if (!Array.isArray(dynamicColumns.value)) {
dynamicColumns.value = [];
}
};
//
const isGridInitialized = ref(false);
//
initDefaultColumns();
//
fetchDeviceOptions();
const formOptions: VbenFormProps = {
schema: querySchema.value,
initialValues: {
@ -118,32 +138,27 @@ const formOptions: VbenFormProps = {
// DeviceIdselectedDeviceInfo
if (changedFields.includes('DeviceId')) {
const deviceId = values.DeviceId;
console.log('DeviceId变化:', deviceId);
if (deviceId) {
// deviceOptions
let device = deviceOptions.value.find(d => d.id === deviceId);
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');
console.log('DeviceSelect组件实例:', deviceSelectRef);
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
device = deviceSelectRef.getSelectedDevice();
console.log('从DeviceSelect组件获取设备信息:', device);
}
} catch (error) {
console.log('无法从DeviceSelect组件获取设备信息:', error);
//
}
}
if (device) {
selectedDeviceInfo.value = device;
console.log('同步设备信息成功:', device);
} else {
console.log('未找到匹配的设备deviceId:', deviceId);
//
setTimeout(() => {
try {
@ -152,31 +167,22 @@ const formOptions: VbenFormProps = {
const delayedDevice = deviceSelectRef.getSelectedDevice();
if (delayedDevice) {
selectedDeviceInfo.value = delayedDevice;
console.log('延迟获取设备信息成功:', delayedDevice);
}
}
} catch (error) {
console.log('延迟获取设备信息失败:', error);
//
}
}, 100);
}
} else {
selectedDeviceInfo.value = null;
console.log('DeviceId为空清空selectedDeviceInfo');
}
}
if (hasRelevantChange) {
console.log('表单字段变化:', { values, changedFields });
console.log(
'相关字段变化:',
changedFields.filter((field) => relevantFields.has(field)),
);
// 使 setTimeout
setTimeout(async () => {
const latestValues = await gridApi.formApi.getValues();
console.log('最新表单值:', latestValues);
gridApi.reload(latestValues);
}, 0);
}
@ -188,7 +194,7 @@ const gridOptions: VxeGridProps<any> = {
highlight: true,
labelField: 'name',
},
columns: allColumns, // 使
columns: fixedColumns, // 使
height: 'auto',
keepSource: true,
pagerConfig: {
@ -196,10 +202,8 @@ const gridOptions: VxeGridProps<any> = {
pageSize: 20,
//
onChange: (currentPage: number, pageSize: number) => {
console.log('分页变化:', { currentPage, pageSize });
// pageSize
if (pageSize !== gridOptions.pagerConfig.pageSize) {
console.log('页面大小变化,重置到第一页');
// pageSize
gridOptions.pagerConfig.pageSize = pageSize;
gridOptions.pagerConfig.currentPage = 1;
@ -218,7 +222,6 @@ const gridOptions: VxeGridProps<any> = {
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
console.log('=== API调用开始 ===');
// DeviceTypeIoTDataType
const deviceTypeValue = formValues.DeviceType || DeviceType;
const deviceTypeNumber = deviceTypeValue
@ -227,38 +230,27 @@ const gridOptions: VxeGridProps<any> = {
const ioTDataTypeValue = formValues.IoTDataType;
console.log('=== API调用开始 ===2', ioTDataTypeValue);
// DeviceId(10)使focusId
let finalDeviceId = formValues.DeviceId || DeviceId;
let finalFocusAddress = formValues.FocusAddress;
let finalDeviceId = formValues.DeviceId || DeviceId || '';
let finalFocusAddress = formValues.FocusAddress || '';
// 使
console.log('API调用时的selectedDeviceInfo:', selectedDeviceInfo.value);
console.log('API调用时的formValues.DeviceId:', formValues.DeviceId);
const deviceInfo = selectedDeviceInfo.value || (formValues.DeviceId ? getDeviceInfoById(formValues.DeviceId) : null);
console.log('最终使用的deviceInfo:', deviceInfo);
const deviceInfo = selectedDeviceInfo.value || (formValues.DeviceId && deviceOptions.value.length > 0 ? getDeviceInfoById(formValues.DeviceId) : null);
if (deviceInfo) {
finalFocusAddress = deviceInfo.focusAddress;
console.log('使用focusAddress:', finalFocusAddress);
finalFocusAddress = deviceInfo.focusAddress || '';
if (deviceTypeNumber === 10) {
// 使focusId
if (deviceInfo.focusId) {
finalDeviceId = deviceInfo.focusId;
console.log('集中器类型使用focusId:', finalDeviceId);
}
} else {
// 使meterId
if (deviceInfo.meterId) {
finalDeviceId = deviceInfo.meterId;
console.log('其他类型使用meterId:', finalDeviceId);
}
}
} else {
console.log('没有找到设备信息使用原始DeviceId:', finalDeviceId);
}
try {
const { data } = await postTreeModelDeviceDataInfoPage({
@ -266,59 +258,41 @@ const gridOptions: VxeGridProps<any> = {
pageIndex: page.currentPage,
pageSize: page.pageSize,
// 使使
DeviceType: deviceTypeNumber,
DeviceId: finalDeviceId.toString(),
FocusAddress: finalFocusAddress || FocusAddress,
DeviceType: deviceTypeNumber || undefined,
DeviceId: finalDeviceId ? finalDeviceId.toString() : '',
FocusAddress: finalFocusAddress || FocusAddress || '',
//
SystemName: formValues.SystemName || SystemName,
IoTDataType: ioTDataTypeValue,
SystemName: formValues.SystemName || SystemName || '',
IoTDataType: ioTDataTypeValue || undefined,
},
});
console.log('API返回的原始数据:', data);
console.log('数据类型:', typeof data);
console.log(
'data是否为null/undefined:',
data === null || data === undefined,
);
if (data) {
console.log('data.items存在:', !!data.items);
console.log(
'data.items类型:',
Array.isArray(data.items) ? 'Array' : typeof data.items,
);
if (data.items) {
console.log('data.items长度:', data.items.length);
if (data.items.length > 0) {
console.log('第一条数据:', data.items[0]);
console.log(
'第一条数据的所有字段:',
Object.keys(data.items[0]),
);
}
}
}
// 使
if (data?.items && data.items.length > 0) {
console.log('原始items数据:', data.items);
try {
//
const generatedColumns = generateDynamicColumns(data.items);
console.log('生成的列定义:', generatedColumns);
//
dynamicColumns.value = generatedColumns;
// 使setStategridOptions
await nextTick();
if (gridApi && gridApi.setState) {
gridApi.setState({
gridOptions: {
...gridOptions,
columns: allColumns.value,
},
});
}
} catch (error) {
console.error('更新列定义时出错:', error);
// 使
dynamicColumns.value = [];
}
// 使totalCount
const result = {
@ -326,22 +300,8 @@ const gridOptions: VxeGridProps<any> = {
totalCount: data.totalCount || 0,
};
console.log('返回给表格的数据:', result);
console.log(
'总数:',
result.totalCount,
'当前页数据量:',
result.items.length,
);
console.log('分页信息:', {
currentPage: page.currentPage,
pageSize: page.pageSize,
});
return result;
}
console.log('没有数据或数据为空');
return {
items: [],
totalCount: 0,
@ -358,57 +318,71 @@ const gridOptions: VxeGridProps<any> = {
const [Grid, gridApi] = useVbenVxeGrid({ formOptions, gridOptions });
//
watch(
() => gridApi?.pagerApi?.currentPage,
(newPage, oldPage) => {
console.log('当前页变化:', { newPage, oldPage });
},
);
watch(
() => gridApi?.pagerApi?.pageSize,
(newSize, oldSize) => {
console.log('页面大小变化:', { newSize, oldSize });
if (newSize !== oldSize && oldSize) {
console.log('页面大小从', oldSize, '变为', newSize, ',重置到第一页');
//
gridApi.pagerApi.currentPage = 1;
}
},
);
//
const initializeGrid = async () => {
try {
//
await fetchDeviceOptions();
//
isGridInitialized.value = true;
//
if (gridApi && gridApi.setState) {
await nextTick();
gridApi.setState({
gridOptions: {
...gridOptions,
columns: allColumns.value,
},
});
}
//
if (DeviceType || DeviceId || FocusAddress || SystemName) {
//
setTimeout(() => {
if (gridApi) {
gridApi.reload();
}
}, 300);
}
} catch (error) {
console.error('初始化表格失败:', error);
}
};
//
watch(
() => [DeviceType, DeviceId, FocusAddress, SystemName],
async (newValues, oldValues) => {
console.log('路由参数变化:', { newValues, oldValues });
//
if (newValues.some(val => val) && gridApi) {
//
await fetchDeviceOptions();
if (newValues.some(val => val) && gridApi && isGridInitialized.value) {
//
setTimeout(() => {
console.log('自动触发查询,路由参数:', { DeviceType, DeviceId, FocusAddress, SystemName });
gridApi.reload();
}, 100);
}
},
{ immediate: true }
{ immediate: false } // false
);
//
//
onMounted(async () => {
console.log('页面挂载完成,检查路由参数:', { DeviceType, DeviceId, FocusAddress, SystemName });
//
if (DeviceType || DeviceId || FocusAddress || SystemName) {
//
await fetchDeviceOptions();
//
setTimeout(() => {
console.log('页面初始化时自动触发查询');
gridApi.reload();
}, 200);
}
// VXE
setTimeout(async () => {
await initializeGrid();
}, 100);
});
</script>

View File

@ -30,6 +30,7 @@ export interface ColumnConfig {
width?: string | number;
showOverflow?: boolean;
formatter?: (value: any) => string;
slots?: Record<string, any>; // 添加slots属性
[key: string]: any;
}
@ -44,6 +45,7 @@ export interface FieldTypeConfig {
formatter?: (value: any) => string;
width?: string | number;
minWidth?: string | number;
slots?: Record<string, any>;
[key: string]: any;
};
}