WEB/src/api/camera_server/localServerApi.ts
liufumingren 737866aa92 feat: 摄像头管理模块 - 连通测试、通道拉取、实时预览
- 新增 localServerApi.ts: 本地代理 API (通道列表/连通测试/实时流)
- 新增 localCameraServer store: 管理本机多端口连接
- 新增 CameraPreviewDialog: 摄像头预览弹窗 (RTSP→MJPEG, 内外网回退)
- 新增 CameraPlayer: 海康SDK播放器组件(备用)
- 录像服务器列表增加连通测试按钮(先内网后公网)
- 摄像头表单选择服务器后自动拉取通道列表
- 通道选中自动填充设备信息(IP/端口/型号/序列号)
- camera API create/update 入参扩充(ip/port/model/serial_number/stream_type)
- custom_name 改为必填
- vite.config.ts 增加 /ISAPI 代理转发
- public/hikvision/ 海康Web SDK静态资源
2026-07-18 03:02:31 +08:00

119 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 本地录像服务器 HTTP REST API 列表
*
* 本机端口:17311 → 17312 → 17313
* 所有请求直接发往 localhost,不经过主后端 /system 代理。
*
* 使用方式:
* import { localServerApi } from '@/api/camera_server/localServerApi'
* const res = await localServerApi.getHikvisionChannels(baseUrl, params)
*
* 新增 API 时只需在此文件追加函数即可。
*/
import axios from 'axios'
// ============================================================
// 通用请求工具
// ============================================================
/** 创建针对指定 baseUrl 的 axios 实例(每次调用按需创建,避免共享拦截器) */
function createClient(baseUrl: string) {
return axios.create({
baseURL: baseUrl,
timeout: 15000,
headers: { 'Content-Type': 'application/json' }
})
}
/** 对本地 API 做统一响应解包(兼容多种格式) */
function unwrap<T = any>(res: any): T {
const body = res?.data
if (!body || typeof body !== 'object') return body as T
// 格式1: { success: true, channels: [...] }
if ('success' in body && body.success) {
return (body.channels !== undefined ? body.channels : body.data ?? body) as T
}
// 格式2: { code, data, message }
if ('code' in body) {
if (body.code !== 0 && body.code !== 200) {
throw new Error(body.message || body.msg || `请求失败 (code=${body.code})`)
}
return (body.data !== undefined ? body.data : body) as T
}
return body as T
}
// ============================================================
// API 列表(按模块分组,方便扩展)
// ============================================================
// ---------- 海康威视 ----------
/** 获取海康威视通道列表(通用请求,参数透传) */
export async function getHikvisionChannels(
baseUrl: string,
params?: Record<string, unknown>
) {
const client = createClient(baseUrl)
const res = await client.post('/api/hikvision/channels', params || {})
return unwrap<any[]>(res)
}
// ---------- 连通测试 ----------
export interface CameraServerConnectionParams {
ip: string
port?: number
user: string
pass: string
}
/**
* 测试录像服务器连通性
* 向本地代理服务发送请求,由其转发到目标录像服务器
*/
export async function testConnection(
baseUrl: string,
params: CameraServerConnectionParams
) {
const client = createClient(baseUrl)
const res = await client.post('/api/hikvision/channels', {
ip: params.ip,
port: params.port,
user: params.user,
pass: params.pass
})
return unwrap<any>(res)
}
// ---------- MJPEG 实时流 ----------
const DEFAULT_PROXY_BASE = 'http://localhost:17311'
/**
* 生成 MJPEG 实时流 URL
* 后端 GET /api/hikvision/live-stream 返回 multipart MJPEG 流
* 前端直接用 <img src="..."> 渲染
*
* @example
* getLiveStreamUrl({ ip: '192.168.8.241', user: 'admin', pass: 'ky666888', channel: 9, streamType: 1 })
* // → http://localhost:17311/api/hikvision/live-stream?ip=192.168.8.241&user=admin&pass=ky666888&channel=9&streamType=1
*/
export function getLiveStreamUrl(params: {
ip: string
user: string
pass: string
channel: number
streamType?: number
}): string {
const { ip, user, pass, channel, streamType = 1 } = params
return `${DEFAULT_PROXY_BASE}/api/hikvision/live-stream?ip=${encodeURIComponent(ip)}&user=${encodeURIComponent(user)}&pass=${encodeURIComponent(pass)}&channel=${channel}&streamType=${streamType}`
}
// ---------- 后续在此追加其他 API ----------
// export async function getSomeOtherApi(baseUrl: string, params?: any) { ... }