DeviceDetailsLog.vue 4.2 KB

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