/** * 本地录像服务器 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(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 ) { const client = createClient(baseUrl) const res = await client.post('/api/hikvision/channels', params || {}) return unwrap(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(res) } // ---------- MJPEG 实时流 ---------- const DEFAULT_PROXY_BASE = 'http://localhost:17311' /** * 生成 MJPEG 实时流 URL * 后端 GET /api/hikvision/live-stream 返回 multipart MJPEG 流 * 前端直接用 渲染 * * @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) { ... }