refactor:数据实时监控-弹框内容重构

liutao_branch
黄伟杰 4 months ago
parent f79bf1a43f
commit 7102032c70

@ -1,73 +1,59 @@
<template> <template>
<Dialog v-model="dialogVisible" :title="dialogTitle" width="1200"> <Dialog v-model="dialogVisible" :title="dialogTitle" width="900">
<ContentWrap> <div class="single-device-dialog">
<el-form class="-mb-15px" :inline="true" label-width="80px"> <div class="single-device-dialog__header">
<el-form-item label="参数类型"> <div>设备名称{{ deviceName || '-' }}</div>
<el-select <div>采集时间{{ displayTime }}</div>
v-model="selectedGroupKey" </div>
placeholder="请选择" <ContentWrap v-loading="loading">
:disabled="groupKeys.length <= 1" <div v-if="sections.length" class="single-device-dialog__table-grid">
class="!w-240px" <div v-for="section in sections" :key="section.key" class="single-device-dialog__section">
> <div class="single-device-dialog__section-title">
<el-option v-for="key in groupKeys" :key="key" :label="key" :value="key" /> {{ section.title }}
</el-select> </div>
</el-form-item> <el-empty v-if="!section.columns.length" description="暂无数据" />
</el-form>
<el-table <el-table
v-loading="loading" v-else :data="section.rows" :border="true" :header-cell-style="headerCellStyle"
:data="pagedList" :cell-style="bodyCellStyle" size="small">
:stripe="true" <el-table-column
:show-overflow-tooltip="true" v-for="col in section.columns" :key="col.prop" :prop="col.prop" :label="col.label"
:row-key="getRowKey" align="center">
>
<el-table-column label="点位编码" align="left" prop="pointCode" min-width="140px" />
<el-table-column label="点位名称" align="left" prop="pointName" min-width="160px" />
<el-table-column label="数据类型" align="center" prop="dataType" width="120px" />
<el-table-column label="寄存器地址" align="center" prop="address" width="140px" />
<el-table-column label="最新值" align="center" prop="addressValue" width="140px">
<template #default="scope"> <template #default="scope">
{{ formatLatestValue(scope.row.addressValue) }} <span>{{ formatCell(scope.row[col.prop]) }}</span>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column label="倍率" align="center" prop="ratio" width="100px" />
<el-table-column label="单位" align="center" prop="unit" width="100px" />
<el-table-column
label="最新采集时间"
align="center"
prop="latestCollectTime"
:formatter="dateFormatter"
width="180px"
/>
</el-table> </el-table>
<Pagination </div>
:total="total" </div>
v-model:page="pageNo" <el-empty v-else description="暂无数据" />
v-model:limit="pageSize"
@pagination="handlePagination"
/>
</ContentWrap> </ContentWrap>
</div>
</Dialog> </Dialog>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime' import { formatDate } from '@/utils/formatTime'
import { DeviceApi } from '@/api/iot/device' import { DeviceApi } from '@/api/iot/device'
type MonitorRow = { type SectionColumn = {
pointCode: string prop: string
pointName: string label: string
dataType: string }
address: string
addressValue: unknown type SectionRow = Record<string, string | number | null>
ratio: number | null
unit: string type Section = {
latestCollectTime: string | number key: string
title: string
columns: SectionColumn[]
rows: SectionRow[]
} }
const props = defineProps<{ const props = defineProps<{
modelValue: boolean modelValue: boolean
deviceId?: string | number deviceId?: string | number
deviceName?: string deviceName?: string
collectionTime?: string | number
}>() }>()
const emit = defineEmits<{ const emit = defineEmits<{
@ -84,92 +70,89 @@ const dialogVisible = computed({
}) })
const dialogTitle = computed(() => { const dialogTitle = computed(() => {
return props.deviceName ? `设备名称:${props.deviceName}` : '单设备监控' return '单设备监控'
}) })
const loading = ref(false) const loading = ref(false)
const groupRawMap = ref<Record<string, any[]>>({}) const deviceName = computed(() => props.deviceName)
const selectedGroupKey = ref('') const sections = ref<Section[]>([])
const pageNo = ref(1)
const pageSize = ref(10)
const groupKeys = computed(() => Object.keys(groupRawMap.value))
const fullList = computed(() => {
const raw = groupRawMap.value[selectedGroupKey.value]
return normalizeRows(Array.isArray(raw) ? raw : [])
})
const total = computed(() => fullList.value.length)
const pagedList = computed(() => { const toGroupMap = (value: any): Record<string, any[]> => {
const start = (pageNo.value - 1) * pageSize.value if (!value || typeof value !== 'object' || Array.isArray(value)) {
return fullList.value.slice(start, start + pageSize.value) return {}
})
const handlePagination = () => {
if (!total.value) {
pageNo.value = 1
return
} }
const maxPageNo = Math.max(1, Math.ceil(total.value / pageSize.value)) const entries = Object.entries(value).filter(([, v]) => Array.isArray(v))
if (pageNo.value > maxPageNo) { if (!entries.length) {
pageNo.value = maxPageNo return {}
} }
const map: Record<string, any[]> = {}
for (const [k, v] of entries) {
map[k] = v as any[]
}
return map
} }
const getRowKey = (row: MonitorRow, index: number) => { const displayTime = computed(() => {
return row.pointCode || row.pointName || `${index}` if (!props.collectionTime) {
}
const formatLatestValue = (value: unknown) => {
if (value === null) {
return 'null'
}
if (value === undefined) {
return '-' return '-'
} }
if (typeof value === 'string' || typeof value === 'number') { const value = props.collectionTime
if (typeof value === 'number') {
return formatDate(new Date(value))
}
const date = new Date(value)
if (Number.isNaN(date.getTime())) {
return value return value
} }
return '' return formatDate(date)
})
const headerCellStyle = () => {
return {
fontWeight: 500,
padding: '6px 4px'
}
} }
const normalizeRows = (raw: any[]): MonitorRow[] => { const bodyCellStyle = () => {
return raw.map((item: any) => {
return { return {
pointCode: item?.pointCode ?? item?.attributeCode ?? item?.code ?? '', padding: '4px 4px'
pointName: item?.pointName ?? item?.attributeName ?? item?.name ?? '',
dataType: item?.dataType ?? '',
address: item?.address ?? '',
addressValue: item?.addressValue ?? item?.latestValue ?? item?.value,
ratio: item?.ratio ?? null,
unit: item?.unit ?? item?.dataUnit ?? '',
latestCollectTime:
item?.latestCollectTime ?? item?.latestCollectionTime ?? item?.collectionTime ?? item?.collectTime ?? ''
} }
})
} }
const toGroupMap = (value: any): Record<string, any[]> => { const formatCell = (value: string | number | null | undefined) => {
if (!value || typeof value !== 'object' || Array.isArray(value)) { if (value === null || value === undefined) {
return {} return ''
} }
const entries = Object.entries(value).filter(([, v]) => Array.isArray(v)) return value
if (!entries.length) { }
return {}
const buildSectionsFromGroups = (groups: Record<string, any[]>): Section[] => {
const result: Section[] = []
for (const [key, list] of Object.entries(groups)) {
const columns: SectionColumn[] = []
const row: SectionRow = {}
if (Array.isArray(list) && list.length) {
list.forEach((item: any, index: number) => {
const prop = `col${index}`
const label = item?.attributeName ?? `字段${index + 1}`
columns.push({ prop, label })
row[prop] = item?.addressValue ?? ''
})
} }
const map: Record<string, any[]> = {} result.push({
for (const [k, v] of entries) { key,
map[k] = v as any[] title: key,
columns,
rows: columns.length ? [row] : []
})
} }
return map return result
} }
const fetchList = async () => { const fetchList = async () => {
if (props.deviceId === undefined || props.deviceId === null || props.deviceId === '') { if (props.deviceId === undefined || props.deviceId === null || props.deviceId === '') {
groupRawMap.value = {} sections.value = []
selectedGroupKey.value = ''
return return
} }
loading.value = true loading.value = true
@ -182,9 +165,7 @@ const fetchList = async () => {
...toGroupMap(res?.data?.data), ...toGroupMap(res?.data?.data),
...toGroupMap(res) ...toGroupMap(res)
} }
groupRawMap.value = groups sections.value = buildSectionsFromGroups(groups)
selectedGroupKey.value = Object.keys(groups)[0] ?? ''
pageNo.value = 1
} finally { } finally {
loading.value = false loading.value = false
} }
@ -199,20 +180,36 @@ watch(
fetchList() fetchList()
} }
) )
</script>
watch( <style scoped>
() => selectedGroupKey.value, .single-device-dialog {
() => { display: flex;
pageNo.value = 1 flex-direction: column;
handlePagination() gap: 12px;
} }
)
watch( .single-device-dialog__header {
() => pageSize.value, display: flex;
() => { justify-content: space-between;
pageNo.value = 1 align-items: center;
handlePagination() font-size: 14px;
} color: var(--el-text-color-primary);
) }
</script>
.single-device-dialog__table-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.single-device-dialog__section-title {
font-size: 13px;
margin-bottom: 4px;
color: var(--el-text-color-primary);
}
.single-device-dialog__section :deep(.el-table__inner-wrapper) {
border-radius: 0;
}
</style>

