Przeglądaj źródła

fix(water): handle monitoring trend times and export errors (#8)

Kazerin 3 dni temu
rodzic
commit
11b2858047

+ 11 - 1
src/utils/waterSupplyModel.js

@@ -35,7 +35,17 @@ export function normalizeMonitoringRecord(record = {}, metric = 'value', monitor
     value: value == null || value === '' ? null : Number(value),
     unit: record.unit ?? (metric === 'pressure' ? 'MPa' : metric === 'power' ? 'kW' : 'm³/h'),
     status: record.status ?? record.onlineStatus ?? record.alarmStatus ?? 0,
-    updateTime: record.updateTime ?? record.monitorTime ?? record.createTime ?? '-'
+    updateTime: record.updateTime ?? record.monitorTime ?? record.createTime ?? record.time ?? '-'
+  }
+}
+
+export async function readMonitoringExportError(blob) {
+  if (!blob || blob.type !== 'application/json') return null
+  try {
+    const payload = JSON.parse(await blob.text())
+    return payload?.msg || payload?.message || '导出失败,请稍后重试'
+  } catch (error) {
+    return '导出失败:服务端返回了无法解析的错误信息'
   }
 }
 

+ 9 - 3
src/views/subSystem/waterSupply/components/WaterMonitoringPage.vue

@@ -88,7 +88,7 @@
         <el-card shadow="never" class="table-card">
             <div class="card-title">
                 <span>{{ title }}点位清单</span>
-                <el-button link type="primary" :loading="exportLoading" @click="exportData">批量导出</el-button>
+                <el-button v-hasPermi="[exportPermission]" link type="primary" :loading="exportLoading" @click="exportData">批量导出</el-button>
             </div>
             <el-table :data="page.records" stripe border v-loading="loading" @row-click="showTrend">
                 <template #empty>
@@ -161,11 +161,11 @@
 </template>
 <script setup>
     import {computed, nextTick, onBeforeUnmount, onMounted, ref} from 'vue'
-    import {DataAnalysis, Monitor, Warning} from '@element-plus/icons-vue'
+    import {DataAnalysis, Monitor, Refresh, Warning} from '@element-plus/icons-vue'
     import {ElMessage} from 'element-plus'
     import {saveAs} from 'file-saver'
     import * as echarts from 'echarts'
-    import {normalizeMonitoringRecord, normalizePage, unwrapResponse} from '@/utils/waterSupplyModel'
+    import {normalizeMonitoringRecord, normalizePage, readMonitoringExportError, unwrapResponse} from '@/utils/waterSupplyModel'
     import {buildMonitoringQuery, buildTrendQuery} from '@/utils/waterSupplyMonitoringQuery'
 
     const props = defineProps({
@@ -178,6 +178,7 @@
         load: {type: Function, required: true},
         trend: {type: Function, required: true},
         exportApi: {type: Function, required: true},
+        exportPermission: {type: String, required: true},
         metric: {type: String, default: 'value'},
         monitorType: {type: String, default: ''}
     })
