better-staridc-MNBT
1<template>
2 <div class="td-page">
3 <div class="td-page-head">
4 <div>
5 <h3 class="td-page-title"><i class="mdi mdi-radar"></i>监控任务</h3>
6 <p class="td-page-subtitle">管理监控任务,查看可用性指标</p>
7 </div>
8 </div>
9
10 <div class="td-table-wrap">
11 <div class="td-toolbar">
12 <t-button theme="primary" @click="openAdd">
13 <i class="mdi mdi-plus"></i> 新增任务
14 </t-button>
15 <div class="td-toolbar-spacer"></div>
16 <t-button theme="default" variant="text" @click="load">
17 <i class="mdi mdi-refresh"></i> 刷新
18 </t-button>
19 </div>
20
21 <t-table
22 row-key="id"
23 :data="rows"
24 :columns="columns"
25 :loading="loading"
26 table-layout="auto"
27 stripe
28 bordered
29 >
30 <template #empty>
31 <div class="td-empty">
32 <i class="mdi mdi-radar"></i>
33 暂无监控任务
34 </div>
35 </template>
36 <template #status="{ row }">
37 <span :class="statusClass(row.status || row.qk)">
38 {{ statusText(row.status || row.qk) }}
39 </span>
40 </template>
41 <template #operate="{ row }">
42 <div class="td-row-actions">
43 <t-button theme="default" variant="outline" size="small" @click="openEdit(row)" title="编辑">
44 <i class="mdi mdi-pencil"></i>
45 </t-button>
46 <t-button theme="danger" variant="outline" size="small" @click="del(row)" title="删除">
47 <i class="mdi mdi-delete"></i>
48 </t-button>
49 </div>
50 </template>
51 </t-table>
52 </div>
53
54 <!-- 添加 / 编辑弹窗 -->
55 <t-dialog
56 v-model:visible="dialogVisible"
57 :header="editForm.id ? '编辑任务' : '新增任务'"
58 :on-confirm="onSave"
59 width="520px"
60 :confirm-btn="{ loading: saving }"
61 >
62 <div class="td-form">
63 <div class="td-form-row">
64 <label>任务名称</label>
65 <t-input v-model="editForm.name" placeholder="请输入任务名称" clearable />
66 </div>
67 <div class="td-form-row">
68 <label>监控 URL</label>
69 <t-input v-model="editForm.url" placeholder="https://example.com/health" clearable />
70 <div class="td-form-hint">监控目标地址,需带 http:// 或 https://</div>
71 </div>
72 <div class="td-form-row">
73 <label>监控频率(秒)</label>
74 <t-input-number v-model="editForm.interval" :min="10" :step="10" theme="normal" />
75 </div>
76 <div class="td-form-switch">
77 <div class="td-form-switch-txt">
78 <strong>启用监控</strong>
79 <span>关闭后将不再检查该任务</span>
80 </div>
81 <t-switch v-model="editForm.kg" />
82 </div>
83 </div>
84 </t-dialog>
85 </div>
86</template>
87
88<script setup>
89import { ref, reactive, onMounted } from 'vue'
90import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
91import {
92 listMonitorTasks,
93 saveMonitorTask,
94 deleteMonitorTask,
95} from '@/user/api/monitor'
96
97const loading = ref(false)
98const saving = ref(false)
99const rows = ref([])
100
101const dialogVisible = ref(false)
102
103const editForm = reactive({
104 id: null,
105 name: '',
106 url: '',
107 interval: 60,
108 kg: true,
109})
110
111const columns = [
112 { colKey: 'id', title: 'ID', width: 70 },
113 { colKey: 'name', title: '任务名称', minWidth: 160, ellipsis: true },
114 { colKey: 'url', title: 'URL', minWidth: 240, ellipsis: true },
115 { colKey: 'status', title: '状态', width: 100 },
116 { colKey: 'operate', title: '操作', width: 130, fixed: 'right' },
117]
118
119function statusClass(v) {
120 if (v === true || v === 'true' || v === 1 || v === '1' || v === 'on') return 'td-chip td-chip-success'
121 if (v === false || v === 'false' || v === 0 || v === '0' || v === 'off') return 'td-chip td-chip-danger'
122 return 'td-chip td-chip-default'
123}
124
125function statusText(v) {
126 if (v === true || v === 'true' || v === 1 || v === '1' || v === 'on') return '运行中'
127 if (v === false || v === 'false' || v === 0 || v === '0' || v === 'off') return '已停止'
128 return v || '-'
129}
130
131async function load() {
132 loading.value = true
133 const r = await listMonitorTasks()
134 loading.value = false
135 if (r.ok && r.data) {
136 const d = r.data
137 rows.value = Array.isArray(d) ? d : (d.rows || d.list || d.data || [])
138 } else {
139 rows.value = []
140 }
141}
142
143function openAdd() {
144 editForm.id = null
145 editForm.name = ''
146 editForm.url = ''
147 editForm.interval = 60
148 editForm.kg = true
149 dialogVisible.value = true
150}
151
152function openEdit(row) {
153 editForm.id = row.id
154 editForm.name = row.name || ''
155 editForm.url = row.url || ''
156 editForm.interval = Number(row.interval) || 60
157 editForm.kg = row.status === true || row.status === 'true' || row.status === 1 ||
158 row.status === '1' || row.qk === true || row.qk === 'true' || row.qk === 1 || row.qk === '1' || row.kg === true
159 dialogVisible.value = true
160}
161
162async function onSave() {
163 if (!editForm.name) {
164 MessagePlugin.warning('请填写任务名称')
165 return
166 }
167 if (!editForm.url) {
168 MessagePlugin.warning('请填写监控 URL')
169 return
170 }
171 saving.value = true
172 const payload = {
173 name: editForm.name,
174 url: editForm.url,
175 interval: String(editForm.interval),
176 kg: editForm.kg ? 'true' : 'false',
177 }
178 const r = editForm.id
179 ? await saveMonitorTask({ id: editForm.id, ...payload })
180 : await saveMonitorTask(payload)
181 saving.value = false
182 if (r.ok) {
183 MessagePlugin.success(editForm.id ? '保存成功' : '添加成功')
184 dialogVisible.value = false
185 load()
186 }
187}
188
189function del(row) {
190 const dlg = DialogPlugin.confirm({
191 header: '删除任务',
192 body: `确定删除任务「${row.name || row.id}」吗?`,
193 theme: 'warning',
194 confirmBtn: { content: '删除', theme: 'danger' },
195 onConfirm: async () => {
196 const r = await deleteMonitorTask(row.id)
197 dlg.destroy()
198 if (r.ok) {
199 MessagePlugin.success('删除成功')
200 load()
201 }
202 },
203 onClose: () => dlg.destroy(),
204 })
205}
206
207onMounted(load)
208</script>
209
210<style scoped>
211.td-empty {
212 text-align: center;
213 padding: 36px 16px;
214 color: var(--td-text-placeholder);
215 font-size: 13px;
216}
217.td-empty i {
218 font-size: 36px;
219 display: block;
220 margin-bottom: 8px;
221 color: #cbd5e1;
222}
223</style>