添加项目文件。
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import { getCameraStatus } from '@/api/camera'
|
||||
|
||||
export type CameraConnectionState = 'checking' | 'connected' | 'disconnected' | 'offline'
|
||||
|
||||
/**
|
||||
* 轮询 /api/camera/status,供首页与支付页共用。
|
||||
* 后端不可达时返回 offline,界面展示为“相机服务离线”,不会阻塞页面。
|
||||
*/
|
||||
export function useCameraStatus(pollInterval = 5000, autoStart = true) {
|
||||
const state = ref<CameraConnectionState>('checking')
|
||||
const connected = ref<boolean | null>(null)
|
||||
const lastChecked = ref<Date | null>(null)
|
||||
const errorMessage = ref('')
|
||||
|
||||
let timer: number | undefined
|
||||
let inFlight = false
|
||||
|
||||
async function refresh() {
|
||||
if (inFlight) return
|
||||
inFlight = true
|
||||
try {
|
||||
const { connected: isConnected } = await getCameraStatus()
|
||||
connected.value = isConnected
|
||||
state.value = isConnected ? 'connected' : 'disconnected'
|
||||
errorMessage.value = ''
|
||||
} catch (error) {
|
||||
connected.value = null
|
||||
state.value = 'offline'
|
||||
errorMessage.value = error instanceof Error ? error.message : '无法连接相机服务'
|
||||
} finally {
|
||||
inFlight = false
|
||||
lastChecked.value = new Date()
|
||||
}
|
||||
}
|
||||
|
||||
function startPolling() {
|
||||
stopPolling()
|
||||
timer = window.setInterval(refresh, pollInterval)
|
||||
}
|
||||
|
||||
function stopPolling() {
|
||||
if (timer !== undefined) {
|
||||
window.clearInterval(timer)
|
||||
timer = undefined
|
||||
}
|
||||
}
|
||||
|
||||
function onVisibilityChange() {
|
||||
if (document.visibilityState === 'visible') void refresh()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (!autoStart) return
|
||||
void refresh()
|
||||
startPolling()
|
||||
document.addEventListener('visibilitychange', onVisibilityChange)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
stopPolling()
|
||||
document.removeEventListener('visibilitychange', onVisibilityChange)
|
||||
})
|
||||
|
||||
return {
|
||||
state,
|
||||
connected,
|
||||
lastChecked,
|
||||
errorMessage,
|
||||
isConnected: computed(() => state.value === 'connected'),
|
||||
refresh,
|
||||
startPolling,
|
||||
stopPolling,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import { computed, onBeforeUnmount, ref } from 'vue'
|
||||
import {
|
||||
HubConnectionBuilder,
|
||||
HubConnectionState,
|
||||
LogLevel,
|
||||
type HubConnection,
|
||||
} from '@microsoft/signalr'
|
||||
import { getCameraHubUrl } from '@/api/camera'
|
||||
|
||||
interface CameraFramePayload {
|
||||
frameIndex: number
|
||||
timestamp: string
|
||||
imageBase64: string
|
||||
/** 后端根据实际采集格式给出:image/jpeg、image/png 或 image/bmp */
|
||||
mimeType?: string
|
||||
width?: number
|
||||
height?: number
|
||||
capturedAt?: string
|
||||
}
|
||||
|
||||
interface CameraStatusPayload {
|
||||
isConnected: boolean
|
||||
message?: string
|
||||
deviceName?: string | null
|
||||
framesReceived?: number
|
||||
lastFrameAt?: string | null
|
||||
}
|
||||
|
||||
export type StreamState = 'idle' | 'connecting' | 'connected' | 'offline'
|
||||
|
||||
/** 摄像头 MJPG 帧,base64 直出为 JPEG */
|
||||
const FRAME_MIME = 'image/jpeg'
|
||||
/** 限制前端刷新率,避免高帧率推流时占用过多主线程 */
|
||||
const MIN_FRAME_INTERVAL = 70
|
||||
|
||||
/**
|
||||
* 通过 SignalR(/cameraHub)接收相机实时帧。
|
||||
* 事件契约见 HardwareWeb/Services/CameraBroadcastService.cs:
|
||||
* CameraFrame -> { frameIndex, timestamp, imageBase64 }
|
||||
* CameraStatus -> { isConnected, message }
|
||||
*/
|
||||
export function useCameraStream() {
|
||||
const state = ref<StreamState>('idle')
|
||||
const frameUrl = ref<string | null>(null)
|
||||
const frameIndex = ref(0)
|
||||
const errorMessage = ref('')
|
||||
const hasFrame = ref(false)
|
||||
|
||||
let connection: HubConnection | null = null
|
||||
let lastFrameAt = 0
|
||||
let stoppedByUser = false
|
||||
|
||||
const isLive = computed(() => state.value === 'connected' && hasFrame.value)
|
||||
|
||||
function handleFrame(payload: CameraFramePayload) {
|
||||
if (!payload?.imageBase64) return
|
||||
const now = performance.now()
|
||||
if (now - lastFrameAt < MIN_FRAME_INTERVAL) return
|
||||
lastFrameAt = now
|
||||
const mime =
|
||||
payload.mimeType ??
|
||||
(payload.imageBase64.startsWith('iVBOR')
|
||||
? 'image/png'
|
||||
: payload.imageBase64.startsWith('Qk')
|
||||
? 'image/bmp'
|
||||
: FRAME_MIME)
|
||||
frameUrl.value = `data:${mime};base64,${payload.imageBase64}`
|
||||
frameIndex.value = payload.frameIndex
|
||||
hasFrame.value = true
|
||||
state.value = 'connected'
|
||||
}
|
||||
|
||||
function handleStatus(payload: CameraStatusPayload) {
|
||||
if (payload.isConnected) {
|
||||
state.value = 'connected'
|
||||
errorMessage.value = ''
|
||||
} else {
|
||||
hasFrame.value = false
|
||||
frameUrl.value = null
|
||||
state.value = 'offline'
|
||||
}
|
||||
}
|
||||
|
||||
async function connect() {
|
||||
if (connection && connection.state !== HubConnectionState.Disconnected) return
|
||||
stoppedByUser = false
|
||||
state.value = 'connecting'
|
||||
errorMessage.value = ''
|
||||
|
||||
const hub = new HubConnectionBuilder()
|
||||
.withUrl(getCameraHubUrl(), { withCredentials: false })
|
||||
.withAutomaticReconnect({
|
||||
nextRetryDelayInMilliseconds: (context) =>
|
||||
Math.min(1000 * 2 ** context.previousRetryCount, 10000),
|
||||
})
|
||||
.configureLogging(LogLevel.Warning)
|
||||
.build()
|
||||
|
||||
hub.on('CameraFrame', handleFrame)
|
||||
hub.on('CameraStatus', handleStatus)
|
||||
hub.onreconnecting(() => {
|
||||
state.value = 'connecting'
|
||||
})
|
||||
hub.onreconnected(() => {
|
||||
state.value = 'connected'
|
||||
})
|
||||
hub.onclose(() => {
|
||||
if (stoppedByUser) return
|
||||
state.value = 'offline'
|
||||
hasFrame.value = false
|
||||
})
|
||||
|
||||
connection = hub
|
||||
|
||||
try {
|
||||
await hub.start()
|
||||
state.value = 'connected'
|
||||
} catch (error) {
|
||||
state.value = 'offline'
|
||||
errorMessage.value = error instanceof Error ? error.message : '实时画面连接失败'
|
||||
connection = null
|
||||
await hub.stop().catch(() => undefined)
|
||||
}
|
||||
}
|
||||
|
||||
async function disconnect() {
|
||||
stoppedByUser = true
|
||||
hasFrame.value = false
|
||||
frameUrl.value = null
|
||||
const hub = connection
|
||||
connection = null
|
||||
state.value = 'idle'
|
||||
if (hub) await hub.stop().catch(() => undefined)
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
void disconnect()
|
||||
})
|
||||
|
||||
return {
|
||||
state,
|
||||
frameUrl,
|
||||
frameIndex,
|
||||
errorMessage,
|
||||
hasFrame,
|
||||
isLive,
|
||||
connect,
|
||||
disconnect,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
/** 秒级倒计时,归零时只触发一次回调。 */
|
||||
export function useCountdown(seconds: number, onComplete: () => void) {
|
||||
const remaining = ref(seconds)
|
||||
|
||||
let timer: number | undefined
|
||||
let finished = false
|
||||
|
||||
onMounted(() => {
|
||||
timer = window.setInterval(() => {
|
||||
remaining.value = Math.max(0, remaining.value - 1)
|
||||
|
||||
if (remaining.value === 0 && !finished) {
|
||||
finished = true
|
||||
window.clearInterval(timer)
|
||||
timer = undefined
|
||||
onComplete()
|
||||
}
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (timer !== undefined) {
|
||||
window.clearInterval(timer)
|
||||
timer = undefined
|
||||
}
|
||||
})
|
||||
|
||||
return { remaining }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
/**
|
||||
* 设计稿为 1920 × 1080 的一体机大屏。
|
||||
* 用 transform 整体缩放,可以在任意分辨率下保持设计比例与布局,不会出现错位。
|
||||
*/
|
||||
export function useStageScale(width = 1920, height = 1080) {
|
||||
const scale = ref(1)
|
||||
|
||||
const update = () => {
|
||||
const next = Math.min(window.innerWidth / width, window.innerHeight / height)
|
||||
scale.value = Math.max(0.2, Math.round(next * 10000) / 10000)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
update()
|
||||
window.addEventListener('resize', update, { passive: true })
|
||||
window.addEventListener('orientationchange', update)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener('resize', update)
|
||||
window.removeEventListener('orientationchange', update)
|
||||
})
|
||||
|
||||
return { scale, update }
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
/**
|
||||
* 平滑推进 0 → 100 的进度值,到达 100% 时只触发一次回调。
|
||||
* 组件卸载(用户中止流程)时会自动取消,不会误跳转。
|
||||
*/
|
||||
export function useTimedProgress(durationMs: number, onComplete: () => void) {
|
||||
const progress = ref(0)
|
||||
|
||||
let frame = 0
|
||||
let startedAt = 0
|
||||
let finished = false
|
||||
|
||||
const tick = (now: number) => {
|
||||
if (!startedAt) startedAt = now
|
||||
const elapsed = now - startedAt
|
||||
progress.value = Math.min(100, (elapsed / durationMs) * 100)
|
||||
|
||||
if (progress.value >= 100) {
|
||||
if (!finished) {
|
||||
finished = true
|
||||
onComplete()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
frame = window.requestAnimationFrame(tick)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (durationMs <= 0) {
|
||||
progress.value = 100
|
||||
if (!finished) {
|
||||
finished = true
|
||||
onComplete()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
frame = window.requestAnimationFrame(tick)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.cancelAnimationFrame(frame)
|
||||
})
|
||||
|
||||
return { progress }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { readonly, ref } from 'vue'
|
||||
|
||||
const message = ref('')
|
||||
const visible = ref(false)
|
||||
let timer: number | undefined
|
||||
|
||||
export function showToast(text: string, duration = 2400) {
|
||||
message.value = text
|
||||
visible.value = true
|
||||
|
||||
if (timer !== undefined) {
|
||||
window.clearTimeout(timer)
|
||||
}
|
||||
|
||||
timer = window.setTimeout(() => {
|
||||
visible.value = false
|
||||
timer = undefined
|
||||
}, duration)
|
||||
}
|
||||
|
||||
export function useToast() {
|
||||
return {
|
||||
message: readonly(message),
|
||||
visible: readonly(visible),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user