@@ -310,6 +311,11 @@
             const blob = response instanceof Blob
                 ? response
                 : new Blob([response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
+            const exportError = await readMonitoringExportError(blob)
+            if (exportError) {
+                ElMessage.error(exportError)
+                return
+            }
             if (!blob.size) throw new Error('导出文件为空')
             saveAs(blob, `${props.title}数据_${new Date().toISOString().slice(0, 10)}.xlsx`)
             ElMessage.success('导出完成')

+ 1 - 1
src/views/subSystem/waterSupply/wyxjc/lljc.vue

@@ -2,4 +2,4 @@
 import WaterMonitoringPage from '../components/WaterMonitoringPage.vue'
 import { listFlowMonitoring, getFlowTrend, exportFlowMonitoring } from '@/api/pipeNetwork/waterSupply'
 </script>
-<template><WaterMonitoringPage title="流量监测" metric-label="流量" unit="m³/h" metric="flow" monitor-type="流量监测" :load="listFlowMonitoring" :trend="getFlowTrend" :export-api="exportFlowMonitoring" min-key="flowMin" max-key="flowMax" /></template>
+<template><WaterMonitoringPage title="流量监测" metric-label="流量" unit="m³/h" metric="flow" monitor-type="流量监测" :load="listFlowMonitoring" :trend="getFlowTrend" :export-api="exportFlowMonitoring" export-permission="waterSupply:monitoring:flow:export" min-key="flowMin" max-key="flowMax" /></template>

+ 1 - 1
src/views/subSystem/waterSupply/wyxjc/lousjc.vue

@@ -2,4 +2,4 @@
 import WaterMonitoringPage from '../components/WaterMonitoringPage.vue'
 import { listLeakageMonitoring, getLeakageTrend, exportLeakageMonitoring } from '@/api/pipeNetwork/waterSupply'
 </script>
-<template><WaterMonitoringPage title="漏失监测" metric-label="漏失" unit="kW" metric="power" monitor-type="漏失监测" range-label="功率范围" min-key="powerMin" max-key="powerMax" :load="listLeakageMonitoring" :trend="getLeakageTrend" :export-api="exportLeakageMonitoring" /></template>
+<template><WaterMonitoringPage title="漏失监测" metric-label="漏失" unit="kW" metric="power" monitor-type="漏失监测" range-label="功率范围" min-key="powerMin" max-key="powerMax" :load="listLeakageMonitoring" :trend="getLeakageTrend" :export-api="exportLeakageMonitoring" export-permission="waterSupply:monitoring:leakage:export" /></template>

+ 1 - 1
src/views/subSystem/waterSupply/wyxjc/lsjc.vue

@@ -2,4 +2,4 @@
 import WaterMonitoringPage from '../components/WaterMonitoringPage.vue'
 import { listPressureMonitoring, getPressureTrend, exportPressureMonitoring } from '@/api/pipeNetwork/waterSupply'
 </script>
-<template><WaterMonitoringPage title="压力监测" metric-label="压力" unit="MPa" metric="pressure" monitor-type="压力监测" range-label="压力范围" min-key="pressureMin" max-key="pressureMax" :load="listPressureMonitoring" :trend="getPressureTrend" :export-api="exportPressureMonitoring" /></template>
+<template><WaterMonitoringPage title="压力监测" metric-label="压力" unit="MPa" metric="pressure" monitor-type="压力监测" range-label="压力范围" min-key="pressureMin" max-key="pressureMax" :load="listPressureMonitoring" :trend="getPressureTrend" :export-api="exportPressureMonitoring" export-permission="waterSupply:monitoring:pressure:export" /></template>

+ 24 - 0
tests/waterSupplyMonitoring.test.mjs

@@ -4,6 +4,10 @@ import {
   buildTrendQuery,
   formatMonitoringDateTime
 } from '../src/utils/waterSupplyMonitoringQuery.js'
+import {
+  normalizeMonitoringRecord,
+  readMonitoringExportError
+} from '../src/utils/waterSupplyModel.js'
 
 const query = {
   pageNum: 2,
@@ -42,4 +46,24 @@ assert.deepEqual(buildTrendQuery(), {})
 assert.equal(formatMonitoringDateTime(new Date(2026, 8, 1, 8, 30, 5)), '2026-09-01 08:30:05')
 assert.equal(formatMonitoringDateTime('  '), undefined)
 
+const trendPoint = normalizeMonitoringRecord({
+  equipmentId: 'water-flow-device',
+  equipmentName: '供水流量监测点',
+  time: '2026-09-01 12:30:00',
+  value: '25.5'
+}, 'flow', '流量监测')
+
+assert.equal(trendPoint.updateTime, '2026-09-01 12:30:00')
+assert.equal(trendPoint.value, 25.5)
+
+const exportError = await readMonitoringExportError(
+  new Blob(['{"code":500,"msg":"导出数据过多"}'], {type: 'application/json'})
+)
+const validExport = await readMonitoringExportError(
+  new Blob(['xlsx'], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
+)
+
+assert.equal(exportError, '导出数据过多')
+assert.equal(validExport, null)
+
 console.log('waterSupplyMonitoring tests passed')