huang
2025-11-20 366ba040d2447bacd3455299425e3166f1f992bb
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
<template>
  <div class="execution-monitor">
    <div class="panel-header">
      <div>
        <h3>任务执行监控</h3>
        <p>实时查看最新的多设备任务</p>
      </div>
      <el-button :loading="loading" @click="fetchTasks">
        <el-icon><Refresh /></el-icon>
        刷新
      </el-button>
    </div>
 
    <el-table
      v-loading="loading"
      :data="tasks"
      height="300"
      stripe
      @row-click="handleRowClick"
    >
      <el-table-column prop="taskId" label="任务编号" min-width="160" />
      <el-table-column prop="groupId" label="设备组ID" width="120" />
      <el-table-column prop="status" label="状态" width="120">
        <template #default="{ row }">
          <el-tag :type="statusType(row.status)">{{ row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="currentStep" label="进度" width="120">
        <template #default="{ row }">
          {{ row.currentStep || 0 }} / {{ row.totalSteps || 0 }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间" min-width="160">
        <template #default="{ row }">
          {{ formatDateTime(row.startTime) }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" min-width="160">
        <template #default="{ row }">
          {{ formatDateTime(row.endTime) }}
        </template>
      </el-table-column>
    </el-table>
 
    <el-drawer v-model="drawerVisible" size="40%" title="任务步骤详情">
      <el-timeline v-loading="stepsLoading" :reverse="false">
        <el-timeline-item
          v-for="step in steps"
          :key="step.id"
          :timestamp="formatDateTime(step.startTime) || '-'"
          :type="step.status === 'COMPLETED' ? 'success' : step.status === 'FAILED' ? 'danger' : 'primary'"
        >
          <div class="step-title">{{ step.stepName }}</div>
          <div class="step-desc">状态:{{ step.status }}</div>
          <div class="step-desc">耗时:{{ formatDuration(step.durationMs) }}</div>
          <div class="step-desc" v-if="step.errorMessage">
            错误:{{ step.errorMessage }}
          </div>
        </el-timeline-item>
      </el-timeline>
    </el-drawer>
  </div>
</template>
 
<script setup>
import { onMounted, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import { multiDeviceTaskApi } from '@/api/device/multiDeviceTask'
 
const props = defineProps({
  groupId: {
    type: [String, Number],
    default: null
  }
})
 
const loading = ref(false)
const tasks = ref([])
const drawerVisible = ref(false)
const stepsLoading = ref(false)
const steps = ref([])
const currentTaskId = ref(null)
 
const fetchTasks = async () => {
  try {
    loading.value = true
    const { data } = await multiDeviceTaskApi.getTaskList({
      groupId: props.groupId,
      page: 1,
      size: 10
    })
    tasks.value = data?.records || data?.data || data || []
  } catch (error) {
    ElMessage.error(error?.message || '加载任务列表失败')
  } finally {
    loading.value = false
  }
}
 
const handleRowClick = async (row) => {
  currentTaskId.value = row.taskId
  drawerVisible.value = true
  stepsLoading.value = true
  try {
    const { data } = await multiDeviceTaskApi.getTaskSteps(row.taskId)
    steps.value = Array.isArray(data) ? data : (data?.data || [])
  } catch (error) {
    ElMessage.error(error?.message || '加载任务步骤失败')
  } finally {
    stepsLoading.value = false
  }
}
 
const statusType = (status) => {
  switch ((status || '').toUpperCase()) {
    case 'COMPLETED':
      return 'success'
    case 'FAILED':
      return 'danger'
    case 'RUNNING':
      return 'warning'
    default:
      return 'info'
  }
}
 
const formatDuration = (ms) => {
  if (!ms) return '-'
  if (ms < 1000) return `${ms} ms`
  return `${(ms / 1000).toFixed(1)} s`
}
 
// 格式化日期时间
const formatDateTime = (dateTime) => {
  if (!dateTime) return '-'
  try {
    const date = new Date(dateTime)
    // 检查日期是否有效
    if (isNaN(date.getTime())) {
      return dateTime // 如果无法解析,返回原始值
    }
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0')
    const seconds = String(date.getSeconds()).padStart(2, '0')
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  } catch (error) {
    console.warn('格式化时间失败:', dateTime, error)
    return dateTime
  }
}
 
watch(
  () => props.groupId,
  () => {
    fetchTasks()
  },
  { immediate: true }
)
 
onMounted(fetchTasks)
 
defineExpose({
  fetchTasks
})
</script>
 
<style scoped>
.execution-monitor {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(15, 18, 63, 0.08);
}
 
.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
 
.panel-header h3 {
  margin: 0;
}
 
.panel-header p {
  margin: 4px 0 0;
  color: #909399;
  font-size: 13px;
}
 
.step-title {
  font-weight: 600;
  margin-bottom: 4px;
}
 
.step-desc {
  font-size: 13px;
  color: #606266;
}
</style>