gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!-- 设备属性管理 -->
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { IotDeviceApi } from '#/api/iot/device/device';
 
import {
  nextTick,
  onBeforeUnmount,
  onMounted,
  reactive,
  ref,
  watch,
} from 'vue';
 
import { Page } from '..\..\..\..\..\..\packages\effects\common-ui\src';
import { IconifyIcon } from '..\..\..\..\..\..\packages\icons\src';
import { formatDateTime } from '..\..\..\..\..\..\packages\utils\src';
 
import {
  Button,
  Card,
  Col,
  Divider,
  Input,
  Row,
  Switch,
  Tag,
} from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getLatestDeviceProperties } from '#/api/iot/device/device';
 
import DeviceDetailsThingModelPropertyHistory from './thing-model-property-history.vue';
 
const props = defineProps<{ deviceId: number }>();
 
const loading = ref(true); // 列表的加载中
const list = ref<IotDeviceApi.DevicePropertyDetail[]>([]); // 显示的列表数据
const filterList = ref<IotDeviceApi.DevicePropertyDetail[]>([]); // 完整的数据列表
const queryParams = reactive({
  keyword: '' as string,
}); // 查询参数
const autoRefresh = ref(false); // 自动刷新开关
let autoRefreshTimer: any = null; // 定时器
const viewMode = ref<'card' | 'list'>('card'); // 视图模式状态
 
/** Grid 列定义 */
function useGridColumns(): VxeTableGridOptions<IotDeviceApi.DevicePropertyDetail>['columns'] {
  return [
    {
      field: 'identifier',
      title: '属性标识符',
    },
    {
      field: 'name',
      title: '属性名称',
    },
    {
      field: 'dataType',
      title: '数据类型',
    },
    {
      field: 'value',
      title: '属性值',
      slots: { default: 'value' },
    },
    {
      field: 'updateTime',
      title: '更新时间',
      width: 180,
      slots: { default: 'updateTime' },
    },
    {
      title: '操作',
      width: 120,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
 
/** 创建 Grid 实例 */
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    rowConfig: {
      keyField: 'identifier',
      isHover: true,
    },
    proxyConfig: {
      ajax: {
        query: async () => {
          if (!props.deviceId) {
            return { list: [], total: 0 };
          }
          const data = await getLatestDeviceProperties({
            deviceId: props.deviceId,
            identifier: undefined,
            name: undefined,
          });
          // 筛选数据
          let filteredData = data;
          if (queryParams.keyword.trim()) {
            const keyword = queryParams.keyword.toLowerCase();
            filteredData = data.filter(
              (item: IotDeviceApi.DevicePropertyDetail) =>
                item.identifier?.toLowerCase().includes(keyword) ||
                item.name?.toLowerCase().includes(keyword),
            );
          }
          // 更新本地列表用于卡片视图
          filterList.value = data;
          list.value = filteredData;
          return {
            list: filteredData,
            total: filteredData.length,
          };
        },
      },
    },
    toolbarConfig: {
      refresh: false,
      search: false,
    },
    pagerConfig: {
      enabled: false,
    },
  } as VxeTableGridOptions<IotDeviceApi.DevicePropertyDetail>,
});
 
// 包装 gridApi.query() 方法,统一列表视图和卡片视图的查询接口
gridApi.query = async () => {
  if (viewMode.value === 'list') {
    // 列表视图:手动获取数据并加载到 Grid
    if (!props.deviceId) {
      return;
    }
    const data = await getLatestDeviceProperties({
      deviceId: props.deviceId,
      identifier: undefined,
      name: undefined,
    });
    const dataArray = Array.isArray(data) ? data : [];
    let filteredData = dataArray;
    if (queryParams.keyword.trim()) {
      const keyword = queryParams.keyword.toLowerCase();
      filteredData = dataArray.filter(
        (item: IotDeviceApi.DevicePropertyDetail) =>
          item.identifier?.toLowerCase().includes(keyword) ||
          item.name?.toLowerCase().includes(keyword),
      );
    }
    filterList.value = dataArray;
    list.value = filteredData;
    // 直接加载数据到 Grid
    if (gridApi.grid) {
      gridApi.grid.loadData(filteredData);
    }
  } else {
    // 卡片视图:调用 getList 方法
    await getList();
  }
};
 
