W0: 网关API封装+数据模型+CORS启用

This commit is contained in:
2026-05-17 15:36:44 +08:00
parent bec241be15
commit 21f1df4dac
2 changed files with 129 additions and 0 deletions

View File

@@ -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");

View File

@@ -0,0 +1,126 @@
/**
* 网关 B 组接口封装
* 所有网关 API 调用使用 fetch() 直连,不走 Vol.Pro 后端中转
*/
const GW_BASE = 'http://localhost:5100'
/** GET 请求网关 */
export async function gwGet(url: string): Promise<any> {
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<any> {
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<string, any>
}
/** 摄像机视图模型 */
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<T> {
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<string, any>
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<Camera[]> {
const data = await gwGet(`/api/gateway/devices?adapter=${adapter}&page=1&size=100`)
return (data.items || []).map(toCamera)
}