增加通过前端页面控制扫描开始和停止

This commit is contained in:
2026-06-20 21:40:51 +08:00
parent 1e7b462c2c
commit 98b0d71dc9
2 changed files with 156 additions and 1 deletions
+123 -1
View File
@@ -1,6 +1,6 @@
<template>
<div class="power-control-page">
<h2>电源控制</h2>
<h2>电源与扫描控制</h2>
<!-- 电源状态概览 -->
<el-card class="status-card">
@@ -113,6 +113,49 @@
</div>
</el-card>
<!-- 扫描控制 -->
<el-card class="control-card scan-card">
<template #header>
<div class="card-header">
<span>扫描控制</span>
<el-tag :type="scanStatus === 'running' ? 'success' : scanStatus === 'stopped' ? 'info' : 'warning'">
{{ scanStatus === 'running' ? '扫描中' : scanStatus === 'stopped' ? '已停止' : '就绪' }}
</el-tag>
</div>
</template>
<div class="control-content">
<div class="control-description">
<p>控制扫描过程的启动与停止。</p>
<p>确保 Scanner 电源已开启后再启动扫描。</p>
</div>
<div class="control-actions">
<el-button
type="success"
:loading="scanLoading"
:disabled="scanStatus === 'running'"
@click="controlScan('start')"
>
<span class="btn-icon">▶</span> 启动扫描
</el-button>
<el-button
type="warning"
:loading="scanLoading"
:disabled="scanStatus !== 'running'"
@click="controlScan('stop')"
>
<span class="btn-icon">■</span> 停止扫描
</el-button>
</div>
<div v-if="scanStatus === 'running'" class="scan-progress">
<el-progress :percentage="scanProgress" :stroke-width="8" />
<span class="progress-text">扫描进行中...</span>
</div>
</div>
</el-card>
<!-- 操作日志 -->
<el-card class="log-card">
<template #header>
@@ -151,6 +194,12 @@ const scannerPowerStatus = ref('unknown')
const hvsLoading = ref(false)
const scannerLoading = ref(false)
// 扫描状态
const scanStatus = ref<'ready' | 'running' | 'stopped'>('ready')
const scanLoading = ref(false)
const scanProgress = ref(0)
let scanProgressInterval: number | null = null
// 操作日志
const operationLogs = ref<Array<{ time: string; message: string; type: string }>>([
{ time: new Date().toLocaleTimeString(), message: '系统已启动', type: 'info' }
@@ -223,6 +272,61 @@ const controlPower = async (device: string, action: string) => {
}
}
// 控制扫描
const controlScan = async (action: 'start' | 'stop') => {
const actionText = action === 'start' ? '启动' : '停止'
scanLoading.value = true
addLog('info', `正在${actionText}扫描...`)
try {
const response = await axios.post('/api/scanctrl', {
action: action
})
if (response.data.status === 'success') {
ElMessage.success(`扫描${actionText}成功`)
addLog('success', `扫描${actionText}成功`)
if (action === 'start') {
scanStatus.value = 'running'
startScanProgress()
} else {
scanStatus.value = 'stopped'
stopScanProgress()
}
} else {
ElMessage.error('操作失败')
addLog('error', `扫描${actionText}失败`)
}
} catch (error) {
console.error('扫描控制失败:', error)
ElMessage.error(`扫描${actionText}失败`)
addLog('error', `扫描${actionText}失败: ${error}`)
} finally {
scanLoading.value = false
}
}
// 开始扫描进度
const startScanProgress = () => {
scanProgress.value = 0
scanProgressInterval = window.setInterval(() => {
if (scanProgress.value < 95) {
scanProgress.value += Math.random() * 5
}
}, 500)
}
// 停止扫描进度
const stopScanProgress = () => {
if (scanProgressInterval) {
clearInterval(scanProgressInterval)
scanProgressInterval = null
}
scanProgress.value = 100
}
// 添加日志
const addLog = (type: string, message: string) => {
operationLogs.value.unshift({
@@ -342,6 +446,24 @@ h2 {
border-radius: 8px;
}
.scan-card {
border-left: 4px solid #409eff;
}
.scan-progress {
margin-top: 20px;
padding-top: 16px;
border-top: 1px dashed #ebeef5;
}
.progress-text {
display: block;
text-align: center;
margin-top: 8px;
color: #409eff;
font-size: 14px;
}
.control-content {
padding: 20px;
}