/** 查询列表 */
async function getList() {
  loading.value = true;
  try {
    if (viewMode.value === 'list') {
      await gridApi.query();
    } else {
      // 卡片视图:手动获取数据
      const params = {
        deviceId: props.deviceId,
        identifier: undefined as string | undefined,
        name: undefined as string | undefined,
      };
      filterList.value = await getLatestDeviceProperties(params);
      handleFilter();
    }
  } finally {
    loading.value = false;
  }
}
 
/** 前端筛选数据 */
function handleFilter() {
  if (queryParams.keyword.trim()) {
    const keyword = queryParams.keyword.toLowerCase();
    list.value = filterList.value.filter(
      (item: IotDeviceApi.DevicePropertyDetail) =>
        item.identifier?.toLowerCase().includes(keyword) ||
        item.name?.toLowerCase().includes(keyword),
    );
  } else {
    list.value = filterList.value;
  }
}
 
/** 搜索按钮操作 */
function handleQuery() {
  if (viewMode.value === 'list') {
    gridApi.query();
  } else {
    handleFilter();
  }
}
 
/** 搜索关键词变化 */
function handleKeywordChange(event: Event) {
  if (!(event.target as HTMLInputElement).value) {
    handleQuery();
  }
}
 
/** 视图切换 */
async function handleViewModeChange(mode: 'card' | 'list') {
  if (viewMode.value === mode) {
    return;
  }
  viewMode.value = mode;
  await nextTick();
  gridApi.query();
}
 
/** 历史操作 */
const historyRef = ref();
function openHistory(deviceId: number, identifier: string, dataType: string) {
  historyRef.value.open(deviceId, identifier, dataType);
}
 
/** 格式化属性值和单位 */
function formatValueWithUnit(item: IotDeviceApi.DevicePropertyDetail) {
  if (item.value === null || item.value === undefined || item.value === '') {
    return '-';
  }
  const unitName = item.dataSpecs?.unitName;
  return unitName ? `${item.value} ${unitName}` : item.value;
}
 
/** 监听自动刷新 */
watch(autoRefresh, (newValue) => {
  if (newValue) {
    autoRefreshTimer = setInterval(() => {
      gridApi.query();
    }, 5000);
  } else {
    clearInterval(autoRefreshTimer);
    autoRefreshTimer = null;
  }
});
 
/** 监听设备标识变化 */
watch(
  () => props.deviceId,
  (newValue) => {
    if (newValue) {
      gridApi.query();
    }
  },
);
 
/** 初始化 */
onMounted(async () => {
  if (props.deviceId) {
    await nextTick();
    gridApi.query();
  }
});
 
/** 组件卸载时清除定时器 */
onBeforeUnmount(() => {
  if (autoRefreshTimer) {
    clearInterval(autoRefreshTimer);
    autoRefreshTimer = null;
  }
});
</script>
 
