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
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
 
import {
  computed,
  onBeforeUnmount,
  onMounted,
  reactive,
  ref,
  watch,
} from 'vue';
 
import { Page } from '..\..\..\..\..\..\packages\effects\common-ui\src';
import { DICT_TYPE, IotDeviceMessageMethodEnum } from '..\..\..\..\..\..\packages\constants\src';
import { IconifyIcon } from '..\..\..\..\..\..\packages\icons\src';
import { formatDateTime } from '..\..\..\..\..\..\packages\utils\src';
 
import { Button, Select, Space, Switch, Tag } from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getDeviceMessagePage } from '#/api/iot/device/device';
 
const props = defineProps<{
  deviceId: number;
}>();
 
const queryParams = reactive({
  method: undefined,
  upstream: undefined,
}); // 查询参数
 
const autoRefresh = ref(false); // 自动刷新开关
let autoRefreshTimer: any = null; // 自动刷新定时器
let refreshTimer: ReturnType<typeof setTimeout> | undefined; // 延迟刷新定时器
 
/** 消息方法选项 */
const methodOptions = computed(() => {
  return Object.values(IotDeviceMessageMethodEnum).map((item) => ({
    label: item.name,
    value: item.method,
  }));
});
 
/** Grid 列定义 */
function useGridColumns(): VxeTableGridOptions<Record<string, any>>['columns'] {
  return [
    {
      field: 'ts',
      title: '时间',
      width: 160,
      slots: { default: 'ts' },
    },
    {
      field: 'upstream',
      title: '上行/下行',
      width: 100,
      slots: { default: 'upstream' },
    },
    {
      field: 'reply',
      title: '是否回复',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'requestId',
      title: '请求编号',
      width: 280,
      showOverflow: 'tooltip',
    },
    {
      field: 'method',
      title: '请求方法',
      width: 120,
      slots: { default: 'method' },
    },
    {
      field: 'params',
      title: '请求/响应数据',
      minWidth: 200,
      showOverflow: 'tooltip',
      slots: { default: 'params' },
    },
  ];
}
 
/** 创建 Grid 实例 */
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    proxyConfig: {
      ajax: {
        query: async ({ page }) => {
          if (!props.deviceId) {
            return { list: [], total: 0 };
          }
          return await getDeviceMessagePage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            deviceId: props.deviceId,
            method: queryParams.method,
            upstream: queryParams.upstream,
          });
        },
      },
    },
    toolbarConfig: {
      refresh: false,
      search: false,
    },
    pagerConfig: {
      enabled: true,
    },
  } as VxeTableGridOptions,
});
 
/** 搜索操作 */
function handleQuery() {
  gridApi.query();
}
 
/** 监听自动刷新 */
watch(autoRefresh, (newValue) => {
  if (newValue) {
    autoRefreshTimer = setInterval(() => {
      gridApi.query();
    }, 5000);
  } else {
    clearInterval(autoRefreshTimer);
    autoRefreshTimer = null;
  }
});
 
/** 监听设备标识变化 */
watch(
  () => props.deviceId,
  (newValue) => {
    if (newValue) {
      handleQuery();
    }
  },
);
 
/** 组件卸载时清除定时器 */
onBeforeUnmount(() => {
  if (autoRefreshTimer) {
    clearInterval(autoRefreshTimer);
    autoRefreshTimer = null;
  }
  if (refreshTimer) {
    clearTimeout(refreshTimer);
    refreshTimer = undefined;
  }
});
 
/** 初始化 */
onMounted(() => {
  if (props.deviceId) {
    handleQuery();
  }
});
 
/** 刷新消息列表 */
function refresh(delay = 0) {
  if (refreshTimer) {
    clearTimeout(refreshTimer);
    refreshTimer = undefined;
  }
  if (delay > 0) {
    refreshTimer = setTimeout(() => {
      gridApi.query();
      refreshTimer = undefined;
    }, delay);
  } else {
    gridApi.query();
  }
}
 
/** 暴露方法给父组件 */
defineExpose({
  refresh,
});
</script>
 
<template>
  <Page auto-content-height>
    <!-- 搜索区域 -->
    <div class="mb-4 flex flex-wrap items-center gap-3">
      <Select
        v-model:value="queryParams.method"
        allow-clear
        placeholder="所有方法"
        style="width: 160px"
      >
        <Select.Option
          v-for="item in methodOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          {{ item.label }}
        </Select.Option>
      </Select>
      <Select
        v-model:value="queryParams.upstream"
        allow-clear
        placeholder="上行/下行"
        style="width: 160px"
      >
        <Select.Option label="上行" value="true">上行</Select.Option>
        <Select.Option label="下行" value="false">下行</Select.Option>
      </Select>
      <Space>
        <Button type="primary" @click="handleQuery">
          <IconifyIcon icon="ep:search" class="mr-[5px]" /> 搜索
        </Button>
        <Switch
          v-model:checked="autoRefresh"
          checked-children="定时刷新"
          un-checked-children="定时刷新"
        />
      </Space>
    </div>
 
    <!-- 消息列表 -->
    <Grid>
      <template #ts="{ row }">
        {{ formatDateTime(row.ts) }}
      </template>
      <template #upstream="{ row }">
        <Tag :color="row.upstream ? 'blue' : 'green'">
          {{ row.upstream ? '上行' : '下行' }}
        </Tag>
      </template>
      <template #method="{ row }">
        {{ methodOptions.find((item) => item.value === row.method)?.label }}
      </template>
      <template #params="{ row }">
        <span v-if="row.reply">
          {{ `{"code":${row.code},"msg":"${row.msg}","data":${row.data}\}` }}
        </span>
        <span v-else>{{ row.params }}</span>
      </template>
    </Grid>
  </Page>
</template>