281 lines
8.1 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';
2025-07-17 14:01:07 +08:00
import { onMounted, ref, watch } 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-28 11:54:55 +08:00
import { postMetersPage } from '#/api-client';
import { postCTWingLogInfoPage } from '#/api-client/IoTClient';
2025-06-25 17:29:57 +08:00
2025-07-17 10:23:38 +08:00
import DeviceSelect from '../deviceData/DeviceSelect.vue';
2025-07-17 10:27:56 +08:00
import { querySchema, tableSchema } from './schema';
2025-06-25 17:29:57 +08:00
defineOptions({
2025-07-11 11:48:12 +08:00
name: 'CTWingLog',
2025-06-25 17:29:57 +08:00
});
const route = useRoute();
const { DeviceType, DeviceId, FocusAddress } = route.query;
2025-07-17 10:23:38 +08:00
// 添加设备选择器引用
const deviceSelectRef = ref();
// 添加选中的设备信息
const selectedDeviceInfo = ref<any>(null);
2025-07-17 14:01:07 +08:00
// 存储设备信息选项的完整数据
const deviceOptions = ref<any[]>();
// 获取设备信息的完整数据
const fetchDeviceOptions = async () => {
try {
const { data } = await postMetersPage({
body: {
pageIndex: 1,
pageSize: 1000,
},
});
if (data?.items) {
deviceOptions.value = data.items;
}
} catch (error) {
console.error('获取设备信息失败:', error);
}
};
// 根据设备ID获取设备信息对象
const getDeviceInfoById = (deviceId: string) => {
if (!deviceId || !deviceOptions.value || deviceOptions.value.length === 0) {
return null;
}
2025-07-17 14:36:24 +08:00
2025-07-17 14:01:07 +08:00
const device = deviceOptions.value.find((device) => device.id === deviceId);
return device;
};
2025-07-17 10:23:38 +08:00
// 格式化日期函数
const formatDate = (date: any) => {
if (!date) return '';
if (typeof date === 'string') return date;
if (date.toISOString) return date.format('YYYY-MM-DD HH:mm:ss');
return '';
2025-07-17 10:27:56 +08:00
};
2025-07-17 14:01:07 +08:00
// 从 DeviceSelect 组件获取设备信息
const getDeviceInfoFromRef = async () => {
if (
deviceSelectRef.value &&
typeof deviceSelectRef.value.getSelectedDevice === 'function'
) {
const deviceInfo = deviceSelectRef.value.getSelectedDevice();
return deviceInfo;
}
return null;
};
2025-06-25 17:29:57 +08:00
const formOptions: VbenFormProps = {
schema: querySchema.value,
2025-07-17 14:01:07 +08:00
// 禁用表单值变化时自动提交,使用自定义处理函数
submitOnChange: false,
// 添加表单值变化的处理函数
handleValuesChange: async (values, changedFields) => {
// 当 DeviceId 发生变化时,更新 selectedDeviceInfo
if (changedFields.includes('DeviceId')) {
const deviceId = values.DeviceId;
2025-07-17 14:36:24 +08:00
2025-07-17 14:01:07 +08:00
if (deviceId) {
// 先尝试从 deviceOptions 中查找(备用方案)
2025-07-17 14:36:24 +08:00
let device =
deviceOptions.value.length > 0
? deviceOptions.value.find((d) => d.id === deviceId)
: null;
2025-07-17 14:01:07 +08:00
// 如果没找到,尝试从 DeviceSelect 组件中获取
if (!device && gridApi?.formApi) {
try {
// 获取 DeviceSelect 组件的实例
2025-07-17 14:36:24 +08:00
const deviceSelectRef =
gridApi.formApi.getFieldComponentRef('DeviceId');
2025-07-17 14:01:07 +08:00
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
device = deviceSelectRef.getSelectedDevice();
}
2025-07-17 14:36:24 +08:00
} catch {
2025-07-17 14:14:04 +08:00
// 静默处理错误
2025-07-17 14:01:07 +08:00
}
}
2025-07-17 14:36:24 +08:00
2025-07-17 14:01:07 +08:00
if (device) {
selectedDeviceInfo.value = device;
} else {
// 如果还是没找到,尝试延迟获取(组件可能还没完全更新)
setTimeout(() => {
try {
2025-07-17 14:36:24 +08:00
const deviceSelectRef =
gridApi.formApi.getFieldComponentRef('DeviceId');
2025-07-17 14:01:07 +08:00
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
const delayedDevice = deviceSelectRef.getSelectedDevice();
if (delayedDevice) {
selectedDeviceInfo.value = delayedDevice;
}
}
2025-07-17 14:36:24 +08:00
} catch {
2025-07-17 14:14:04 +08:00
// 静默处理错误
2025-07-17 14:01:07 +08:00
}
}, 100);
}
} else {
selectedDeviceInfo.value = null;
}
}
2025-07-17 14:36:24 +08:00
2025-07-17 14:01:07 +08:00
// 当任何相关字段发生变化时,刷新表格数据
const relevantFields = new Set([
'DeviceId',
'DeviceType',
2025-07-17 14:36:24 +08:00
'EndCreationTime',
2025-07-17 14:01:07 +08:00
'IoTDataType',
'StartCreationTime',
2025-07-17 14:36:24 +08:00
'SystemName',
2025-07-17 14:01:07 +08:00
]);
const hasRelevantChange = changedFields.some((field) =>
relevantFields.has(field),
);
if (hasRelevantChange) {
// 使用 setTimeout 确保表单值已经完全更新
setTimeout(async () => {
const latestValues = await gridApi.formApi.getValues();
gridApi.reload(latestValues);
}, 0);
}
},
2025-06-25 17:29:57 +08:00
};
2025-07-17 10:23:38 +08:00
2025-07-11 11:48:12 +08:00
const gridOptions: VxeGridProps<any> = {
2025-06-25 17:29:57 +08:00
checkboxConfig: {
highlight: true,
labelField: 'name',
},
2025-07-11 11:48:12 +08:00
columns: tableSchema.value,
2025-06-25 17:29:57 +08:00
height: 'auto',
keepSource: true,
2025-07-17 14:01:07 +08:00
// 确保分页功能正常工作
pager: true,
2025-07-17 10:23:38 +08:00
pagerConfig: {
2025-07-17 10:27:56 +08:00
currentPage: 1,
2025-07-17 14:01:07 +08:00
pageSize: 20,
2025-07-17 10:23:38 +08:00
},
2025-06-25 17:29:57 +08:00
toolbarConfig: {
custom: true,
},
customConfig: {
storage: true,
},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
2025-07-17 14:36:24 +08:00
// 总是从表单API获取最新的表单值确保分页时参数完整
const currentFormValues = gridApi?.formApi ? await gridApi.formApi.getValues() : {};
2025-07-17 10:23:38 +08:00
// 获取选中的设备信息
2025-07-17 14:01:07 +08:00
let deviceId = currentFormValues.DeviceId || '';
2025-07-17 10:23:38 +08:00
let systemName = '';
2025-07-17 14:01:07 +08:00
const deviceType = currentFormValues.DeviceType || '';
2025-07-17 14:36:24 +08:00
2025-07-17 14:01:07 +08:00
// 优先使用选中的设备信息
2025-07-17 14:36:24 +08:00
const deviceInfo =
selectedDeviceInfo.value ||
(currentFormValues.DeviceId && deviceOptions.value.length > 0
? getDeviceInfoById(currentFormValues.DeviceId)
: null);
2025-07-17 14:01:07 +08:00
if (deviceInfo) {
2025-07-17 14:36:24 +08:00
systemName = deviceInfo.businessSystemName || '';
2025-07-17 14:01:07 +08:00
// 根据设备类型获取正确的 id
if (Number(deviceType) === 10) {
// 集中器类型使用 focusId
deviceId = deviceInfo.focusId || deviceId;
} else {
// 其他设备类型使用 meterId
deviceId = deviceInfo.meterId || deviceId;
2025-07-17 10:23:38 +08:00
}
}
2025-07-14 16:04:42 +08:00
// 构建查询参数
2025-07-17 14:01:07 +08:00
const queryParams = {
2025-07-11 11:48:12 +08:00
pageIndex: page.currentPage,
pageSize: page.pageSize,
2025-07-17 14:01:07 +08:00
SearchKeyword: currentFormValues.SearchKeyword || '',
2025-07-17 10:23:38 +08:00
DeviceType: deviceType,
2025-07-17 14:01:07 +08:00
IoTDataType: currentFormValues.IoTDataType || '',
2025-07-17 10:23:38 +08:00
DeviceId: deviceId,
2025-07-17 14:01:07 +08:00
SystemName: systemName || currentFormValues.SystemName || '',
StartCreationTime: formatDate(currentFormValues.StartCreationTime),
EndCreationTime: formatDate(currentFormValues.EndCreationTime),
FocusAddress: currentFormValues.FocusAddress || '',
2025-07-14 16:04:42 +08:00
};
2025-07-17 14:36:24 +08:00
2025-07-14 16:04:42 +08:00
if (DeviceType) queryParams.DeviceType = DeviceType;
if (DeviceId) queryParams.DeviceId = DeviceId;
const { data } = await postCTWingLogInfoPage({
body: queryParams,
2025-07-11 11:48:12 +08:00
});
const result = {
items: data.items || [],
totalCount: data.totalCount || (data.items ? data.items.length : 0),
};
return result;
2025-06-25 17:29:57 +08:00
},
},
},
};
2025-07-17 10:23:38 +08:00
const [Grid, gridApi] = useVbenVxeGrid({ formOptions, gridOptions });
// 监听分页器状态变化
watch(
() => gridApi?.pagerApi?.pageSize,
2025-07-17 14:01:07 +08:00
async (newSize, oldSize) => {
2025-07-17 10:23:38 +08:00
if (newSize !== oldSize && oldSize) {
// 重置到第一页
gridApi.pagerApi.currentPage = 1;
2025-07-17 14:01:07 +08:00
// 获取最新表单值并触发数据重新加载
const latestValues = await gridApi.formApi.getValues();
gridApi.reload(latestValues);
2025-07-17 10:23:38 +08:00
}
},
);
// 监听当前页变化
watch(
() => gridApi?.pagerApi?.currentPage,
2025-07-17 14:01:07 +08:00
async (newPage, oldPage) => {
2025-07-17 10:23:38 +08:00
if (newPage !== oldPage && oldPage) {
2025-07-17 14:01:07 +08:00
// 获取最新表单值并触发数据重新加载
const latestValues = await gridApi.formApi.getValues();
gridApi.reload(latestValues);
2025-07-17 10:23:38 +08:00
}
},
);
2025-07-17 14:01:07 +08:00
// 页面初始化时获取设备信息
onMounted(async () => {
await fetchDeviceOptions();
});
2025-06-25 17:29:57 +08:00
</script>
<template>
<Page auto-content-height>
2025-07-17 10:23:38 +08:00
<Grid>
2025-07-17 10:27:56 +08:00
<template #DeviceId="{ model, field }">
<DeviceSelect ref="deviceSelectRef" v-model:value="model[field]"
:placeholder="$t('common.pleaseSelect') + $t('abp.log.deviceInfo')" allow-clear />
2025-07-17 10:23:38 +08:00
</template>
</Grid>
2025-06-25 17:29:57 +08:00
</Page>
</template>