<template>
  <Page auto-content-height>
    <!-- 搜索工作栏 -->
    <div class="flex items-center justify-between" style="margin-bottom: 16px">
      <div class="flex items-center" style="gap: 16px">
        <Input
          v-model:value="queryParams.keyword"
          allow-clear
          placeholder="请输入属性名称、标识符"
          style="width: 240px"
          @change="handleKeywordChange"
          @press-enter="handleQuery"
        />
        <Switch
          v-model:checked="autoRefresh"
          checked-children="定时刷新"
          class="ml-[20px]"
          un-checked-children="定时刷新"
        />
      </div>
      <Button.Group>
        <Button
          :type="viewMode === 'card' ? 'primary' : 'default'"
          @click="handleViewModeChange('card')"
        >
          <IconifyIcon icon="ep:grid" />
        </Button>
        <Button
          :type="viewMode === 'list' ? 'primary' : 'default'"
          @click="handleViewModeChange('list')"
        >
          <IconifyIcon icon="ep:list" />
        </Button>
      </Button.Group>
    </div>
 
    <!-- 分隔线 -->
    <Divider style="margin: 16px 0" />
 
    <!-- 卡片视图 -->
    <template v-if="viewMode === 'card'">
      <Row v-loading="loading" :gutter="16">
        <Col
          v-for="item in list"
          :key="item.identifier"
          :lg="6"
          :md="12"
          :sm="12"
          :xs="24"
          class="mb-4"
        >
          <Card
            :body-style="{ padding: '0' }"
            class="relative h-full overflow-hidden transition-colors"
          >
            <!-- 添加渐变背景层 -->
            <div
              class="pointer-events-none absolute left-0 right-0 top-0 h-12 bg-gradient-to-b from-muted to-transparent"
            ></div>
            <div class="relative p-4">
              <!-- 标题区域 -->
              <div class="mb-3 flex items-center">
                <div class="mr-2.5 flex items-center">
                  <IconifyIcon class="text-lg text-primary" icon="ep:cpu" />
                </div>
                <div class="flex-1 text-base font-bold">{{ item.name }}</div>
                <!-- 标识符 -->
                <div class="mr-2 inline-flex items-center">
                  <Tag color="blue" size="small">
                    {{ item.identifier }}
                  </Tag>
                </div>
                <!-- 数据类型标签 -->
                <div class="mr-2 inline-flex items-center">
                  <Tag size="small">
                    {{ item.dataType }}
                  </Tag>
                </div>
                <!-- 数据图标 - 可点击 -->
                <div
                  class="flex h-8 w-8 cursor-pointer items-center justify-center rounded-full transition-colors hover:bg-blue-50"
                  @click="
                    openHistory(props.deviceId, item.identifier, item.dataType)
                  "
                >
                  <IconifyIcon
                    class="text-lg text-primary"
                    icon="ep:data-line"
                  />
                </div>
              </div>
 
              <!-- 信息区域 -->
              <div class="text-sm">
                <div class="mb-2.5 last:mb-0">
                  <span class="mr-2.5 text-muted-foreground">属性值</span>
                  <span class="font-bold text-foreground">
                    {{ formatValueWithUnit(item) }}
                  </span>
                </div>
                <div class="mb-2.5 last:mb-0">
                  <span class="mr-2.5 text-muted-foreground">更新时间</span>
                  <span class="text-sm text-foreground">
                    {{
                      item.updateTime ? formatDateTime(item.updateTime) : '-'
                    }}
                  </span>
                </div>
              </div>
            </div>
          </Card>
        </Col>
      </Row>
    </template>
 
    <!-- 列表视图 -->
    <Grid v-show="viewMode === 'list'">
      <template #value="{ row }">
        {{ formatValueWithUnit(row) }}
      </template>
      <template #updateTime="{ row }">
        {{ row.updateTime ? formatDateTime(row.updateTime) : '-' }}
      </template>
      <template #actions="{ row }">
        <Button
          type="link"
          @click="openHistory(props.deviceId, row.identifier, row.dataType)"
        >
          查看数据
        </Button>
      </template>
    </Grid>
 
    <!-- 表单弹窗:添加/修改 -->
    <DeviceDetailsThingModelPropertyHistory
      ref="historyRef"
      :device-id="props.deviceId"
    />
  </Page>
</template>
<style scoped>
/* 移除 a-row 的额外边距 */
:deep(.ant-row) {
  margin-right: -8px !important;
  margin-left: -8px !important;
}
</style>