@ -26,7 +26,6 @@ const selectedDeviceInfo = ref<any>(null);
/ / 获 取 设 备 信 息 的 完 整 数 据 ( 用 于 根 据 设 备 I D 获 取 设 备 信 息 )
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 () => {
/ / 根 据 设 备 I D 获 取 设 备 信 息 对 象
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[]>([]);
/ / 固 定 列 定 义 ( 始 终 显 示 ) - 基 于 I o T D B T r e e M o d e l D e v i c e D a t a D t o 类 型
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 || { } / / 确 保 每 个 列 都 有 s l o t s 属 性
} ) ) ;
columns . push ( ... validDynamicColumns ) ;
}
return columns ;
} ) ;
/ / 初 始 化 默 认 列 ( 防 止 表 格 空 白 )
const initDefaultColumns = ( ) => {
if ( dynamicColumns . value . length === 0 ) {
/ / 不 再 需 要 在 这 里 设 置 默 认 列 , 因 为 固 定 列 已 经 包 含 了 基 本 字 段
/ / 确 保 d y n a m i c C o l u m n s 是 一 个 有 效 的 数 组
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 = {
/ / 新 增 : D e v i c e I d 变 化 时 同 步 s e l e c t e d D e v i c e I n f o
if ( changedFields . includes ( 'DeviceId' ) ) {
const deviceId = values . DeviceId ;
console . log ( 'DeviceId变化:' , deviceId ) ;
if ( deviceId ) {
/ / 先 尝 试 从 d e v i c e O p t i o n s 中 查 找 ( 备 用 方 案 )
let device = deviceOptions . value . find( d => d . id === deviceId ) ;
let device = deviceOptions . value . length > 0 ? deviceOptions . value . find( d => d . id === deviceId ) : null ;
/ / 如 果 没 找 到 , 尝 试 从 D e v i c e S e l e c t 组 件 中 获 取
if ( ! device && gridApi ? . formApi ) {
try {
/ / 获 取 D e v i c e S e l e c t 组 件 的 实 例
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 ) ) ,
) ;
/ / 使 用 s e t T i m e o u t 确 保 表 单 值 已 经 完 全 更 新
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 } ) ;
/ / 当 p a g e S i z e 变 化 时 , 重 置 到 第 一 页
if ( pageSize !== gridOptions . pagerConfig . pageSize ) {
console . log ( '页面大小变化,重置到第一页' ) ;
/ / 更 新 配 置 中 的 p a g e S i z e
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调用开始 ===' ) ;
/ / 处 理 D e v i c e T y p e 和 I o T D a t a T y p e , 确 保 传 递 数 字 类 型
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 ) ;
/ / 处 理 D e v i c e I d , 当 设 备 类 型 为 集 中 器 ( 1 0 ) 时 , 使 用 f o c u s I d
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 ) {
/ / 集 中 器 类 型 使 用 f o c u s I d
if ( deviceInfo . focusId ) {
finalDeviceId = deviceInfo . focusId ;
console . log ( '集中器类型, 使用focusId:' , finalDeviceId ) ;
}
} else {
/ / 其 他 设 备 类 型 使 用 m e t e r I d
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 ) ;
/ / 动 态 生 成 列 定 义
const generatedColumns = generateDynamicColumns ( data . items ) ;
console . log ( '生成的列定义:' , generatedColumns ) ;
/ / 更 新 动 态 列
dynamicColumns . value = generatedColumns ;
/ / 更 新 动 态 列
dynamicColumns . value = generatedColumns ;
/ / 使 用 s e t S t a t e 更 新 整 个 g r i d O p t i o n s , 确 保 列 定 义 能 够 正 确 更 新
await nextTick ( ) ;
gridApi . setState ( {
gridOptions : {
... gridOptions ,
columns : allColumns . value ,
} ,
} ) ;
/ / 使 用 s e t S t a t e 更 新 整 个 g r i d O p t i o n s , 确 保 列 定 义 能 够 正 确 更 新
await nextTick ( ) ;
if ( gridApi && gridApi . setState ) {
gridApi . setState ( {
gridOptions : {
... gridOptions ,
columns : allColumns . value ,
} ,
} ) ;
}
} catch ( error ) {
console . error ( '更新列定义时出错:' , error ) ;
/ / 如 果 列 更 新 失 败 , 使 用 空 数 组
dynamicColumns . value = [ ] ;
}
/ / 直 接 使 用 接 口 返 回 的 t o t a l C o u n t
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 } / / 改 为 f a l s e , 避 免 立 即 触 发
) ;
/ / 页 面 初 始 化 时 , 如 果 有 路 由 参 数 则 自 动 触 发 查 询
/ / 页 面 初 始 化 时 , 延迟 初 始 化 表 格
onMounted ( async ( ) => {
console . log ( '页面挂载完成,检查路由参数:' , { DeviceType , DeviceId , FocusAddress , SystemName } ) ;
/ / 如 果 有 路 由 参 数 , 等 待 设 备 信 息 加 载 完 成 后 自 动 触 发 查 询
if ( DeviceType || DeviceId || FocusAddress || SystemName ) {
/ / 等 待 设 备 信 息 加 载 完 成
await fetchDeviceOptions ( ) ;
/ / 延 迟 一 下 确 保 表 单 值 已 经 设 置
setTimeout ( ( ) => {
console . log ( '页面初始化时自动触发查询' ) ;
gridApi . reload ( ) ;
} , 200 ) ;
}
/ / 延 迟 初 始 化 , 确 保 V X E 表 格 组 件 已 完 全 挂 载
setTimeout ( async ( ) => {
await initializeGrid ( ) ;
} , 100 ) ;
} ) ;
< / script >