From 21f1df4dacbc16b43475ac3f0075668b269c2974 Mon Sep 17 00:00:00 2001 From: g82tt Date: Sun, 17 May 2026 15:36:44 +0800 Subject: [PATCH] =?UTF-8?q?W0:=20=E7=BD=91=E5=85=B3API=E5=B0=81=E8=A3=85+?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=A8=A1=E5=9E=8B+CORS=E5=90=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/IntegrationGateway.Host/Program.cs | 3 + warehouse/src/api/gateway.ts | 126 ++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 warehouse/src/api/gateway.ts diff --git a/gateway/src/IntegrationGateway.Host/Program.cs b/gateway/src/IntegrationGateway.Host/Program.cs index 4c7e38f..9764b52 100644 --- a/gateway/src/IntegrationGateway.Host/Program.cs +++ b/gateway/src/IntegrationGateway.Host/Program.cs @@ -27,7 +27,10 @@ builder.Services.AddHttpClient("VolPro", c => MaxConnectionsPerServer = 10 }); +builder.Services.AddCors(o => o.AddDefaultPolicy(p => p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); + var app = builder.Build(); +app.UseCors(); // ── 读取配置 ── var gwCfg = app.Configuration.GetSection("Gateway"); diff --git a/warehouse/src/api/gateway.ts b/warehouse/src/api/gateway.ts new file mode 100644 index 0000000..585b20e --- /dev/null +++ b/warehouse/src/api/gateway.ts @@ -0,0 +1,126 @@ +/** + * 网关 B 组接口封装 + * 所有网关 API 调用使用 fetch() 直连,不走 Vol.Pro 后端中转 + */ + +const GW_BASE = 'http://localhost:5100' + +/** GET 请求网关 */ +export async function gwGet(url: string): Promise { + const resp = await fetch(`${GW_BASE}${url}`) + if (!resp.ok) throw new Error(`网关请求失败: ${resp.status}`) + return resp.json() +} + +/** POST 请求网关 */ +export async function gwPost(url: string, body?: object): Promise { + const resp = await fetch(`${GW_BASE}${url}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: body ? JSON.stringify(body) : undefined + }) + if (!resp.ok) throw new Error(`网关请求失败: ${resp.status}`) + return resp.json() +} + +// ═══════════════════════════════════════════ +// 数据模型 +// ═══════════════════════════════════════════ + +/** 统一设备模型(对应网关 StandardDevice) */ +export interface StandardDevice { + deviceId: number + adapterCode: string + sourceId: string + name: string + category: string + group: string + isParent: boolean + parentSourceId?: string + isOnline: boolean + ipAddress?: string + port?: number + extra?: Record +} + +/** 摄像机视图模型 */ +export interface Camera { + id: string + name: string + location: string + status: 'online' | 'offline' + adapterCode: string + sourceId: string + streamUrl?: string + hasPtz: boolean +} + +/** 视频流地址集合 */ +export interface StreamUrls { + wsFlv?: string + httpFlv?: string + hls?: string + webRtc?: string + rtmp?: string + rtsp?: string +} + +/** 分页容器 */ +export interface PagedResult { + items: T[] + total: number +} + +/** 实时点位值 */ +export interface PointValue { + sourceDeviceId: string + pointIndex: number + value: number + updateTime?: string + interval: number +} + +/** 告警模型 */ +export interface StandardAlarm { + alarmId: string + deviceId?: string + adapterCode: string + level: string + title: string + content?: string + occurTime: string + status: string + actualValue?: number + thresholdValue?: number +} + +/** 设备树节点 */ +export interface DeviceTreeNode { + id: number + sourceId: string + name: string + type: number + objectType: number + tag?: string + option?: Record + children: DeviceTreeNode[] +} + +/** StandardDevice → Camera 映射 */ +export function toCamera(d: StandardDevice): Camera { + return { + id: d.sourceId, + name: d.name, + location: d.extra?.location || '', + status: d.isOnline ? 'online' : 'offline', + adapterCode: d.adapterCode, + sourceId: d.sourceId, + hasPtz: d.extra?.hasPtz === '1' || d.extra?.hasPtz === true + } +} + +/** 从网关获取设备列表并映射为 Camera[] */ +export async function fetchCameras(adapter: string): Promise { + const data = await gwGet(`/api/gateway/devices?adapter=${adapter}&page=1&size=100`) + return (data.items || []).map(toCamera) +}