This commit is contained in:
ldh
2026-11-17 16:28:24 +08:00
parent b30d9f92ce
commit 29e90bbc7b
25 changed files with 3891 additions and 46 deletions
+104
View File
@@ -0,0 +1,104 @@
/**
* 靶样轮廓识别与采样点规划接口封装。
* 后端实现见 HardwareWeb/Controllers/SampleAnalysisController.cs:
* POST /api/sample/baseline -> { width, height, capturedAt }
* POST /api/sample/target -> { width, height, capturedAt }
* POST /api/sample/analyze -> { imageBase64, mimeType, width, height, samplingPointCount, samplingPoints }
*/
export interface SampleFrameResult {
width: number
height: number
capturedAt: string
}
export interface SamplePoint {
x: number
y: number
}
export interface SampleAnalysisResult {
imageBase64: string
mimeType: string
width: number
height: number
samplingPointCount: number
samplingPoints: SamplePoint[]
}
const API_BASE = (import.meta.env.VITE_API_BASE ?? '').replace(/\/$/, '')
const DEFAULT_TIMEOUT = 15000
const ANALYZE_TIMEOUT = 30000
export class SampleApiError extends Error {
readonly status: number
constructor(message: string, status = 0) {
super(message)
this.name = 'SampleApiError'
this.status = status
}
}
async function request<T>(
path: string,
init?: RequestInit,
timeout = DEFAULT_TIMEOUT,
): Promise<T> {
const controller = new AbortController()
const timer = window.setTimeout(() => controller.abort(), timeout)
try {
const response = await fetch(`${API_BASE}${path}`, {
...init,
cache: 'no-store',
signal: controller.signal,
headers: {
Accept: 'application/json',
...(init?.headers ?? {}),
},
})
if (!response.ok) {
let detail = `HTTP ${response.status}`
try {
const body = (await response.json()) as { message?: string; error?: string }
detail = body.message ?? body.error ?? detail
} catch {
// 响应不是 JSON 时保留状态码信息
}
throw new SampleApiError(detail, response.status)
}
return (await response.json()) as T
} catch (error) {
if (error instanceof SampleApiError) throw error
if (error instanceof DOMException && error.name === 'AbortError') {
throw new SampleApiError('图像分析服务响应超时')
}
throw new SampleApiError(
error instanceof Error ? error.message : '无法连接图像分析服务',
)
} finally {
window.clearTimeout(timer)
}
}
/** 相机画面稳定后采集基准画面。 */
export function captureSampleBaseline(): Promise<SampleFrameResult> {
return request<SampleFrameResult>('/api/sample/baseline', { method: 'POST' })
}
/** 放置靶样并点击开始检测时采集第二张画面。 */
export function captureSampleTarget(): Promise<SampleFrameResult> {
return request<SampleFrameResult>('/api/sample/target', { method: 'POST' })
}
/** 提取靶样轮廓、绘制采样点并返回处理后的图像。 */
export function analyzeSample(): Promise<SampleAnalysisResult> {
return request<SampleAnalysisResult>(
'/api/sample/analyze',
{ method: 'POST' },
ANALYZE_TIMEOUT,
)
}
@@ -0,0 +1,257 @@
<script setup lang="ts">
import { nextTick, onBeforeUnmount, ref, watch } from 'vue'
const props = defineProps<{
open: boolean
}>()
const emit = defineEmits<{
close: []
'system-config': []
ruler: []
}>()
const firstButton = ref<HTMLButtonElement | null>(null)
function close() {
emit('close')
}
function onKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') close()
}
watch(
() => props.open,
async (open) => {
if (open) {
window.addEventListener('keydown', onKeydown)
await nextTick()
firstButton.value?.focus()
} else {
window.removeEventListener('keydown', onKeydown)
}
},
)
onBeforeUnmount(() => {
window.removeEventListener('keydown', onKeydown)
})
</script>
<template>
<Teleport to="body">
<Transition name="function-panel">
<div v-if="open" class="panel-backdrop" @click.self="close">
<section
class="panel-card"
role="dialog"
aria-modal="true"
aria-label="管理员功能选择"
>
<header class="panel-card__header">
<h2>管理员功能</h2>
<p>请选择需要进入的功能</p>
</header>
<div class="panel-grid">
<button
ref="firstButton"
class="function-card"
type="button"
@click="emit('system-config')"
>
<span class="function-card__icon" aria-hidden="true">
<svg viewBox="0 0 64 64">
<path
d="M26 8h12l2 7a20 20 0 0 1 5 3l7-1 6 10-5 5a20 20 0 0 1 0 6l5 5-6 10-7-1a20 20 0 0 1-5 3l-2 7H26l-2-7a20 20 0 0 1-5-3l-7 1-6-10 5-5a20 20 0 0 1 0-6l-5-5 6-10 7 1a20 20 0 0 1 5-3Z"
fill="none"
stroke="currentColor"
stroke-width="3"
stroke-linejoin="round"
/>
<circle
cx="32"
cy="32"
r="8"
fill="none"
stroke="currentColor"
stroke-width="3"
/>
</svg>
</span>
<span class="function-card__label">参数设置</span>
<span class="function-card__desc">管理系统运行参数</span>
</button>
<button
class="function-card"
type="button"
@click="emit('ruler')"
>
<span class="function-card__icon" aria-hidden="true">
<svg viewBox="0 0 64 64">
<rect
x="7"
y="22"
width="50"
height="20"
rx="3"
fill="none"
stroke="currentColor"
stroke-width="3"
transform="rotate(-12 32 32)"
/>
<path
d="M16 26v7M24 24v7M32 22v7M40 20v7M48 18v7"
fill="none"
stroke="currentColor"
stroke-width="3"
stroke-linecap="round"
transform="rotate(-12 32 32)"
/>
</svg>
</span>
<span class="function-card__label">标尺设定</span>
<span class="function-card__desc">配置标尺与校准参数</span>
</button>
</div>
<button class="panel-card__close" type="button" @click="close">
取消
</button>
</section>
</div>
</Transition>
</Teleport>
</template>
<style scoped>
.panel-backdrop {
position: fixed;
inset: 0;
z-index: 70;
display: grid;
place-items: center;
padding: 24px;
background: rgba(18, 33, 54, 0.44);
backdrop-filter: blur(5px);
}
.panel-card {
width: 820px;
padding: 42px 46px 34px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 24px;
background: #fff;
box-shadow: 0 30px 80px rgba(12, 30, 55, 0.3);
text-align: center;
}
.panel-card__header h2 {
color: #1f2937;
font-size: 32px;
font-weight: 600;
letter-spacing: 2px;
}
.panel-card__header p {
margin-top: 12px;
color: #778394;
font-size: 19px;
letter-spacing: 1px;
}
.panel-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 24px;
margin-top: 32px;
}
.function-card {
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
padding: 30px 24px 28px;
border: 2px solid var(--line);
border-radius: 18px;
background: linear-gradient(180deg, #ffffff 0%, #f7f9fc 100%);
transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}
.function-card:hover {
border-color: #b9d1f3;
box-shadow: 0 16px 34px rgba(47, 111, 208, 0.16);
transform: translateY(-3px);
}
.function-card__icon {
display: grid;
place-items: center;
width: 88px;
height: 88px;
border-radius: 50%;
color: var(--blue);
background: linear-gradient(145deg, #f1f7ff, #dfeaff);
box-shadow: inset 0 1px 0 #fff, 0 8px 22px rgba(47, 111, 208, 0.14);
}
.function-card__icon svg {
width: 48px;
height: 48px;
}
.function-card__label {
color: #1f2937;
font-size: 26px;
font-weight: 600;
letter-spacing: 2px;
}
.function-card__desc {
color: var(--muted);
font-size: 17px;
letter-spacing: 0.5px;
}
.panel-card__close {
width: 180px;
height: 54px;
margin-top: 30px;
border: 2px solid var(--line-strong);
border-radius: 12px;
color: var(--ink-soft);
font-size: 19px;
font-weight: 600;
letter-spacing: 3px;
background: #fff;
transition: background 0.2s ease;
}
.panel-card__close:hover {
background: var(--surface-soft);
}
.function-panel-enter-active,
.function-panel-leave-active {
transition: opacity 0.24s ease;
}
.function-panel-enter-active .panel-card,
.function-panel-leave-active .panel-card {
transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.24s ease;
}
.function-panel-enter-from,
.function-panel-leave-to {
opacity: 0;
}
.function-panel-enter-from .panel-card,
.function-panel-leave-to .panel-card {
opacity: 0;
transform: translateY(16px) scale(0.97);
}
</style>
+36 -1
View File
@@ -1,14 +1,21 @@
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { connectCamera } from '@/api/camera'
import { useCameraStatus } from '@/composables/useCameraStatus'
import { useCameraStream } from '@/composables/useCameraStream'
import cameraPlaceholder from '@/assets/camera-placeholder.webp'
const emit = defineEmits<{
/** 相机画面稳定显示后触发一次,用于采集基准画面。 */
ready: []
}>()
const { state: cameraState, refresh, startPolling } = useCameraStatus(5000, false)
const { state: streamState, frameUrl, isLive, connect } = useCameraStream()
const connectingCamera = ref(false)
const readyEmitted = ref(false)
let readyTimer: number | undefined
const statusText = computed(() => {
if (isLive.value) return '实时画面'
@@ -32,6 +39,34 @@ const statusTone = computed<'on' | 'wait' | 'off'>(() => {
return 'off'
})
watch(isLive, (live) => {
if (readyTimer !== undefined) {
window.clearTimeout(readyTimer)
readyTimer = undefined
}
if (!live) {
readyEmitted.value = false
return
}
if (readyEmitted.value) return
// 画面连续显示 800ms 视为稳定,再通知外部采集基准画面
readyTimer = window.setTimeout(() => {
readyTimer = undefined
if (!isLive.value || readyEmitted.value) return
readyEmitted.value = true
emit('ready')
}, 800)
})
onBeforeUnmount(() => {
if (readyTimer !== undefined) {
window.clearTimeout(readyTimer)
}
})
onMounted(async () => {
await refresh()
+6
View File
@@ -45,6 +45,12 @@ const router = createRouter({
component: () => import('@/views/SystemConfigView.vue'),
meta: { title: '系统参数管理', requiresAdmin: true },
},
{
path: '/ruler',
name: 'ruler',
component: () => import('@/views/RulerSettingView.vue'),
meta: { title: '标尺设定', requiresAdmin: true },
},
{
path: '/:pathMatch(.*)*',
redirect: { name: 'home' },
+19
View File
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue'
import { useRouter } from 'vue-router'
import AdminFunctionPanel from '@/components/AdminFunctionPanel.vue'
import AdminPasswordModal from '@/components/AdminPasswordModal.vue'
import AppStage from '@/components/AppStage.vue'
import ModuleButton from '@/components/ModuleButton.vue'
@@ -29,6 +30,7 @@ const router = useRouter()
const modalOpen = ref(false)
const modalModule = ref('')
const adminModalOpen = ref(false)
const adminPanelOpen = ref(false)
// 隐藏入口:800ms 内连续点击 Logo 三次,打开管理员密码框。
const LOGO_CLICK_WINDOW = 800
@@ -57,9 +59,19 @@ function onLogoClick() {
function onAdminSuccess() {
adminModalOpen.value = false
adminPanelOpen.value = true
}
function openSystemConfig() {
adminPanelOpen.value = false
void router.push({ name: 'systemConfig' })
}
function openRulerSetting() {
adminPanelOpen.value = false
void router.push({ name: 'ruler' })
}
onBeforeUnmount(() => {
if (logoTimer !== undefined) {
window.clearTimeout(logoTimer)
@@ -121,6 +133,13 @@ function openModule(module: HomeModule) {
@close="adminModalOpen = false"
@success="onAdminSuccess"
/>
<AdminFunctionPanel
:open="adminPanelOpen"
@close="adminPanelOpen = false"
@system-config="openSystemConfig"
@ruler="openRulerSetting"
/>
</AppStage>
</template>
+728
View File
@@ -0,0 +1,728 @@
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import AppStage from '@/components/AppStage.vue'
import PageHeader from '@/components/PageHeader.vue'
import { connectCamera } from '@/api/camera'
import {
SystemConfigApiError,
clearAdminSession,
createSystemConfig,
listSystemConfigs,
updateSystemConfig,
} from '@/api/systemConfig'
import { useCameraStatus } from '@/composables/useCameraStatus'
import { useCameraStream } from '@/composables/useCameraStream'
import { showToast } from '@/composables/useToast'
import cameraPlaceholder from '@/assets/camera-placeholder.webp'
interface Point {
x: number
y: number
}
const SCALE_CONFIG_NAME = 'camera_scale'
const MIN_LINE_PIXELS = 4
const router = useRouter()
const { state: cameraState, refresh, startPolling } = useCameraStatus(5000, false)
const { state: streamState, frameUrl, isLive, connect } = useCameraStream()
const viewport = ref<HTMLElement | null>(null)
const overlay = ref<SVGSVGElement | null>(null)
// 相机原始画面尺寸(像素),用于把显示坐标换算成采集画面像素
const frameWidth = ref(0)
const frameHeight = ref(0)
const overlayRect = ref({ left: 0, top: 0, width: 0, height: 0 })
const startPoint = ref<Point | null>(null)
const endPoint = ref<Point | null>(null)
const drawing = ref(false)
const distanceModalOpen = ref(false)
const distanceInput = ref('')
const inputError = ref('')
const saving = ref(false)
const distanceField = ref<HTMLInputElement | null>(null)
const statusText = computed(() => {
if (isLive.value) return '实时画面'
if (
streamState.value === 'connecting' ||
cameraState.value === 'checking'
) {
return '连接中'
}
if (cameraState.value === 'offline') return '相机服务离线'
return '等待画面'
})
const overlayStyle = computed(() => ({
left: `${overlayRect.value.left}px`,
top: `${overlayRect.value.top}px`,
width: `${overlayRect.value.width}px`,
height: `${overlayRect.value.height}px`,
}))
const pixelDistance = computed(() => {
if (!startPoint.value || !endPoint.value) return 0
return Math.hypot(
endPoint.value.x - startPoint.value.x,
endPoint.value.y - startPoint.value.y,
)
})
const handleRadius = computed(() => Math.max(6, frameWidth.value * 0.006))
function goHome() {
void router.replace({ name: 'home' })
}
/** 根据相机原始比例计算画面在容器内的实际显示区域 */
function updateOverlayRect() {
const element = viewport.value
if (!element || !frameWidth.value || !frameHeight.value) return
const viewWidth = element.clientWidth
const viewHeight = element.clientHeight
if (!viewWidth || !viewHeight) return
const aspect = frameWidth.value / frameHeight.value
let width = viewWidth
let height = viewHeight
if (viewWidth / viewHeight > aspect) {
width = viewHeight * aspect
} else {
height = viewWidth / aspect
}
overlayRect.value = {
left: (viewWidth - width) / 2,
top: (viewHeight - height) / 2,
width,
height,
}
}
function onImageLoad(event: Event) {
if (!frameUrl.value) return
const image = event.target as HTMLImageElement
if (!image.naturalWidth || !image.naturalHeight) return
frameWidth.value = image.naturalWidth
frameHeight.value = image.naturalHeight
updateOverlayRect()
}
function pointFromEvent(event: PointerEvent): Point | null {
const element = overlay.value
if (!element || !frameWidth.value || !frameHeight.value) return null
const rect = element.getBoundingClientRect()
if (!rect.width || !rect.height) return null
return {
x: ((event.clientX - rect.left) / rect.width) * frameWidth.value,
y: ((event.clientY - rect.top) / rect.height) * frameHeight.value,
}
}
function clearLine() {
startPoint.value = null
endPoint.value = null
drawing.value = false
}
function onPointerDown(event: PointerEvent) {
if (!isLive.value || distanceModalOpen.value || event.button !== 0) return
const point = pointFromEvent(event)
if (!point) return
startPoint.value = point
endPoint.value = point
drawing.value = true
overlay.value?.setPointerCapture(event.pointerId)
}
function onPointerMove(event: PointerEvent) {
if (!drawing.value) return
const point = pointFromEvent(event)
if (point) endPoint.value = point
}
function finishDrawing() {
if (!drawing.value) return
drawing.value = false
if (pixelDistance.value < MIN_LINE_PIXELS) {
clearLine()
return
}
distanceInput.value = ''
inputError.value = ''
distanceModalOpen.value = true
}
function onPointerUp(event: PointerEvent) {
if (overlay.value?.hasPointerCapture(event.pointerId)) {
overlay.value.releasePointerCapture(event.pointerId)
}
finishDrawing()
}
function onPointerCancel(event: PointerEvent) {
if (overlay.value?.hasPointerCapture(event.pointerId)) {
overlay.value.releasePointerCapture(event.pointerId)
}
finishDrawing()
}
function cancelDistanceInput() {
if (saving.value) return
distanceModalOpen.value = false
clearLine()
}
async function confirmDistance() {
if (saving.value) return
const distanceMm = Number(distanceInput.value)
if (!Number.isFinite(distanceMm) || distanceMm <= 0) {
inputError.value = '请输入大于 0 的实际距离'
return
}
if (pixelDistance.value < MIN_LINE_PIXELS) {
inputError.value = '线段无效,请重新绘制'
return
}
saving.value = true
inputError.value = ''
try {
const scale = pixelDistance.value / distanceMm
const scaleValue = scale.toFixed(2)
const matches = await listSystemConfigs(SCALE_CONFIG_NAME)
const existing = matches.find(
(item) => item.configName.trim().toLowerCase() === SCALE_CONFIG_NAME,
)
if (existing) {
await updateSystemConfig(existing.configId, {
configClass: existing.configClass || '相机',
configName: SCALE_CONFIG_NAME,
configValue: scaleValue,
configDescript: existing.configDescript || '标尺比例(像素/毫米)',
})
} else {
await createSystemConfig({
configClass: '相机',
configName: SCALE_CONFIG_NAME,
configValue: scaleValue,
configDescript: '标尺比例(像素/毫米)',
})
}
distanceModalOpen.value = false
showToast(`标尺设定完成:${scaleValue} px/mm`)
goHome()
} catch (error) {
if (error instanceof SystemConfigApiError && error.status === 401) {
clearAdminSession()
showToast('管理员凭证已过期,请重新进入标尺设定')
goHome()
return
}
inputError.value = error instanceof Error ? error.message : '标尺保存失败'
} finally {
saving.value = false
}
}
function onModalKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') cancelDistanceInput()
}
watch(distanceModalOpen, async (open) => {
if (open) {
window.addEventListener('keydown', onModalKeydown)
await nextTick()
distanceField.value?.focus()
} else {
window.removeEventListener('keydown', onModalKeydown)
}
})
watch(frameUrl, (url) => {
if (!url) {
clearLine()
frameWidth.value = 0
frameHeight.value = 0
}
})
onMounted(async () => {
await refresh()
const streamPromise = connect()
if (cameraState.value === 'disconnected') {
try {
await connectCamera()
await refresh()
} catch {
// 没有相机或服务不可达时保留占位画面
}
}
startPolling()
await streamPromise
})
onBeforeUnmount(() => {
window.removeEventListener('keydown', onModalKeydown)
})
</script>
<template>
<AppStage>
<PageHeader title="标尺设定" subtitle="在相机画面中拖动画线,输入实际距离完成标定">
<template #left>
<button class="header-button header-button--outline" type="button" @click="goHome">
返回
</button>
</template>
</PageHeader>
<main class="ruler-main">
<section class="ruler-card">
<header class="ruler-card__header">
<h2>相机画面</h2>
<span class="ruler-card__status">
<i class="ruler-card__dot" :class="{ 'ruler-card__dot--on': isLive }"></i>
{{ statusText }}
</span>
</header>
<div ref="viewport" class="ruler-viewport">
<img
class="ruler-viewport__image"
:src="frameUrl ?? cameraPlaceholder"
:alt="frameUrl ? '相机实时画面' : '相机预览画面'"
draggable="false"
@load="onImageLoad"
/>
<svg
v-if="frameUrl && frameWidth && frameHeight"
ref="overlay"
class="ruler-overlay"
:style="overlayStyle"
:viewBox="`0 0 ${frameWidth} ${frameHeight}`"
preserveAspectRatio="none"
@pointerdown="onPointerDown"
@pointermove="onPointerMove"
@pointerup="onPointerUp"
@pointercancel="onPointerCancel"
>
<line
v-if="startPoint && endPoint"
class="ruler-line"
:x1="startPoint.x"
:y1="startPoint.y"
:x2="endPoint.x"
:y2="endPoint.y"
vector-effect="non-scaling-stroke"
/>
<circle
v-if="startPoint"
class="ruler-handle"
:cx="startPoint.x"
:cy="startPoint.y"
:r="handleRadius"
/>
<circle
v-if="endPoint"
class="ruler-handle"
:cx="endPoint.x"
:cy="endPoint.y"
:r="handleRadius"
/>
</svg>
<p v-if="!isLive" class="ruler-viewport__tip">等待相机画面...</p>
</div>
<footer class="ruler-card__footer">
<span class="ruler-card__step">1</span>
<span>按住鼠标左键,沿已知实际长度的物体拖动画线</span>
<span class="ruler-card__divider"></span>
<span class="ruler-card__step">2</span>
<span>松开后输入该线段的实际距离(mm),确认后自动计算比例尺</span>
</footer>
</section>
</main>
<Teleport to="body">
<Transition name="distance-modal">
<div v-if="distanceModalOpen" class="distance-backdrop" @click.self="cancelDistanceInput">
<section class="distance-card" role="dialog" aria-modal="true" aria-label="输入实际距离">
<h2>输入实际距离</h2>
<p>线段像素长度:<strong>{{ pixelDistance.toFixed(1) }} px</strong></p>
<form class="distance-form" @submit.prevent="confirmDistance">
<label class="distance-field">
<span>实际距离</span>
<div class="distance-field__control">
<input
ref="distanceField"
v-model="distanceInput"
type="number"
min="0"
step="0.01"
inputmode="decimal"
placeholder="例如:100"
:disabled="saving"
/>
<span class="distance-field__unit">mm</span>
</div>
</label>
<p v-if="inputError" class="distance-error">{{ inputError }}</p>
<div class="distance-actions">
<button
class="distance-button distance-button--ghost"
type="button"
:disabled="saving"
@click="cancelDistanceInput"
>
重画
</button>
<button
class="distance-button distance-button--primary"
type="submit"
:disabled="saving"
>
{{ saving ? '保存中...' : '确定' }}
</button>
</div>
</form>
</section>
</div>
</Transition>
</Teleport>
</AppStage>
</template>
<style scoped>
.ruler-main {
position: absolute;
top: 176px;
right: 44px;
bottom: 40px;
left: 44px;
display: flex;
justify-content: center;
}
.ruler-card {
display: flex;
flex-direction: column;
width: 1400px;
max-width: 100%;
height: 100%;
overflow: hidden;
border: 1px solid #d8e0e9;
border-radius: 18px;
background: #fff;
box-shadow: var(--shadow-card);
}
.ruler-card__header {
display: flex;
flex: none;
align-items: center;
justify-content: space-between;
height: 76px;
padding: 0 28px;
background: linear-gradient(180deg, #3d80e2 0%, #2f6fd0 100%);
}
.ruler-card__header h2 {
color: #fff;
font-size: 30px;
font-weight: 600;
letter-spacing: 3px;
text-shadow: 0 1px 3px rgba(16, 49, 101, 0.22);
}
.ruler-card__status {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 5px 12px;
border: 1px solid rgba(255, 255, 255, 0.24);
border-radius: 30px;
background: rgba(255, 255, 255, 0.12);
color: rgba(255, 255, 255, 0.92);
font-size: 15px;
letter-spacing: 1px;
}
.ruler-card__dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #c9d4e4;
}
.ruler-card__dot--on {
background: #74f4c5;
box-shadow: 0 0 9px rgba(116, 244, 197, 0.9);
}
.ruler-viewport {
position: relative;
flex: 1;
min-height: 0;
overflow: hidden;
background: #080d14;
}
.ruler-viewport__image {
width: 100%;
height: 100%;
object-fit: contain;
user-select: none;
}
.ruler-overlay {
position: absolute;
touch-action: none;
cursor: crosshair;
}
.ruler-line {
stroke: #74f1df;
stroke-width: 3;
stroke-linecap: round;
filter: drop-shadow(0 0 4px rgba(116, 241, 223, 0.75));
}
.ruler-handle {
fill: #74f1df;
stroke: rgba(8, 13, 20, 0.65);
stroke-width: 2;
}
.ruler-viewport__tip {
position: absolute;
top: 50%;
left: 50%;
color: rgba(216, 228, 243, 0.7);
font-size: 20px;
letter-spacing: 2px;
transform: translate(-50%, -50%);
}
.ruler-card__footer {
display: flex;
flex: none;
align-items: center;
gap: 12px;
height: 72px;
padding: 0 30px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 18px;
letter-spacing: 0.5px;
}
.ruler-card__step {
display: grid;
place-items: center;
width: 28px;
height: 28px;
border-radius: 50%;
background: var(--blue-soft);
color: var(--blue);
font-size: 16px;
font-weight: 600;
}
.ruler-card__divider {
width: 1px;
height: 22px;
margin: 0 8px;
background: var(--line-strong);
}
.distance-backdrop {
position: fixed;
inset: 0;
z-index: 70;
display: grid;
place-items: center;
padding: 24px;
background: rgba(18, 33, 54, 0.44);
backdrop-filter: blur(5px);
}
.distance-card {
width: 600px;
padding: 38px 44px 34px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 24px;
background: #fff;
box-shadow: 0 30px 80px rgba(12, 30, 55, 0.3);
text-align: center;
}
.distance-card h2 {
color: #1f2937;
font-size: 30px;
font-weight: 600;
letter-spacing: 2px;
}
.distance-card > p {
margin-top: 12px;
color: var(--muted);
font-size: 18px;
letter-spacing: 0.5px;
}
.distance-card > p strong {
color: var(--blue);
font-weight: 600;
}
.distance-form {
margin-top: 24px;
}
.distance-field {
display: flex;
flex-direction: column;
gap: 10px;
text-align: left;
color: var(--ink-soft);
font-size: 18px;
}
.distance-field__control {
display: flex;
align-items: center;
overflow: hidden;
border: 2px solid var(--line-strong);
border-radius: 10px;
transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.distance-field__control:focus-within {
border-color: var(--blue);
box-shadow: 0 0 0 4px rgba(47, 111, 208, 0.12);
}
.distance-field input {
flex: 1;
height: 58px;
padding: 0 18px;
border: 0;
color: var(--ink);
font-family: inherit;
font-size: 22px;
outline: none;
}
.distance-field__unit {
flex: none;
padding: 0 20px;
color: var(--muted);
font-size: 19px;
letter-spacing: 1px;
}
.distance-error {
margin-top: 14px;
color: var(--red);
font-size: 17px;
letter-spacing: 0.5px;
text-align: center;
}
.distance-actions {
display: flex;
justify-content: center;
gap: 20px;
margin-top: 28px;
}
.distance-button {
width: 180px;
height: 56px;
border-radius: 12px;
font-size: 20px;
font-weight: 600;
letter-spacing: 3px;
transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}
.distance-button:disabled {
cursor: default;
opacity: 0.65;
}
.distance-button--ghost {
border: 2px solid var(--line-strong);
color: var(--ink-soft);
background: #fff;
}
.distance-button--ghost:hover:not(:disabled) {
background: var(--surface-soft);
}
.distance-button--primary {
color: #fff;
background: linear-gradient(180deg, #4b89e4 0%, #2f6fd0 100%);
box-shadow: 0 10px 22px rgba(47, 111, 208, 0.28);
}
.distance-button--primary:hover:not(:disabled) {
transform: translateY(-1px);
box-shadow: 0 14px 28px rgba(47, 111, 208, 0.36);
}
.distance-modal-enter-active,
.distance-modal-leave-active {
transition: opacity 0.24s ease;
}
.distance-modal-enter-active .distance-card,
.distance-modal-leave-active .distance-card {
transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.24s ease;
}
.distance-modal-enter-from,
.distance-modal-leave-to {
opacity: 0;
}
.distance-modal-enter-from .distance-card,
.distance-modal-leave-to .distance-card {
opacity: 0;
transform: translateY(16px) scale(0.97);
}
</style>
+203 -29
View File
@@ -1,30 +1,87 @@
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppStage from '@/components/AppStage.vue'
import PageHeader from '@/components/PageHeader.vue'
import CameraPreview from '@/components/CameraPreview.vue'
import GuideIcon from '@/components/GuideIcon.vue'
import StatusFooter from '@/components/StatusFooter.vue'
import { captureSampleBaseline, captureSampleTarget } from '@/api/sample'
import { showToast } from '@/composables/useToast'
const router = useRouter()
const baselineReady = ref(false)
const capturingBaseline = ref(false)
const starting = ref(false)
const requirementsConfirmed = ref(false)
const steps = [
{ title: '打开样品仓', icon: 'door' as const },
{ title: '放置待测样品', icon: 'tray' as const },
{ title: '调整聚焦位置', icon: 'focus' as const },
]
const placementRequirements = [
'待测样品边缘不能超出载物台',
'样品尽量放置在画面中央',
'样品放置要稳定,检测过程中不能滑动或翻滚',
'样品高度不能超过 2 厘米',
]
const footerItems = computed(() => [
baselineReady.value
? '基准画面已采集'
: capturingBaseline.value
? '正在采集基准画面'
: '等待相机画面稳定',
'激光安全等级 Class 1',
'环境温度 22℃',
])
function goHome() {
void router.push({ name: 'home' })
}
function startDetection() {
void router.push({ name: 'scan' })
async function onCameraReady() {
if (baselineReady.value || capturingBaseline.value) return
capturingBaseline.value = true
try {
await captureSampleBaseline()
baselineReady.value = true
} catch (error) {
showToast(
error instanceof Error
? `基准画面采集失败:${error.message}`
: '基准画面采集失败',
)
} finally {
capturingBaseline.value = false
}
}
function previewFocus() {
showToast('预览对焦功能正在研发中')
async function startDetection() {
if (starting.value) return
if (!requirementsConfirmed.value) {
showToast('请先确认样品放置符合要求')
return
}
if (!baselineReady.value) {
showToast('基准画面尚未采集完成,请稍候再试')
return
}
starting.value = true
try {
await captureSampleTarget()
void router.push({ name: 'scan' })
} catch (error) {
showToast(error instanceof Error ? error.message : '靶样画面采集失败')
} finally {
starting.value = false
}
}
</script>
@@ -39,7 +96,7 @@ function previewFocus() {
</PageHeader>
<main class="sample-main">
<CameraPreview class="sample-camera" />
<CameraPreview class="sample-camera" @ready="onCameraReady" />
<aside class="guide-column">
<section class="guide-card">
@@ -56,22 +113,49 @@ function previewFocus() {
</span>
</li>
</ol>
<section class="placement-requirements">
<h3>
<span class="placement-requirements__mark">!</span>
放置要求
</h3>
<ul>
<li
v-for="(requirement, index) in placementRequirements"
:key="requirement"
>
<span class="placement-requirements__index">{{ index + 1 }}</span>
<span>{{ requirement }}</span>
</li>
</ul>
<p class="placement-requirements__warning">
以上任一项不符合请中止检测,否则可能损坏机器。
</p>
<label class="placement-confirm">
<input v-model="requirementsConfirmed" type="checkbox" />
<span>我已确认样品放置符合以上要求</span>
</label>
</section>
</section>
<div class="guide-actions">
<button class="action-button action-button--start" type="button" @click="startDetection">
开始检测
</button>
<button class="action-button action-button--ghost" type="button" @click="previewFocus">
预览对焦
<button
class="action-button action-button--start"
type="button"
:disabled="starting || !requirementsConfirmed"
:title="requirementsConfirmed ? undefined : '请先确认样品放置符合要求'"
@click="startDetection"
>
{{ starting ? '准备中...' : '开始检测' }}
</button>
</div>
</aside>
</main>
<StatusFooter
:items="['样品仓已关闭', '激光安全等级 Class 1', '环境温度 22℃']"
/>
<StatusFooter :items="footerItems" />
</AppStage>
</template>
@@ -128,16 +212,16 @@ function previewFocus() {
.guide-steps {
display: flex;
flex: 1;
flex: none;
flex-direction: column;
}
.guide-step {
display: flex;
flex: 1;
flex: none;
align-items: center;
gap: 22px;
min-height: 0;
min-height: 88px;
padding: 0 34px;
border-bottom: 1px solid rgba(255, 255, 255, 0.18);
}
@@ -177,10 +261,105 @@ function previewFocus() {
padding: 2px;
}
.placement-requirements {
display: flex;
flex: 1;
flex-direction: column;
min-height: 0;
margin: 0 24px 22px;
padding: 18px 22px;
overflow: hidden;
border: 1px solid rgba(224, 154, 6, 0.45);
border-radius: 14px;
background: var(--orange-soft);
color: #5b4213;
}
.placement-requirements h3 {
display: flex;
align-items: center;
gap: 10px;
color: #8a5b00;
font-size: 22px;
font-weight: 700;
letter-spacing: 1px;
}
.placement-requirements__mark {
display: grid;
flex: none;
place-items: center;
width: 28px;
height: 28px;
border-radius: 50%;
color: #fff;
background: var(--orange-dark);
font-size: 19px;
font-weight: 700;
}
.placement-requirements ul {
display: grid;
gap: 8px;
margin-top: 12px;
}
.placement-requirements li {
display: flex;
gap: 9px;
color: #5b4213;
font-size: 18px;
line-height: 1.4;
}
.placement-requirements__index {
display: grid;
flex: none;
place-items: center;
width: 21px;
height: 21px;
margin-top: 2px;
border-radius: 50%;
background: rgba(224, 154, 6, 0.18);
color: #8a5b00;
font-size: 13px;
font-weight: 700;
}
.placement-requirements__warning {
margin-top: 10px;
padding-top: 10px;
border-top: 1px dashed rgba(138, 91, 0, 0.32);
color: var(--red);
font-size: 18px;
font-weight: 600;
line-height: 1.4;
}
.placement-confirm {
display: flex;
align-items: center;
gap: 10px;
margin-top: auto;
padding-top: 10px;
color: #5b4213;
font-size: 17px;
cursor: pointer;
user-select: none;
}
.placement-confirm input {
flex: none;
width: 20px;
height: 20px;
accent-color: var(--teal-dark);
cursor: pointer;
}
.guide-actions {
display: grid;
flex: none;
grid-template-columns: 1.16fr 0.84fr;
grid-template-columns: 1fr;
gap: 20px;
height: 78px;
}
@@ -200,6 +379,12 @@ function previewFocus() {
transform: translateY(-1px);
}
.action-button:disabled {
cursor: default;
opacity: 0.7;
transform: none;
}
.action-button--start {
color: #fff;
background: linear-gradient(180deg, #ffc52c 0%, #f3ae0d 100%);
@@ -214,15 +399,4 @@ function previewFocus() {
0 16px 30px rgba(224, 154, 6, 0.36),
inset 0 1px 0 rgba(255, 255, 255, 0.55);
}
.action-button--ghost {
border: 2px solid #d3dbe6;
color: #253243;
background: #fff;
}
.action-button--ghost:hover {
border-color: #aebdcd;
background: #f8fafc;
}
</style>
+312 -13
View File
@@ -1,24 +1,115 @@
<script setup lang="ts">
import { computed } from 'vue'
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppStage from '@/components/AppStage.vue'
import PageHeader from '@/components/PageHeader.vue'
import ScanVisual from '@/components/ScanVisual.vue'
import ProgressStrip from '@/components/ProgressStrip.vue'
import StatusFooter from '@/components/StatusFooter.vue'
import { useTimedProgress } from '@/composables/useTimedProgress'
import { analyzeSample, type SampleAnalysisResult } from '@/api/sample'
type AnalysisStatus = 'processing' | 'done' | 'error'
const router = useRouter()
const { progress } = useTimedProgress(15000, () => {
void router.replace({ name: 'spectrum' })
const status = ref<AnalysisStatus>('processing')
const result = ref<SampleAnalysisResult | null>(null)
const errorMessage = ref('')
const progress = ref(0)
let progressTimer: number | undefined
const resultImageUrl = computed(() =>
result.value
? `data:${result.value.mimeType};base64,${result.value.imageBase64}`
: '',
)
const samplingPointCount = computed(
() => result.value?.samplingPointCount ?? '—',
)
const progressLabel = computed(() => {
if (status.value === 'done') return '靶样轮廓与采样点 100%'
if (status.value === 'error') return '图像分析失败'
return `靶样轮廓识别 ${Math.round(progress.value)}%`
})
const progressLabel = computed(() => `点位扫描 ${Math.round(progress.value)}%`)
const progressNote = computed(() => {
if (status.value === 'done') {
return `已规划 ${result.value?.samplingPointCount ?? 0} 个采样点`
}
if (status.value === 'error') return '请返回或重试'
return '背景差分分析中'
})
const footerItems = computed(() => {
if (status.value === 'done') {
return [
'轮廓提取完成',
`已规划 ${result.value?.samplingPointCount ?? 0} 个采样点`,
'等待采集光谱',
]
}
if (status.value === 'error') {
return ['轮廓提取失败', '请检查靶样放置', '等待重试']
}
return ['相机画面分析中', '背景差分识别轮廓', '采样点规划中']
})
function stopProgress() {
if (progressTimer !== undefined) {
window.clearInterval(progressTimer)
progressTimer = undefined
}
}
function startProgress() {
stopProgress()
progress.value = 0
progressTimer = window.setInterval(() => {
if (progress.value >= 90) return
progress.value = Math.min(90, progress.value + (progress.value < 60 ? 4 : 1.5))
}, 120)
}
async function runAnalysis() {
status.value = 'processing'
result.value = null
errorMessage.value = ''
startProgress()
try {
result.value = await analyzeSample()
progress.value = 100
status.value = 'done'
} catch (error) {
status.value = 'error'
errorMessage.value =
error instanceof Error ? error.message : '靶样轮廓分析失败'
} finally {
stopProgress()
}
}
function abortProcess() {
void router.replace({ name: 'home' })
}
function retry() {
void runAnalysis()
}
function goSpectrum() {
void router.replace({ name: 'spectrum' })
}
onMounted(() => {
void runAnalysis()
})
onBeforeUnmount(stopProgress)
</script>
<template>
@@ -40,7 +131,39 @@ function abortProcess() {
</PageHeader>
<main class="scan-main">
<ScanVisual class="scan-card" />
<ScanVisual v-if="status === 'processing'" class="scan-card" />
<section v-else-if="status === 'done'" class="result-card">
<header class="result-card__header">
<h2>靶样轮廓与采样点</h2>
<span class="result-card__count">
采样点 {{ result?.samplingPointCount ?? 0 }} 个
</span>
</header>
<div class="result-card__viewport">
<img class="result-card__image" :src="resultImageUrl" alt="靶样轮廓与采样点分析结果" />
</div>
</section>
<section v-else class="error-card">
<div class="error-card__icon" aria-hidden="true">
<svg viewBox="0 0 64 64">
<circle cx="32" cy="32" r="24" fill="none" stroke="currentColor" stroke-width="3" />
<path d="M32 20v18M32 44v2" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" />
</svg>
</div>
<h2>靶样轮廓识别失败</h2>
<p>{{ errorMessage }}</p>
<div class="error-card__actions">
<button class="error-button error-button--ghost" type="button" @click="abortProcess">
返回首页
</button>
<button class="error-button error-button--primary" type="button" @click="retry">
重新分析
</button>
</div>
</section>
<aside class="params-card">
<header class="params-card__header">
@@ -50,7 +173,7 @@ function abortProcess() {
<div class="params-card__body">
<div class="param-field">
<span class="param-field__label">采样点数</span>
<div class="param-field__value">5</div>
<div class="param-field__value">{{ samplingPointCount }}</div>
</div>
<div class="param-field">
@@ -71,14 +194,22 @@ function abortProcess() {
</aside>
</main>
<div class="scan-progress">
<ProgressStrip :progress="progress" :label="progressLabel" note="已规划 5 个采样点" />
<div v-if="status !== 'error'" class="scan-progress">
<div class="scan-progress__strip">
<ProgressStrip :progress="progress" :label="progressLabel" :note="progressNote" />
</div>
<button
v-if="status === 'done'"
class="scan-next"
type="button"
@click="goSpectrum"
>
开始采集光谱
</button>
</div>
<StatusFooter
icon="info"
:items="['轮廓提取完成', '点位校验通过', '等待采集光谱']"
/>
<StatusFooter icon="info" :items="footerItems" />
</AppStage>
</template>
@@ -101,6 +232,147 @@ function abortProcess() {
justify-self: center;
}
.result-card {
display: flex;
flex-direction: column;
width: 1120px;
max-width: 100%;
height: 100%;
overflow: hidden;
border: 1px solid #1e55a4;
border-radius: 18px;
background: #0b1220;
box-shadow: 0 18px 44px rgba(31, 77, 147, 0.28);
justify-self: center;
}
.result-card__header {
display: flex;
flex: none;
align-items: center;
justify-content: space-between;
height: 66px;
padding: 0 26px;
background: linear-gradient(180deg, #3d80e2 0%, #2f6fd0 100%);
}
.result-card__header h2 {
color: #fff;
font-size: 28px;
font-weight: 600;
letter-spacing: 3px;
text-shadow: 0 1px 3px rgba(16, 49, 101, 0.22);
}
.result-card__count {
padding: 5px 14px;
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 30px;
background: rgba(255, 255, 255, 0.12);
color: #fff;
font-size: 17px;
letter-spacing: 1px;
}
.result-card__viewport {
display: grid;
flex: 1;
min-height: 0;
place-items: center;
padding: 10px;
}
.result-card__image {
width: 100%;
height: 100%;
object-fit: contain;
}
.error-card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 1120px;
max-width: 100%;
height: 100%;
padding: 40px;
border: 1px solid #d9e0e8;
border-radius: 18px;
background: #fff;
box-shadow: var(--shadow-card);
justify-self: center;
text-align: center;
}
.error-card__icon {
display: grid;
place-items: center;
width: 96px;
height: 96px;
margin-bottom: 24px;
border-radius: 50%;
color: var(--red);
background: #fdeeee;
}
.error-card__icon svg {
width: 54px;
height: 54px;
}
.error-card h2 {
color: #1f2937;
font-size: 32px;
font-weight: 600;
letter-spacing: 2px;
}
.error-card p {
margin-top: 16px;
max-width: 760px;
color: var(--muted);
font-size: 20px;
line-height: 1.6;
}
.error-card__actions {
display: flex;
gap: 20px;
margin-top: 32px;
}
.error-button {
width: 200px;
height: 58px;
border-radius: 12px;
font-size: 21px;
font-weight: 600;
letter-spacing: 3px;
transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}
.error-button--ghost {
border: 2px solid var(--line-strong);
color: var(--ink-soft);
background: #fff;
}
.error-button--ghost:hover {
background: var(--surface-soft);
}
.error-button--primary {
color: #fff;
background: linear-gradient(180deg, #4b89e4 0%, #2f6fd0 100%);
box-shadow: 0 10px 22px rgba(47, 111, 208, 0.28);
}
.error-button--primary:hover {
transform: translateY(-1px);
box-shadow: 0 14px 28px rgba(47, 111, 208, 0.36);
}
.params-card {
display: flex;
flex-direction: column;
@@ -173,6 +445,33 @@ function abortProcess() {
top: 900px;
right: 44px;
left: 44px;
display: flex;
align-items: center;
gap: 20px;
height: 54px;
}
.scan-progress__strip {
flex: 1;
min-width: 0;
}
.scan-next {
flex: none;
width: 280px;
height: 54px;
border-radius: 10px;
color: #fff;
font-size: 23px;
font-weight: 600;
letter-spacing: 3px;
background: linear-gradient(180deg, #4b89e4 0%, #2f6fd0 100%);
box-shadow: 0 10px 22px rgba(47, 111, 208, 0.28);
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.scan-next:hover {
transform: translateY(-1px);
box-shadow: 0 14px 28px rgba(47, 111, 208, 0.36);
}
</style>
+30 -2
View File
@@ -109,6 +109,22 @@ function closeForm() {
formOpen.value = false
}
let backdropPressed = false
function onBackdropPointerDown(event: PointerEvent) {
backdropPressed = event.target === event.currentTarget
}
function onBackdropPointerUp(event: PointerEvent) {
if (event.target !== event.currentTarget) backdropPressed = false
}
function closeOnBackdrop(event: MouseEvent, action: () => void) {
const shouldClose = backdropPressed && event.target === event.currentTarget
backdropPressed = false
if (shouldClose) action()
}
async function save() {
if (saving.value) return
@@ -254,7 +270,13 @@ onMounted(() => {
<Teleport to="body">
<Transition name="config-modal">
<div v-if="formOpen" class="config-backdrop" @click.self="closeForm">
<div
v-if="formOpen"
class="config-backdrop"
@pointerdown="onBackdropPointerDown"
@pointerup="onBackdropPointerUp"
@click="closeOnBackdrop($event, closeForm)"
>
<section class="form-card" role="dialog" aria-modal="true" aria-label="系统参数表单">
<h2>{{ editingId === null ? '新增系统参数' : '编辑系统参数' }}</h2>
@@ -316,7 +338,13 @@ onMounted(() => {
<Teleport to="body">
<Transition name="config-modal">
<div v-if="deleting" class="config-backdrop" @click.self="cancelDelete">
<div
v-if="deleting"
class="config-backdrop"
@pointerdown="onBackdropPointerDown"
@pointerup="onBackdropPointerUp"
@click="closeOnBackdrop($event, cancelDelete)"
>
<section class="confirm-card" role="dialog" aria-modal="true" aria-label="删除确认">
<h2>删除系统参数</h2>
<p>