liufumingren bca8f7270c 本机预览/云台/拉通道全部改走 WebSocket(桌面 1.0.75)
桌面端
- 新增 LocalWsServer(ws://127.0.0.1:17301):hello / ping / get_channels /
  start_preview / stop_preview / open_window / close_window / ptz / ptz_preset,
  一条连接可同时推多路 JPEG 帧(帧头带 sid);慢命令并发派发,云台命令按连接串行,
  页面断开自动清理会话与满帧率窗口
- LocalHttpServer 删除预览(MJPEG/快照/满帧率窗口)、/api/hik/channels、/api/hik/ptz*,
  HTTP 17300 只保留 /api/query/{code}、/api/activate-login、/api/playback/download,
  其余 POST 统一回 404 并提示已迁到 WS;不再依赖 HCNetSDK
- App 自行组装 HikStreamService -> HikPreviewService + HikNativePreviewService 注入 WS
- 后台服务加固:框架依赖(开发版)安装/启动时自动写服务环境变量 DOTNET_ROOT,
  避免 LocalSystem 找不到用户目录里的 .NET 8 而报「后台服务启动超时」

前端
- 新增 api/localWs.ts(连接/断线/请求-响应/二进制帧分发),补 getChannels / openWindow /
  closeWindow / ptz / ptzPreset
- CamerasView 预览只走 WS(保留「唤起本机软件 -> 等待 -> 提示安装」兜底),
  满帧率窗口、云台、预置点改走 WS
- NvrsView 拉取通道改走 WS(连不上时自动唤起一次再重试)
- CameraLayoutCanvas 合成画面 4 路改 WS 推帧
- 删除 createHikPreview/closeHikPreview*/openHikWindow/closeHikWindow*/ptzControl/ptzPreset
  与 isLocalServiceAlive/waitForLocalService

其他
- 移除 cameras.has_ptz(迁移 20261019233000)与相关接口/字段/UI
- 通道号按 struStreamMode 映射(IP 通道空槽导致 51 之后整体错位)
- 扫码 24 小时内只保留最后一次扫描(后上报时间更新时覆盖)
- 清理历史 DESKTOP 占位通道(迁移 20261019230000)
- README / desktop README 补 17301 与 WS 接口说明,修正已删除的 /api/health 描述
2026-10-20 00:47:31 +08:00

99 lines
3.4 KiB
JavaScript
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.

'use strict';
const { Controller } = require('egg');
/**
* 把上报体里的数组字段取出来,兼容几种写法:
* - { channels: [...] } / { records: [...] }
* - { channelId, channelName } / { barcode, scanTime, channelId }(单条)
* - { channels: '[{...}]' }(JSON 字符串,有些设备端会这么塞)
*/
function pickList(body, key, single) {
const raw = body[key];
if (Array.isArray(raw)) return raw;
if (typeof raw === 'string' && raw.trim()) {
try {
const parsed = JSON.parse(raw);
if (Array.isArray(parsed)) return parsed;
if (parsed && typeof parsed === 'object') return [ parsed ];
} catch (_) {
return [];
}
}
if (raw && typeof raw === 'object') return [ raw ];
if (body[single]) return [ body ];
return [];
}
class ScanController extends Controller {
/**
* POST /api/scan/channels —— 中控上报通道(需长期 JWT)
* body: { channels: [{ channelId, channelName }], replace?: boolean }
* 同一中控 + 同一 channelId 为同一个通道,重复上报即更新通道名;
* replace=true 时按全量对账(列表里没有的通道会被软删并解绑工作台),允许 channels 为空数组。
*/
async reportChannels() {
const { ctx } = this;
const body = ctx.request.body || {};
const channels = pickList(body, 'channels', 'channelId');
const replace = body.replace === true || body.replace === 'true' || body.replace === 1;
if (!channels.length && !replace) {
ctx.throw(422, 'channels 不能为空,body 形如 { channels: [{ channelId, channelName }] }');
}
const data = await ctx.service.scan.reportChannels(ctx.state.scanner, channels, { replace });
ctx.body = { ok: true, data };
}
/** GET /api/scan/channels —— 当前中控已登记的通道(需长期 JWT) */
async channels() {
const { ctx } = this;
const { page, pageSize, keyword, status } = ctx.query;
const data = await ctx.service.scan.listChannels({
page,
pageSize,
keyword,
status,
scannerId: ctx.state.scanner.id,
});
ctx.body = { ok: true, data };
}
/**
* POST /api/scan/records —— 中控上报扫码结果(需长期 JWT)
* body: { records: [{ barcode, scanTime, channelId }] }
* 同一中控 + 通道 + 条码 + 扫描时间(秒) 重复上报只入库一次;
* **同一运单号 24 小时内只保留一条**:扫描时间更新 → 覆盖时间与工作台快照;旧上报忽略,都不报错。
*/
async reportRecords() {
const { ctx } = this;
const body = ctx.request.body || {};
const records = pickList(body, 'records', 'barcode');
if (!records.length) ctx.throw(422, 'records 不能为空,body 形如 { records: [{ barcode, scanTime, channelId }] }');
const data = await ctx.service.scan.ingest(ctx.state.scanner, records);
ctx.body = { ok: true, data };
}
/**
* GET /api/scan/records —— 查当前中控上报的扫码记录(需长期 JWT)
* query: page / pageSize / channelId / barcode / startTime / endTime
*/
async records() {
const { ctx } = this;
const { page, pageSize, channelId, barcode, startTime, endTime } = ctx.query;
const data = await ctx.service.scan.listRecords({
page,
pageSize,
channelId,
barcode,
startTime,
endTime,
scannerId: ctx.state.scanner.id,
});
ctx.body = { ok: true, data };
}
}
module.exports = ScanController;