重构: aiot 模块重命名为 video,WVP 凭据移至环境变量
路径重命名:
- api/aiot/{alarm,device,edge,request} → api/video/{alarm,device,edge,request}
- views/aiot/{alarm,device,edge} → views/video/{alarm,device,edge}
- vite.config.mts 代理路径 /admin-api/aiot/* → /admin-api/video/*
video/request.ts 改造:
- WVP 用户名/密码 MD5 改读 import.meta.env,不再写死在源码里
- force 截图失败时补一条 console.debug,便于回溯 COS 图片加载异常
video/alarm/index.ts 顺带清理:
- 移除无调用方的重复 API getRecentAlerts(与 getAlertPage 重叠)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
323
apps/web-antd/src/api/video/device/index.ts
Normal file
323
apps/web-antd/src/api/video/device/index.ts
Normal file
@@ -0,0 +1,323 @@
|
||||
/**
|
||||
* Video 模块 - 设备管理 API
|
||||
*
|
||||
* 所有请求通过 wvpRequestClient 发送到 WVP 视频平台后端,
|
||||
* 由 Vite 代理按路径分发并 rewrite:
|
||||
* /video/device/proxy/* → WVP /api/proxy/*
|
||||
* /video/device/user/* → WVP /api/user/*
|
||||
* /video/device/server/* → WVP /api/server/media_server/*
|
||||
* /video/device/* → WVP /api/ai/*
|
||||
*/
|
||||
import { useAppConfig } from '@vben/hooks';
|
||||
|
||||
import { getWvpToken, wvpRequestClient } from '#/api/video/request';
|
||||
|
||||
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
|
||||
export namespace VideoDeviceApi {
|
||||
/** 分页响应结构 */
|
||||
export interface PageResult<T> {
|
||||
list: T[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
/** 摄像头(拉流代理) */
|
||||
export interface Camera {
|
||||
id?: number;
|
||||
type?: string; // 'default' | 'ffmpeg'
|
||||
app?: string;
|
||||
stream?: string;
|
||||
cameraCode?: string; // 摄像头唯一编码
|
||||
cameraName?: string; // 摄像头名称(用户自定义)
|
||||
edgeDeviceId?: string; // 绑定的边缘设备ID
|
||||
srcUrl?: string;
|
||||
timeout?: number;
|
||||
rtspType?: string; // '0'=TCP, '1'=UDP, '2'=Multicast
|
||||
enable?: boolean;
|
||||
enableAudio?: boolean;
|
||||
enableMp4?: boolean;
|
||||
enableDisableNoneReader?: boolean;
|
||||
relatesMediaServerId?: string;
|
||||
ffmpegCmdKey?: string;
|
||||
pulling?: boolean;
|
||||
mediaServerId?: string;
|
||||
streamKey?: string;
|
||||
createTime?: string;
|
||||
}
|
||||
|
||||
/** ROI 区域 */
|
||||
export interface Roi {
|
||||
id?: number;
|
||||
roiId?: string;
|
||||
cameraId?: string;
|
||||
deviceId?: string;
|
||||
name?: string;
|
||||
roiType?: string; // 'rectangle' | 'polygon'
|
||||
coordinates?: string;
|
||||
color?: string;
|
||||
priority?: number;
|
||||
enabled?: number; // 0 | 1
|
||||
description?: string;
|
||||
algorithms?: RoiAlgoBinding[];
|
||||
}
|
||||
|
||||
/** ROI 算法绑定(详情嵌套结构) */
|
||||
export interface RoiAlgoBinding {
|
||||
bind: AlgoBind;
|
||||
algorithm?: Algorithm;
|
||||
}
|
||||
|
||||
/** 算法绑定记录 */
|
||||
export interface AlgoBind {
|
||||
bindId?: string;
|
||||
roiId?: string;
|
||||
algoCode?: string;
|
||||
enabled?: number; // 0 | 1
|
||||
params?: string;
|
||||
}
|
||||
|
||||
/** 算法定义 */
|
||||
export interface Algorithm {
|
||||
id?: number;
|
||||
algoCode?: string;
|
||||
algoName?: string;
|
||||
description?: string;
|
||||
isActive?: boolean;
|
||||
paramSchema?: string;
|
||||
globalParams?: string;
|
||||
}
|
||||
|
||||
/** 媒体服务器 */
|
||||
export interface MediaServer {
|
||||
id: string;
|
||||
ip: string;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 摄像头管理 ====================
|
||||
|
||||
/** 摄像头列表(分页) */
|
||||
export function getCameraList(params: {
|
||||
page: number;
|
||||
count: number;
|
||||
query?: string;
|
||||
pulling?: boolean;
|
||||
}) {
|
||||
return wvpRequestClient.get<VideoDeviceApi.PageResult<VideoDeviceApi.Camera>>(
|
||||
'/video/device/proxy/list',
|
||||
{ params },
|
||||
);
|
||||
}
|
||||
|
||||
/** 新增摄像头 */
|
||||
export function addCamera(data: Partial<VideoDeviceApi.Camera>) {
|
||||
return wvpRequestClient.post('/video/device/proxy/add', data);
|
||||
}
|
||||
|
||||
/** 编辑摄像头 */
|
||||
export function updateCamera(data: Partial<VideoDeviceApi.Camera>) {
|
||||
return wvpRequestClient.post('/video/device/proxy/update', data);
|
||||
}
|
||||
|
||||
/** 保存摄像头(新增/编辑,有 id 为编辑) */
|
||||
export function saveCamera(data: Partial<VideoDeviceApi.Camera>) {
|
||||
return data.id ? updateCamera(data) : addCamera(data);
|
||||
}
|
||||
|
||||
/** 删除摄像头 */
|
||||
export function deleteCamera(id: number) {
|
||||
return wvpRequestClient.delete('/video/device/proxy/delete', {
|
||||
params: { id },
|
||||
});
|
||||
}
|
||||
|
||||
/** 开始拉流 */
|
||||
export function startCamera(id: number) {
|
||||
return wvpRequestClient.get('/video/device/proxy/start', { params: { id } });
|
||||
}
|
||||
|
||||
/** 停止拉流 */
|
||||
export function stopCamera(id: number) {
|
||||
return wvpRequestClient.get('/video/device/proxy/stop', { params: { id } });
|
||||
}
|
||||
|
||||
/** 在线媒体服务器列表 */
|
||||
export function getMediaServerList() {
|
||||
return wvpRequestClient.get<VideoDeviceApi.MediaServer[]>(
|
||||
'/video/device/server/online/list',
|
||||
);
|
||||
}
|
||||
|
||||
/** 摄像头选项列表(用于下拉搜索选择) */
|
||||
export function getCameraOptions() {
|
||||
return wvpRequestClient.get<
|
||||
{ cameraCode: string; cameraName: string }[]
|
||||
>('/video/device/camera/options');
|
||||
}
|
||||
|
||||
// ==================== ROI 区域管理 ====================
|
||||
|
||||
/** ROI 列表(分页) */
|
||||
export function getRoiList(params: {
|
||||
page: number;
|
||||
count: number;
|
||||
cameraId?: string;
|
||||
deviceId?: string;
|
||||
query?: string;
|
||||
}) {
|
||||
return wvpRequestClient.get<VideoDeviceApi.PageResult<VideoDeviceApi.Roi>>(
|
||||
'/video/device/roi/list',
|
||||
{ params },
|
||||
);
|
||||
}
|
||||
|
||||
/** ROI 详情(含算法绑定) */
|
||||
export function getRoiDetail(id: number) {
|
||||
return wvpRequestClient.get<VideoDeviceApi.Roi>(`/video/device/roi/${id}`);
|
||||
}
|
||||
|
||||
/** 某摄像头的所有 ROI */
|
||||
export function getRoiByCameraId(cameraId: string) {
|
||||
return wvpRequestClient.get<VideoDeviceApi.Roi[]>(
|
||||
'/video/device/roi/channel',
|
||||
{ params: { cameraId } },
|
||||
);
|
||||
}
|
||||
|
||||
/** 保存 ROI(新增/编辑) */
|
||||
export function saveRoi(data: Partial<VideoDeviceApi.Roi>) {
|
||||
return wvpRequestClient.post('/video/device/roi/save', data);
|
||||
}
|
||||
|
||||
/** 删除 ROI */
|
||||
export function deleteRoi(roiId: string) {
|
||||
return wvpRequestClient.delete(`/video/device/roi/delete/${roiId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取摄像头截图 URL
|
||||
*
|
||||
* 非 force 模式:直接返回 /snap/image 代理 URL(无时间戳,浏览器自动缓存)
|
||||
* force 模式:先触发边缘端截图,再返回带时间戳的代理 URL 破缓存
|
||||
*/
|
||||
export async function getSnapUrl(cameraCode: string, force = false): Promise<string> {
|
||||
const token = await getWvpToken();
|
||||
if (force) {
|
||||
// 强制刷新:先触发边缘端截图(等待完成)
|
||||
try {
|
||||
await wvpRequestClient.get('/video/device/roi/snap', {
|
||||
params: { cameraCode, force: true },
|
||||
});
|
||||
} catch (err) {
|
||||
// 截图请求可能超时,但 COS 上可能已有图片,继续返回代理 URL
|
||||
// 留一条日志便于图片加载 401/404 时回溯根因
|
||||
if (import.meta.env.DEV) {
|
||||
console.debug('[WVP] force snap 触发失败,继续返回已有代理 URL', err);
|
||||
}
|
||||
}
|
||||
// 加时间戳破浏览器缓存
|
||||
return (
|
||||
`${apiURL}/video/device/roi/snap/image` +
|
||||
`?cameraCode=${encodeURIComponent(cameraCode)}` +
|
||||
`&access-token=${encodeURIComponent(token)}` +
|
||||
`&t=${Date.now()}`
|
||||
);
|
||||
}
|
||||
// 非 force:使用代理端点,不加时间戳,浏览器自动缓存
|
||||
return (
|
||||
`${apiURL}/video/device/roi/snap/image` +
|
||||
`?cameraCode=${encodeURIComponent(cameraCode)}` +
|
||||
`&access-token=${encodeURIComponent(token)}`
|
||||
);
|
||||
}
|
||||
|
||||
// ==================== 算法管理 ====================
|
||||
|
||||
/** 算法列表 */
|
||||
export function getAlgorithmList(deviceId?: string) {
|
||||
return wvpRequestClient.get<VideoDeviceApi.Algorithm[]>(
|
||||
'/video/device/algorithm/list',
|
||||
{ params: deviceId ? { deviceId } : {} },
|
||||
);
|
||||
}
|
||||
|
||||
/** 保存算法全局参数 */
|
||||
export function saveAlgoGlobalParams(algoCode: string, globalParams: string) {
|
||||
return wvpRequestClient.post(
|
||||
`/video/device/algorithm/global-params/${algoCode}`,
|
||||
{ globalParams },
|
||||
);
|
||||
}
|
||||
|
||||
/** 批量更新设备算法参数 */
|
||||
export function updateDeviceAlgoParams(deviceId: string, algoCode: string, params: string) {
|
||||
return wvpRequestClient.post(
|
||||
`/video/device/algorithm/device-binds/${deviceId}/${algoCode}`,
|
||||
{ params },
|
||||
);
|
||||
}
|
||||
|
||||
// ==================== 算法绑定 ====================
|
||||
|
||||
/** 绑定算法到 ROI */
|
||||
export function bindAlgo(data: { roiId: string; algoCode: string }) {
|
||||
return wvpRequestClient.post('/video/device/roi/bindAlgo', data);
|
||||
}
|
||||
|
||||
/** 解绑算法 */
|
||||
export function unbindAlgo(bindId: string) {
|
||||
return wvpRequestClient.delete('/video/device/roi/unbindAlgo', {
|
||||
params: { bindId },
|
||||
});
|
||||
}
|
||||
|
||||
/** 更新算法绑定参数 */
|
||||
export function updateAlgoParams(data: {
|
||||
bindId: string;
|
||||
params?: string;
|
||||
enabled?: number;
|
||||
}) {
|
||||
return wvpRequestClient.post('/video/device/roi/updateAlgoParams', data);
|
||||
}
|
||||
|
||||
// ==================== 配置推送 ====================
|
||||
|
||||
/** 推送配置到边缘端 */
|
||||
export function pushConfig(cameraId: string) {
|
||||
return wvpRequestClient.post('/video/device/config/push', null, {
|
||||
params: { cameraId },
|
||||
});
|
||||
}
|
||||
|
||||
/** 一次性推送全部配置到本地Edge */
|
||||
export function pushAllConfig() {
|
||||
return wvpRequestClient.post<Record<string, any>>(
|
||||
'/video/device/config/push-all',
|
||||
);
|
||||
}
|
||||
|
||||
/** 导出摄像头配置 JSON */
|
||||
export function exportConfig(cameraId: string) {
|
||||
return wvpRequestClient.get<Record<string, any>>(
|
||||
'/video/device/config/export',
|
||||
{ params: { cameraId } },
|
||||
);
|
||||
}
|
||||
|
||||
// ==================== 告警图片代理 ====================
|
||||
|
||||
/**
|
||||
* 构造告警图片代理 URL(通过 WVP 下载 COS 图片后返回字节流)
|
||||
* @param imagePath COS 对象路径或完整 URL
|
||||
*/
|
||||
export async function getAlertImageUrl(imagePath: string): Promise<string> {
|
||||
if (!imagePath) return '';
|
||||
const token = await getWvpToken();
|
||||
return (
|
||||
`${apiURL}/video/device/alert/image` +
|
||||
`?imagePath=${encodeURIComponent(imagePath)}` +
|
||||
`&access-token=${encodeURIComponent(token)}`
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user