@ -59,6 +59,7 @@ v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @
v-model="monitorDialogVisible" v-model="monitorDialogVisible"
:device-id="monitorDeviceId" :device-id="monitorDeviceId"
:device-name="monitorDeviceName" :device-name="monitorDeviceName"
:collection-time="monitorCollectionTime"
/> />
</template> </template>
@ -91,6 +92,7 @@ const queryFormRef = ref() // 搜索的表单
const monitorDialogVisible = ref(false) const monitorDialogVisible = ref(false)
const monitorDeviceId = ref<string | number>() const monitorDeviceId = ref<string | number>()
const monitorDeviceName = ref<string>() const monitorDeviceName = ref<string>()
const monitorCollectionTime = ref<string | number>()
const buildQueryParams = (): Parameters<typeof DeviceApi.getLineDevicePage>[0] => { const buildQueryParams = (): Parameters<typeof DeviceApi.getLineDevicePage>[0] => {
const params: Parameters<typeof DeviceApi.getLineDevicePage>[0] = { const params: Parameters<typeof DeviceApi.getLineDevicePage>[0] = {
@ -137,25 +139,11 @@ const handleSingleMonitor = (row: LineDeviceVO) => {
} }
monitorDeviceId.value = deviceId monitorDeviceId.value = deviceId
monitorDeviceName.value = row?.deviceName ?? '' monitorDeviceName.value = row?.deviceName ?? ''
monitorCollectionTime.value = row?.collectionTime
monitorDialogVisible.value = true monitorDialogVisible.value = true
} }
let timer: any = null
onMounted(() => { onMounted(() => {
getList() getList()
timer = setInterval(async () => {
try {
const data = await DeviceApi.getLineDevicePage(buildQueryParams())
list.value = data.list
total.value = data.total
} catch { }
}, 5000)
})
onUnmounted(() => {
if (timer) {
clearInterval(timer)
}
}) })
</script> </script>

Loading…
Cancel
Save