392 lines
10 KiB
Vue
392 lines
10 KiB
Vue
<template>
|
|
<el-dialog
|
|
:model-value="modelValue"
|
|
title="摄像头预览"
|
|
width="800px"
|
|
align-center
|
|
destroy-on-close
|
|
@update:model-value="onDialogVisibleChange"
|
|
@closed="handleClosed"
|
|
>
|
|
<div class="preview-container">
|
|
<div v-if="error" class="preview-error">
|
|
<p class="preview-error-title">服务暂不可用</p>
|
|
<p class="preview-error-sub">{{ error }}</p>
|
|
<el-button v-if="errorLink" type="primary" size="small" class="preview-error-btn" @click="openDownload">
|
|
下载打印控件
|
|
</el-button>
|
|
</div>
|
|
<div v-else-if="streamReady && streamSrc" class="preview-video-wrapper">
|
|
<img
|
|
ref="videoImgRef"
|
|
:key="streamKey"
|
|
:src="streamSrc"
|
|
class="preview-video"
|
|
/>
|
|
<div v-if="retryHint" class="preview-hint">{{ retryHint }}</div>
|
|
</div>
|
|
<div v-else class="preview-placeholder">
|
|
{{ loading ? '正在获取流地址…' : streamSrc ? '正在连接录像机…' : '正在连接…' }}
|
|
</div>
|
|
|
|
<!-- OSD 文字叠加设置 -->
|
|
<div v-if="streamReady && cameraRow?.id" class="osd-controls">
|
|
<div class="osd-title">OSD 文字叠加</div>
|
|
<el-form :inline="true" size="small" class="osd-form">
|
|
<el-form-item label="文字">
|
|
<el-input v-model="osdText" placeholder="输入叠加文字" maxlength="50" style="width: 160px" />
|
|
</el-form-item>
|
|
<el-form-item label="X">
|
|
<el-input-number v-model="osdX" :min="-1" :max="1" :step="0.1" :precision="1" style="width: 100px" />
|
|
</el-form-item>
|
|
<el-form-item label="Y">
|
|
<el-input-number v-model="osdY" :min="-1" :max="1" :step="0.1" :precision="1" style="width: 100px" />
|
|
</el-form-item>
|
|
<el-form-item>
|
|
<el-button type="primary" size="small" :loading="osdLoading" @click="handleSetOsd">设置 OSD</el-button>
|
|
</el-form-item>
|
|
</el-form>
|
|
</div>
|
|
</div>
|
|
<template #footer>
|
|
<el-button @click="onDialogVisibleChange(false)">关闭</el-button>
|
|
<el-button type="primary" :loading="loading" @click="reloadStream">刷新</el-button>
|
|
</template>
|
|
</el-dialog>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, computed, watch } from 'vue'
|
|
import { ElMessage } from 'element-plus'
|
|
import { getLiveStreamUrl } from '@/api/camera_server/localServerApi'
|
|
import { list as serverList, detail as serverDetail } from '@/api/camera_server/camera_server'
|
|
import { setOsd } from '@/api/camera/camera'
|
|
|
|
export interface CameraPreviewRow {
|
|
id?: number
|
|
server_id?: number
|
|
channel_id?: number
|
|
channel_name?: string
|
|
custom_name?: string
|
|
ip?: string
|
|
port?: number
|
|
}
|
|
|
|
const props = defineProps<{
|
|
modelValue: boolean
|
|
cameraRow: CameraPreviewRow | null
|
|
}>()
|
|
|
|
const emit = defineEmits<{
|
|
'update:modelValue': [value: boolean]
|
|
}>()
|
|
|
|
const loading = ref(false)
|
|
const error = ref('')
|
|
const errorLink = ref(false)
|
|
const retryHint = ref('')
|
|
const streamReady = ref(false)
|
|
const streamKey = ref(0)
|
|
const videoImgRef = ref<HTMLImageElement | null>(null)
|
|
let probeAbort: AbortController | null = null
|
|
/** 是否已尝试过公网 IP 回退 */
|
|
let triedPublic = false
|
|
|
|
// ---- OSD ----
|
|
const osdText = ref('')
|
|
const osdX = ref(-0.9)
|
|
const osdY = ref(0.8)
|
|
const osdLoading = ref(false)
|
|
|
|
const nvrIp = ref('')
|
|
const nvrPort = ref<number | undefined>(undefined)
|
|
const nvrUser = ref('')
|
|
const nvrPass = ref('')
|
|
const internalIp = ref('')
|
|
const publicIp = ref('')
|
|
const internalPort = ref<number | undefined>(undefined)
|
|
const publicPort = ref<number | undefined>(undefined)
|
|
const serverCache = ref<Record<number, any>>({})
|
|
|
|
const streamSrc = computed(() => {
|
|
const row = props.cameraRow
|
|
if (!row || !row.channel_id || !nvrIp.value || !nvrUser.value || !nvrPass.value) return null
|
|
return getLiveStreamUrl({
|
|
ip: nvrIp.value,
|
|
user: nvrUser.value,
|
|
pass: nvrPass.value,
|
|
channel: row.channel_id,
|
|
streamType: 0,
|
|
port: nvrPort.value
|
|
})
|
|
})
|
|
|
|
const ok = (res: any) => res && [0, 1, 200].includes(res.code)
|
|
|
|
async function getServerInfo(id: number) {
|
|
if (serverCache.value[id]) return serverCache.value[id]
|
|
try {
|
|
const res: any = await serverList({ pagination: { page: 1, pageSize: 999 } })
|
|
if (ok(res)) {
|
|
for (const item of res?.list || []) {
|
|
if (item.id) serverCache.value[item.id] = item
|
|
}
|
|
}
|
|
} catch { /* ignore */ }
|
|
return serverCache.value[id] || null
|
|
}
|
|
|
|
async function init() {
|
|
const row = props.cameraRow
|
|
if (!row) return
|
|
|
|
loading.value = true
|
|
error.value = ''
|
|
errorLink.value = false
|
|
retryHint.value = ''
|
|
triedPublic = false
|
|
nvrIp.value = ''
|
|
nvrPort.value = undefined
|
|
internalIp.value = ''
|
|
internalPort.value = undefined
|
|
publicIp.value = ''
|
|
publicPort.value = undefined
|
|
|
|
const sid = row.server_id
|
|
if (!sid) { error.value = '未关联录像服务器'; loading.value = false; return }
|
|
|
|
const srv = await getServerInfo(sid)
|
|
if (!srv) { error.value = '未找到服务器信息'; loading.value = false; return }
|
|
|
|
let pwd = srv.password
|
|
if (!pwd) {
|
|
try {
|
|
const d: any = await serverDetail({ id: sid })
|
|
if (ok(d) && d?.data?.password) pwd = d.data.password
|
|
} catch { /* ignore */ }
|
|
}
|
|
if (!pwd) { error.value = '缺少服务器密码'; loading.value = false; return }
|
|
|
|
internalIp.value = (srv.internal_ip || '').trim()
|
|
internalPort.value = srv.internal_port || srv.port
|
|
publicIp.value = (srv.public_ip || '').trim()
|
|
publicPort.value = srv.public_port || srv.port
|
|
nvrUser.value = srv.username || ''
|
|
nvrPass.value = pwd
|
|
|
|
const ip = internalIp.value || publicIp.value
|
|
if (!ip) { error.value = '服务器未配置 IP'; loading.value = false; return }
|
|
|
|
nvrIp.value = ip
|
|
nvrPort.value = internalIp.value ? internalPort.value : publicPort.value
|
|
streamReady.value = false
|
|
loading.value = false
|
|
|
|
// 用 fetch 探测流地址,能正确识别 502 与网络错误
|
|
await probeStream()
|
|
}
|
|
|
|
/** fetch 探测流地址,成功后才让 <img> 渲染 */
|
|
async function probeStream() {
|
|
const url = streamSrc.value
|
|
if (!url) return
|
|
|
|
// 取消上一次未完成的请求
|
|
if (probeAbort) { probeAbort.abort() }
|
|
probeAbort = new AbortController()
|
|
|
|
try {
|
|
const resp = await fetch(url, { signal: probeAbort.signal })
|
|
if (resp.ok) {
|
|
streamReady.value = true
|
|
} else if (resp.status === 502) {
|
|
// 502 → 尝试公网回退或显示失败
|
|
await handleStream502()
|
|
} else {
|
|
error.value = `服务器返回错误 (${resp.status}),请检查网络或服务器配置`
|
|
}
|
|
} catch (e: any) {
|
|
if (e.name === 'AbortError') return // 用户主动关闭,忽略
|
|
// 连接被拒等网络错误 → 本地代理未启动
|
|
error.value = '本地打印服务未启动,请下载安装后再试'
|
|
errorLink.value = true
|
|
}
|
|
}
|
|
|
|
/** 502 错误处理:内网→公网回退,已回退则显示失败 */
|
|
async function handleStream502() {
|
|
stopStream()
|
|
|
|
// 当前在内网 IP、有公网 IP 且未尝试过 → 自动切公网重试
|
|
if (nvrIp.value === internalIp.value && publicIp.value && publicIp.value !== internalIp.value && !triedPublic) {
|
|
triedPublic = true
|
|
retryHint.value = '内网无法访问,正在切换公网…'
|
|
streamReady.value = false
|
|
streamKey.value++
|
|
await new Promise(r => setTimeout(r, 300))
|
|
nvrIp.value = publicIp.value
|
|
nvrPort.value = publicPort.value
|
|
retryHint.value = ''
|
|
await probeStream()
|
|
return
|
|
}
|
|
|
|
// 已尝试过公网或无备用 IP → 显示失败
|
|
const tried = [internalIp.value, publicIp.value].filter(Boolean).join('、')
|
|
error.value = `无法连接录像机(已尝试:${tried}),请检查网络或服务器配置`
|
|
streamReady.value = false
|
|
}
|
|
|
|
function reloadStream() {
|
|
if (probeAbort) { probeAbort.abort(); probeAbort = null }
|
|
triedPublic = false
|
|
streamReady.value = false
|
|
streamKey.value++
|
|
init()
|
|
}
|
|
|
|
function stopStream() {
|
|
if (probeAbort) { probeAbort.abort(); probeAbort = null }
|
|
if (videoImgRef.value) videoImgRef.value.src = ''
|
|
}
|
|
|
|
function openDownload() {
|
|
window.open('/download', '_blank')
|
|
}
|
|
|
|
function onDialogVisibleChange(val: boolean) {
|
|
if (!val) { probeAbort?.abort(); stopStream() }
|
|
emit('update:modelValue', val)
|
|
}
|
|
|
|
function handleClosed() {
|
|
if (probeAbort) { probeAbort.abort(); probeAbort = null }
|
|
triedPublic = false
|
|
error.value = ''
|
|
errorLink.value = false
|
|
retryHint.value = ''
|
|
streamReady.value = false
|
|
nvrIp.value = ''
|
|
nvrPort.value = undefined
|
|
osdText.value = ''
|
|
osdX.value = -0.9
|
|
osdY.value = 0.8
|
|
}
|
|
|
|
async function handleSetOsd() {
|
|
const row = props.cameraRow
|
|
if (!row?.id) return
|
|
if (!osdText.value.trim()) {
|
|
ElMessage.warning('请输入 OSD 文字')
|
|
return
|
|
}
|
|
osdLoading.value = true
|
|
try {
|
|
const res: any = await setOsd({
|
|
id: row.id,
|
|
text: osdText.value.trim(),
|
|
x: osdX.value,
|
|
y: osdY.value
|
|
})
|
|
if (ok(res)) {
|
|
ElMessage.success('OSD 设置成功')
|
|
} else {
|
|
ElMessage.error(res?.message || 'OSD 设置失败')
|
|
}
|
|
} catch {
|
|
ElMessage.error('OSD 设置失败')
|
|
} finally {
|
|
osdLoading.value = false
|
|
}
|
|
}
|
|
|
|
watch(() => props.modelValue, (v) => {
|
|
if (v) {
|
|
if (probeAbort) { probeAbort.abort(); probeAbort = null }
|
|
triedPublic = false
|
|
streamReady.value = false
|
|
streamKey.value++
|
|
init()
|
|
}
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.preview-container {
|
|
min-height: 200px;
|
|
}
|
|
.preview-video-wrapper {
|
|
background: #000;
|
|
border-radius: 4px;
|
|
overflow: hidden;
|
|
line-height: 0;
|
|
}
|
|
.preview-video {
|
|
width: 100%;
|
|
min-height: 360px;
|
|
display: block;
|
|
object-fit: contain;
|
|
background: #000;
|
|
}
|
|
.preview-hint {
|
|
padding: 8px 12px;
|
|
font-size: 13px;
|
|
color: #e6a23c;
|
|
background: #fdf6ec;
|
|
}
|
|
.osd-controls {
|
|
margin-top: 16px;
|
|
padding: 12px 16px;
|
|
border: 1px solid #ebeef5;
|
|
border-radius: 4px;
|
|
background: #fafafa;
|
|
}
|
|
.osd-title {
|
|
font-size: 14px;
|
|
font-weight: 600;
|
|
color: #303133;
|
|
margin-bottom: 10px;
|
|
}
|
|
.osd-form {
|
|
display: flex;
|
|
align-items: flex-end;
|
|
flex-wrap: wrap;
|
|
gap: 4px;
|
|
}
|
|
.osd-form :deep(.el-form-item) {
|
|
margin-bottom: 0;
|
|
margin-right: 8px;
|
|
}
|
|
.preview-error {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
min-height: 360px;
|
|
background: #f5f7fa;
|
|
border-radius: 4px;
|
|
text-align: center;
|
|
}
|
|
.preview-error-title {
|
|
font-size: 18px;
|
|
color: #e6a23c;
|
|
margin: 0 0 8px;
|
|
}
|
|
.preview-error-sub {
|
|
font-size: 13px;
|
|
color: #909399;
|
|
margin: 0 0 12px;
|
|
}
|
|
.preview-error-btn {
|
|
margin-top: 4px;
|
|
}
|
|
.preview-placeholder {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
height: 200px;
|
|
color: #909399;
|
|
font-size: 14px;
|
|
}
|
|
</style>
|