627 lines
19 KiB
Vue
627 lines
19 KiB
Vue
<template>
|
||
<div class="video-container" :style="{ height: containerHeight }">
|
||
<scan-box
|
||
ref="scanBox"
|
||
cache-key="video-camera-server"
|
||
width="250px"
|
||
title="录像服务器筛选"
|
||
:preset-tree="presetTree"
|
||
:available-fields="availableFields"
|
||
@condition-change="handleConditionChange"
|
||
@reset="handleReset"
|
||
/>
|
||
|
||
<div class="main-content">
|
||
<div class="toolbar">
|
||
<div class="toolbar-left">
|
||
<el-button size="small" type="primary" :loading="loading" @click="loadList">刷新</el-button>
|
||
<el-tag size="small" type="info">共 {{ pagination.total }} 条</el-tag>
|
||
</div>
|
||
<el-button type="success" size="small" @click="openCreate">新增录像服务器</el-button>
|
||
</div>
|
||
|
||
<div class="upper-wrapper">
|
||
<upper-table
|
||
ref="upperTable"
|
||
title="录像服务器列表"
|
||
default-align="center"
|
||
table-id="video-camera-server"
|
||
:data="tableData"
|
||
:columns="columns"
|
||
:table-size="tableSize"
|
||
:pagination="pagination"
|
||
:show-index="true"
|
||
:show-actions="false"
|
||
:loading="loading"
|
||
:query-time="queryTime"
|
||
row-key="id"
|
||
@page-change="handlePageChange"
|
||
@page-size-change="handlePageSizeChange"
|
||
@refresh="loadList"
|
||
>
|
||
<template #column-status="{ row }">
|
||
<el-tag :type="formatStatus(row.status).type" size="small">
|
||
{{ formatStatus(row.status).text }}
|
||
</el-tag>
|
||
</template>
|
||
|
||
<template #column-actions="{ row }">
|
||
<el-button link type="primary" size="small" @click.stop="openEdit(row)">编辑</el-button>
|
||
<el-button link type="danger" size="small" @click.stop="handleDelete(row)">删除</el-button>
|
||
</template>
|
||
|
||
<template #column-connectivity="{ row }">
|
||
<el-button
|
||
size="small"
|
||
type="warning"
|
||
plain
|
||
:loading="testLoadingMap[row.id!]"
|
||
@click.stop="handleTestConnection(row)"
|
||
>
|
||
连通测试
|
||
</el-button>
|
||
</template>
|
||
|
||
<template #empty>
|
||
<div class="empty-tip">暂无录像服务器数据,可调整左侧筛选条件</div>
|
||
</template>
|
||
</upper-table>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 新增/编辑弹窗 -->
|
||
<el-dialog
|
||
v-model="dialogVisible"
|
||
:title="dialogMode === 'create' ? '新增录像服务器' : '编辑录像服务器'"
|
||
width="560px"
|
||
align-center
|
||
@close="resetForm"
|
||
>
|
||
<el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
|
||
<el-form-item label="服务器名称" prop="name">
|
||
<el-input v-model="formData.name" placeholder="请输入服务器名称" maxlength="100" show-word-limit />
|
||
</el-form-item>
|
||
<el-form-item label="品牌" prop="brand">
|
||
<el-select v-model="formData.brand" placeholder="请选择品牌" clearable style="width: 100%">
|
||
<el-option label="海康威视" value="hikvision" />
|
||
<el-option label="大华" value="dahua" />
|
||
<el-option label="宇视" value="uniview" />
|
||
<el-option label="其他" value="other" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="公网IP" prop="public_ip">
|
||
<div class="ip-port-row">
|
||
<el-input v-model="formData.public_ip" placeholder="请输入公网IP" maxlength="50" class="ip-input" />
|
||
<el-input-number v-model="formData.public_port" :min="1" :max="65535" placeholder="端口" class="port-input" />
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item label="内网IP" prop="internal_ip">
|
||
<div class="ip-port-row">
|
||
<el-input v-model="formData.internal_ip" placeholder="请输入内网IP" maxlength="50" class="ip-input" />
|
||
<el-input-number v-model="formData.internal_port" :min="1" :max="65535" placeholder="端口" class="port-input" />
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item label="用户名" prop="username">
|
||
<el-input v-model="formData.username" placeholder="请输入用户名" maxlength="50" />
|
||
</el-form-item>
|
||
<el-form-item label="密码" prop="password">
|
||
<el-input v-model="formData.password" type="password" placeholder="请输入密码" maxlength="50" show-password />
|
||
</el-form-item>
|
||
<el-form-item label="状态" prop="status">
|
||
<el-switch
|
||
v-model="formData.status"
|
||
:active-value="1"
|
||
:inactive-value="0"
|
||
active-text="启用"
|
||
inactive-text="禁用"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item label="备注" prop="remark">
|
||
<el-input v-model="formData.remark" type="textarea" :rows="3" placeholder="请输入备注" maxlength="200" show-word-limit />
|
||
</el-form-item>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button @click="dialogVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="saving" @click="submitForm">保存</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, reactive, ref } from 'vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import type { FormInstance, FormRules } from 'element-plus'
|
||
import ScanBox from '@/components/ScanBox/index.vue'
|
||
import UpperTable from '@/components/UpperTable/index.vue'
|
||
import { list, create, update, del } from '@/api/camera_server/camera_server'
|
||
import { testConnection } from '@/api/camera_server/localServerApi'
|
||
import { useSettingsStore } from '@/stores/modules/settings'
|
||
|
||
type CameraServerRecord = {
|
||
id?: number
|
||
name?: string
|
||
brand?: string
|
||
public_ip?: string
|
||
public_port?: number
|
||
internal_ip?: string
|
||
internal_port?: number
|
||
port?: number
|
||
username?: string
|
||
password?: string
|
||
status?: number
|
||
remark?: string
|
||
created_at?: string
|
||
updated_at?: string
|
||
}
|
||
|
||
const settingsStore = useSettingsStore()
|
||
const scanBox = ref<any>(null)
|
||
const upperTable = ref<any>(null)
|
||
|
||
const shortcutExpanded = computed(() => settingsStore.shortcutExpanded)
|
||
const containerHeight = computed(() =>
|
||
shortcutExpanded.value ? 'calc(100vh - 150px)' : 'calc(100vh - 100px)'
|
||
)
|
||
|
||
const columns = [
|
||
{ prop: 'id', label: 'ID', width: '80', sortable: true },
|
||
{ prop: 'name', label: '服务器名称', minWidth: '160', showOverflowTooltip: true },
|
||
{ prop: 'brand', label: '品牌', width: '120' },
|
||
{ prop: 'public_ip', label: '公网IP', width: '150' },
|
||
{ prop: 'internal_ip', label: '内网IP', width: '150' },
|
||
{ prop: 'port', label: '端口', width: '80' },
|
||
{ prop: 'username', label: '用户名', width: '120' },
|
||
{ prop: 'status', label: '状态', width: '90' },
|
||
{ prop: 'connectivity', label: '连通', width: '110' },
|
||
{ prop: 'remark', label: '备注', minWidth: '160', showOverflowTooltip: true },
|
||
{ prop: 'actions', label: '操作', width: '150', fixed: 'right' }
|
||
]
|
||
|
||
const availableFields = [
|
||
{
|
||
value: 'quickWhere',
|
||
label: '关键词',
|
||
type: 'text',
|
||
placeholder: '服务器名称/IP',
|
||
operators: ['contains'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'name',
|
||
label: '服务器名称',
|
||
type: 'text',
|
||
placeholder: '请输入名称',
|
||
operators: ['contains'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'status',
|
||
label: '状态',
|
||
type: 'select',
|
||
clearable: true,
|
||
options: [
|
||
{ label: '启用', value: '1' },
|
||
{ label: '禁用', value: '0' }
|
||
]
|
||
},
|
||
{
|
||
value: 'brand',
|
||
label: '品牌',
|
||
type: 'select',
|
||
clearable: true,
|
||
options: [
|
||
{ label: '海康威视', value: 'hikvision' },
|
||
{ label: '大华', value: 'dahua' },
|
||
{ label: '宇视', value: 'uniview' },
|
||
{ label: '其他', value: 'other' }
|
||
]
|
||
}
|
||
]
|
||
|
||
const presetTree = ref<any[]>([])
|
||
const loading = ref(false)
|
||
const saving = ref(false)
|
||
const testLoadingMap = ref<Record<number, boolean>>({})
|
||
const tableData = ref<CameraServerRecord[]>([])
|
||
const queryTime = ref(0)
|
||
const tableSize = ref<'small' | 'default' | 'large'>('small')
|
||
const currentConditionRaw = ref<any>(null)
|
||
|
||
const pagination = reactive({ currentPage: 1, pageSize: 20, total: 0 })
|
||
|
||
const dialogVisible = ref(false)
|
||
const dialogMode = ref<'create' | 'edit'>('create')
|
||
const formRef = ref<FormInstance>()
|
||
const formData = reactive({
|
||
id: undefined as number | undefined,
|
||
name: '',
|
||
brand: '',
|
||
public_ip: '',
|
||
public_port: undefined as number | undefined,
|
||
internal_ip: '',
|
||
internal_port: undefined as number | undefined,
|
||
port: undefined as number | undefined,
|
||
username: '',
|
||
password: '',
|
||
status: 1 as number,
|
||
remark: ''
|
||
})
|
||
|
||
const rules: FormRules = {
|
||
name: [{ required: true, message: '请输入服务器名称', trigger: 'blur' }]
|
||
}
|
||
|
||
const isSuccess = (res: any) => res && [0, 1, 200].includes(res.code)
|
||
|
||
function formatStatus(status: number | undefined) {
|
||
if (status === 1) return { text: '启用', type: 'success' as const }
|
||
if (status === 0) return { text: '禁用', type: 'info' as const }
|
||
return { text: '--', type: 'info' as const }
|
||
}
|
||
|
||
/** 解包 ScanBox 的条件对象值,转为 API where 可接受的格式 */
|
||
function unwrapConditionValue(raw: unknown): string | number | null | Record<string, unknown> {
|
||
if (raw === null || raw === undefined || raw === '') return null
|
||
if (Array.isArray(raw) && raw.length === 0) return null
|
||
|
||
// ScanBox 对象值格式:{ operator, value }
|
||
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
||
const obj = raw as Record<string, unknown>
|
||
if (['isnull', 'isnotnull'].includes(String(obj.operator ?? ''))) {
|
||
return { operator: obj.operator as string, value: null }
|
||
}
|
||
const val = obj.value !== undefined ? obj.value : raw
|
||
if (val === null || val === undefined || val === '' || (Array.isArray(val) && val.length === 0)) {
|
||
return null
|
||
}
|
||
return { operator: obj.operator || 'eq', value: val }
|
||
}
|
||
|
||
return raw as string | number
|
||
}
|
||
|
||
function buildWhere(raw: any): Record<string, unknown> {
|
||
if (!raw) return {}
|
||
|
||
// ScanBox 发出 { conditions: { ... } },先解包
|
||
const conds =
|
||
raw.conditions && typeof raw.conditions === 'object' && !Array.isArray(raw.conditions)
|
||
? raw.conditions
|
||
: raw
|
||
|
||
if (!conds || typeof conds !== 'object') return {}
|
||
|
||
const where: Record<string, unknown> = {}
|
||
|
||
for (const key of Object.keys(conds)) {
|
||
const result = unwrapConditionValue(conds[key])
|
||
if (result === null) continue
|
||
|
||
if (key === 'quickWhere') {
|
||
// quickWhere 直接传字符串
|
||
where.quickWhere = typeof result === 'string' ? result : String((result as Record<string, unknown>).value ?? result)
|
||
} else if (key === 'status') {
|
||
where.status = typeof result === 'object' ? Number((result as Record<string, unknown>).value) : Number(result)
|
||
} else {
|
||
where[key] = result
|
||
}
|
||
}
|
||
|
||
return where
|
||
}
|
||
|
||
async function loadList() {
|
||
if (loading.value) return
|
||
loading.value = true
|
||
const start = Date.now()
|
||
try {
|
||
const where = buildWhere(currentConditionRaw.value)
|
||
const res: any = await list({
|
||
pagination: { page: pagination.currentPage, pageSize: pagination.pageSize },
|
||
where
|
||
})
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '获取录像服务器列表失败')
|
||
tableData.value = []
|
||
pagination.total = 0
|
||
return
|
||
}
|
||
tableData.value = res?.list || []
|
||
pagination.total = Number(res?.pagination?.total ?? res?.total ?? 0)
|
||
} catch {
|
||
tableData.value = []
|
||
pagination.total = 0
|
||
ElMessage.error('获取录像服务器列表失败')
|
||
} finally {
|
||
queryTime.value = Date.now() - start
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function handleConditionChange(condition: any) {
|
||
currentConditionRaw.value = condition
|
||
pagination.currentPage = 1
|
||
loadList()
|
||
}
|
||
|
||
function handleReset() {
|
||
currentConditionRaw.value = null
|
||
pagination.currentPage = 1
|
||
if (scanBox.value?.reset) scanBox.value.reset()
|
||
loadList()
|
||
}
|
||
|
||
function handlePageChange(page: number) {
|
||
pagination.currentPage = page
|
||
loadList()
|
||
}
|
||
|
||
function handlePageSizeChange(size: number) {
|
||
pagination.pageSize = size
|
||
pagination.currentPage = 1
|
||
loadList()
|
||
}
|
||
|
||
function resetForm() {
|
||
formData.id = undefined
|
||
formData.name = ''
|
||
formData.brand = ''
|
||
formData.public_ip = ''
|
||
formData.public_port = undefined
|
||
formData.internal_ip = ''
|
||
formData.internal_port = undefined
|
||
formData.port = undefined
|
||
formData.username = ''
|
||
formData.password = ''
|
||
formData.status = 1
|
||
formData.remark = ''
|
||
formRef.value?.clearValidate()
|
||
}
|
||
|
||
function openCreate() {
|
||
dialogMode.value = 'create'
|
||
dialogVisible.value = true
|
||
resetForm()
|
||
}
|
||
|
||
function openEdit(row: CameraServerRecord) {
|
||
if (!row.id) return
|
||
dialogMode.value = 'edit'
|
||
dialogVisible.value = true
|
||
formData.id = row.id
|
||
formData.name = row.name || ''
|
||
formData.brand = row.brand || ''
|
||
formData.public_ip = row.public_ip || ''
|
||
formData.public_port = row.public_port
|
||
formData.internal_ip = row.internal_ip || ''
|
||
formData.internal_port = row.internal_port
|
||
formData.port = row.port
|
||
formData.username = row.username || ''
|
||
formData.password = ''
|
||
formData.status = row.status ?? 1
|
||
formData.remark = row.remark || ''
|
||
formRef.value?.clearValidate()
|
||
}
|
||
|
||
async function submitForm() {
|
||
if (!formRef.value || saving.value) return
|
||
const valid = await formRef.value.validate().catch(() => false)
|
||
if (!valid) return
|
||
|
||
saving.value = true
|
||
try {
|
||
if (dialogMode.value === 'create') {
|
||
const res: any = await create({
|
||
name: formData.name.trim(),
|
||
brand: formData.brand || undefined,
|
||
public_ip: formData.public_ip.trim() || undefined,
|
||
public_port: formData.public_port,
|
||
internal_ip: formData.internal_ip.trim() || undefined,
|
||
internal_port: formData.internal_port,
|
||
port: formData.port,
|
||
username: formData.username.trim() || undefined,
|
||
password: formData.password || undefined,
|
||
status: formData.status,
|
||
remark: formData.remark.trim() || undefined
|
||
})
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '新增失败')
|
||
return
|
||
}
|
||
ElMessage.success('新增成功')
|
||
} else {
|
||
if (!formData.id) return
|
||
const res: any = await update({
|
||
id: formData.id,
|
||
name: formData.name.trim() || undefined,
|
||
brand: formData.brand || undefined,
|
||
public_ip: formData.public_ip.trim() || undefined,
|
||
public_port: formData.public_port,
|
||
internal_ip: formData.internal_ip.trim() || undefined,
|
||
internal_port: formData.internal_port,
|
||
port: formData.port,
|
||
username: formData.username.trim() || undefined,
|
||
password: formData.password || undefined,
|
||
status: formData.status,
|
||
remark: formData.remark.trim() || undefined
|
||
})
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '更新失败')
|
||
return
|
||
}
|
||
ElMessage.success('更新成功')
|
||
}
|
||
dialogVisible.value = false
|
||
loadList()
|
||
} catch {
|
||
ElMessage.error('保存失败')
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
function handleDelete(row: CameraServerRecord) {
|
||
if (!row.id) return
|
||
const name = row.name || String(row.id)
|
||
ElMessageBox.confirm(`确认删除录像服务器「${name}」吗?删除后关联的摄像头将不可用`, '提示', { type: 'warning' })
|
||
.then(async () => {
|
||
const res: any = await del({ id: row.id as number })
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '删除失败')
|
||
return
|
||
}
|
||
ElMessage.success('删除成功')
|
||
if (tableData.value.length === 1 && pagination.currentPage > 1) {
|
||
pagination.currentPage -= 1
|
||
}
|
||
loadList()
|
||
})
|
||
.catch(() => undefined)
|
||
}
|
||
|
||
/**
|
||
* 连通测试:先尝试内网 IP,失败后回退到公网 IP
|
||
* 若列表数据不含密码,则弹出输入框让用户填写
|
||
*/
|
||
async function handleTestConnection(row: CameraServerRecord) {
|
||
const id = row.id!
|
||
if (testLoadingMap.value[id]) return
|
||
|
||
const internalIp = (row.internal_ip || '').trim()
|
||
const publicIp = (row.public_ip || '').trim()
|
||
const username = (row.username || '').trim()
|
||
let password = (row.password || '').trim()
|
||
|
||
if (!internalIp && !publicIp) {
|
||
ElMessage.warning('该服务器未配置内网IP与公网IP,无法测试连通')
|
||
return
|
||
}
|
||
|
||
if (!username) {
|
||
ElMessage.warning('缺少用户名,无法测试连通')
|
||
return
|
||
}
|
||
|
||
// 若列表未返回密码,弹出输入框
|
||
if (!password) {
|
||
try {
|
||
const { value } = await ElMessageBox.prompt('请输入录像服务器密码', '连通测试', {
|
||
inputType: 'password',
|
||
confirmButtonText: '开始测试',
|
||
cancelButtonText: '取消',
|
||
inputPlaceholder: '请输入密码'
|
||
})
|
||
if (!value?.trim()) {
|
||
ElMessage.warning('密码不能为空')
|
||
return
|
||
}
|
||
password = value.trim()
|
||
} catch {
|
||
// 用户取消
|
||
return
|
||
}
|
||
}
|
||
|
||
testLoadingMap.value = { ...testLoadingMap.value, [id]: true }
|
||
const localBaseUrl = 'http://localhost:17311'
|
||
|
||
const tryIp = async (label: string, ip: string, port?: number): Promise<boolean> => {
|
||
try {
|
||
await testConnection(localBaseUrl, {
|
||
ip,
|
||
port: port || row.port,
|
||
user: username,
|
||
pass: password
|
||
})
|
||
ElMessage.success(`「${row.name || row.id}」连通成功(${label}:${ip})`)
|
||
return true
|
||
} catch (e: any) {
|
||
console.warn(`[连通测试] ${label} ${ip} 失败:`, e?.message || e)
|
||
return false
|
||
}
|
||
}
|
||
|
||
// 先尝试内网 IP(使用内网端口,回退到 SDK 端口)
|
||
if (internalIp) {
|
||
const ok = await tryIp('内网IP', internalIp, row.internal_port)
|
||
if (ok) {
|
||
testLoadingMap.value = { ...testLoadingMap.value, [id]: false }
|
||
return
|
||
}
|
||
}
|
||
|
||
// 回退到公网 IP(使用公网端口,回退到 SDK 端口)
|
||
if (publicIp && publicIp !== internalIp) {
|
||
const ok = await tryIp('公网IP', publicIp, row.public_port)
|
||
if (ok) {
|
||
testLoadingMap.value = { ...testLoadingMap.value, [id]: false }
|
||
return
|
||
}
|
||
}
|
||
|
||
testLoadingMap.value = { ...testLoadingMap.value, [id]: false }
|
||
const triedIps = [internalIp, publicIp].filter(Boolean).join('、')
|
||
ElMessage.error(`「${row.name || row.id}」连通失败(已尝试:${triedIps})`)
|
||
}
|
||
|
||
onMounted(() => {
|
||
loadList()
|
||
})
|
||
</script>
|
||
|
||
<style scoped>
|
||
.video-container {
|
||
display: flex;
|
||
gap: 12px;
|
||
padding: 12px;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.main-content {
|
||
flex: 1;
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-width: 0;
|
||
}
|
||
|
||
.toolbar {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.toolbar-left {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
}
|
||
|
||
.upper-wrapper {
|
||
flex: 1;
|
||
}
|
||
|
||
.empty-tip {
|
||
color: #909399;
|
||
padding: 24px;
|
||
}
|
||
|
||
/* IP + 端口 左右布局 */
|
||
.ip-port-row {
|
||
display: flex;
|
||
gap: 8px;
|
||
width: 100%;
|
||
}
|
||
|
||
.ip-input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
.port-input {
|
||
width: 130px;
|
||
flex-shrink: 0;
|
||
}
|
||
</style>
|