DeviceDetailsLog.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <ContentWrap>
  3. <!-- 搜索区域 -->
  4. <el-form :model="queryParams" inline>
  5. <el-form-item>
  6. <el-select v-model="queryParams.type" placeholder="所有" class="!w-120px">
  7. <el-option label="所有" value="" />
  8. <el-option label="状态" value="state" />
  9. <el-option label="事件" value="event" />
  10. <el-option label="属性" value="property" />
  11. <el-option label="服务" value="service" />
  12. </el-select>
  13. </el-form-item>
  14. <el-form-item>
  15. <el-input v-model="queryParams.keyword" placeholder="日志识符" class="!w-200px" />
  16. </el-form-item>
  17. <el-form-item>
  18. <el-button type="primary" @click="handleQuery">
  19. <Icon icon="ep:search" class="mr-5px" /> 搜索
  20. </el-button>
  21. <el-switch v-model="autoRefresh" class="ml-10px" /> 定时刷新
  22. </el-form-item>
  23. </el-form>
  24. <!-- 日志列表 -->
  25. <el-table v-loading="loading" :data="logList" :stripe="true" class="whitespace-nowrap">
  26. <el-table-column label="时间" align="center" prop="time" width="180">
  27. <template #default="scope">
  28. {{ formatDate(scope.row.time) }}
  29. </template>
  30. </el-table-column>
  31. <el-table-column label="类型" align="center" prop="type" width="120">
  32. <template #default="scope">
  33. <dict-tag :type="DICT_TYPE.IOT_MESSAGE_TYPE" :value="scope.row.type" />
  34. </template>
  35. </el-table-column>
  36. <el-table-column label="名称(标识符)" align="center" prop="name" />
  37. <el-table-column label="内容" align="center" prop="content" :show-overflow-tooltip="true" />
  38. </el-table>
  39. <!-- 分页 -->
  40. <div class="mt-10px flex justify-end">
  41. <el-pagination
  42. v-model:current-page="queryParams.pageNo"
  43. v-model:page-size="queryParams.pageSize"
  44. :total="total"
  45. :page-sizes="[10, 20, 50, 100]"
  46. small
  47. background
  48. layout="total, sizes, prev, pager, next, jumper"
  49. @size-change="handleQuery"
  50. @current-change="handleQuery"
  51. />
  52. </div>
  53. </ContentWrap>
  54. </template>
  55. <script setup lang="ts">
  56. import { DeviceApi } from '@/api/iot/device/device'
  57. import { DICT_TYPE } from '@/utils/dict'
  58. import { formatDate } from '@/utils/formatTime'
  59. const props = defineProps<{
  60. deviceId: number
  61. }>()
  62. // 查询参数
  63. const queryParams = reactive({
  64. type: '',
  65. keyword: '',
  66. pageNo: 1,
  67. pageSize: 20
  68. })
  69. // 列表数据
  70. const loading = ref(false)
  71. const total = ref(0)
  72. const logList = ref([])
  73. const autoRefresh = ref(false)
  74. let timer: any = null
  75. // 类型映射
  76. const typeMap = {
  77. lifetime: '生命周期',
  78. state: '设备状态',
  79. property: '属性',
  80. event: '事件',
  81. service: '服务'
  82. }
  83. /** 查询日志列表 */
  84. const getLogList = async () => {
  85. // if (!props.deviceId) return
  86. // loading.value = true
  87. // try {
  88. // const res = await DeviceApi.getDeviceLogs(props.deviceId, queryParams)
  89. // total.value = res.total
  90. // logList.value = res.list.map((item: any) => {
  91. // const log = {
  92. // time: item.time,
  93. // type: typeMap[item.type as keyof typeof typeMap] || item.type,
  94. // name: getLogName(item),
  95. // content: item.content
  96. // }
  97. // return log
  98. // })
  99. // } finally {
  100. // loading.value = false
  101. // }
  102. }
  103. /** 获取日志名称 */
  104. const getLogName = (log: any) => {
  105. const { type, identifier } = log
  106. let name = '未知'
  107. if (type === 'property') {
  108. if (identifier === 'set_reply') name = '设置回复'
  109. else if (identifier === 'report') name = '上报'
  110. else if (identifier === 'set') name = '设置'
  111. } else if (type === 'state') {
  112. name = identifier === 'online' ? '上线' : '下线'
  113. } else if (type === 'lifetime') {
  114. name = identifier === 'register' ? '注册' : name
  115. }
  116. return `${name}(${identifier})`
  117. }
  118. /** 搜索操作 */
  119. const handleQuery = () => {
  120. queryParams.pageNo = 1
  121. getLogList()
  122. }
  123. /** 监听自动刷新 */
  124. watch(autoRefresh, (newValue) => {
  125. if (newValue) {
  126. timer = setInterval(() => {
  127. getLogList()
  128. }, 5000)
  129. } else {
  130. clearInterval(timer)
  131. timer = null
  132. }
  133. })
  134. /** 监听设备ID变化 */
  135. watch(() => props.deviceId, (newValue) => {
  136. if (newValue) {
  137. handleQuery()
  138. }
  139. })
  140. /** 组件卸载时清除定时器 */
  141. onBeforeUnmount(() => {
  142. if (timer) {
  143. clearInterval(timer)
  144. }
  145. })
  146. /** 初始化 */
  147. onMounted(() => {
  148. if (props.deviceId) {
  149. getLogList()
  150. }
  151. })
  152. </script>