OperationHistory.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div class="flex flex-col gap-2">
  3. <div class="flex gap-2 items-center">
  4. <span class="font-bold">操作历史</span>
  5. <span class="text-xs text-gray-500">(所有人共享{{ gatewaySn ? `,网关 ${gatewaySn}` : '' }})</span>
  6. <div class="flex-1" />
  7. <NButton size="small" quaternary :loading="loading" @click="load">刷新</NButton>
  8. </div>
  9. <NDataTable
  10. size="small"
  11. remote
  12. :columns="columns"
  13. :data="rows"
  14. :loading="loading"
  15. :pagination="pagination"
  16. :row-key="(r: FaultRecordOpSummary) => r.id"
  17. :row-props="rowProps"
  18. />
  19. </div>
  20. </template>
  21. <script setup lang="ts">
  22. import { h, reactive, ref, watch } from 'vue'
  23. import { NButton, NDataTable, NTag, NTooltip, useMessage } from 'naive-ui'
  24. import type { DataTableColumns } from 'naive-ui'
  25. import { getFaultRecordOperations } from '@/service/faultRecord'
  26. import type {
  27. FaultRecordEnv,
  28. FaultRecordKind,
  29. FaultRecordOpSummary,
  30. } from '@/service/faultRecord/interface'
  31. import { KIND_LABEL, STATUS_META, errText, fmtTime } from './constants'
  32. const props = defineProps<{
  33. env: FaultRecordEnv
  34. kind: FaultRecordKind | string // 可多个,逗号分隔
  35. gatewaySn?: string
  36. refreshKey?: number
  37. }>()
  38. const emit = defineEmits<{ select: [row: FaultRecordOpSummary] }>()
  39. const slots = defineSlots<{ actions?: (p: { row: FaultRecordOpSummary }) => any }>()
  40. const message = useMessage()
  41. const loading = ref(false)
  42. const rows = ref<FaultRecordOpSummary[]>([])
  43. const pagination = reactive({
  44. page: 1,
  45. pageSize: 10,
  46. itemCount: 0,
  47. showSizePicker: true,
  48. pageSizes: [10, 20, 50],
  49. onChange: (p: number) => {
  50. pagination.page = p
  51. load()
  52. },
  53. onUpdatePageSize: (s: number) => {
  54. pagination.pageSize = s
  55. pagination.page = 1
  56. load()
  57. },
  58. })
  59. const columns: DataTableColumns<FaultRecordOpSummary> = [
  60. { title: '时间', key: 'createdAt', width: 160, render: (r) => fmtTime(r.createdAt) },
  61. { title: '操作人', key: 'operator', width: 110, ellipsis: { tooltip: true } },
  62. { title: '网关', key: 'gatewaySn', width: 180, ellipsis: { tooltip: true } },
  63. { title: '设备', key: 'deviceSn', width: 200, render: (r) => `${r.deviceType} ${r.deviceSn}` },
  64. { title: '操作', key: 'kind', width: 90, render: (r) => KIND_LABEL[r.kind] },
  65. { title: '参数', key: 'summary', ellipsis: { tooltip: true } },
  66. {
  67. title: '状态',
  68. key: 'status',
  69. width: 110,
  70. render: (r) => {
  71. const meta = STATUS_META[r.status]
  72. const tag = h(NTag, { type: meta.type, size: 'small' }, { default: () => meta.label })
  73. return r.error
  74. ? h(NTooltip, null, { trigger: () => tag, default: () => r.error })
  75. : tag
  76. },
  77. },
  78. {
  79. title: '',
  80. key: 'actions',
  81. width: 200,
  82. render: (r) =>
  83. h('div', { class: 'flex gap-1', onClick: (e: Event) => e.stopPropagation() }, [
  84. h(NButton, { size: 'tiny', onClick: () => emit('select', r) }, { default: () => '查看' }),
  85. slots.actions?.({ row: r }),
  86. ]),
  87. },
  88. ]
  89. const rowProps = (r: FaultRecordOpSummary) => ({
  90. style: 'cursor: pointer',
  91. onClick: () => emit('select', r),
  92. })
  93. async function load() {
  94. loading.value = true
  95. try {
  96. const res = await getFaultRecordOperations(
  97. props.env,
  98. pagination.page,
  99. pagination.pageSize,
  100. props.gatewaySn ?? '',
  101. props.kind,
  102. )
  103. rows.value = res.data ?? []
  104. pagination.itemCount = res.total
  105. } catch (e) {
  106. message.error(`获取操作历史失败:${errText(e)}`)
  107. } finally {
  108. loading.value = false
  109. }
  110. }
  111. watch(
  112. () => [props.env, props.gatewaySn, props.kind, props.refreshKey],
  113. () => {
  114. pagination.page = 1
  115. load()
  116. },
  117. { immediate: true },
  118. )
  119. defineExpose({ load })
  120. </script>