- 工作台成员扫码登入:workstation_login_records 记录谁在什么时候登入/下线了哪台工作台, 超时(团购 1h / 散单 30min)由 schedule/workstation_idle_offline 自动下线; 运单查询的 packers 取当时的登入成员 - 面单摄像头识别:workstation_cameras.is_label_camera + service/label_scan 下发本机该跑的识别配置, 设备端(桌面端 LabelScan 工作线程)按配置做面单识别并上报 - 虚拟通道 CAM-<工作台id>:工作台没接扫码枪时,面单摄像头那一路顶替扫码枪上报,records 规则按 ws_type 区分 - 桌面端:新增 LabelScanMonitor/LabelScanWorker/ScanDeviceKind 与面单预览页,扫描通道支持改名,静默窗口 5s Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
115 lines
4.1 KiB
JavaScript
115 lines
4.1 KiB
JavaScript
'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 };
|
||
}
|
||
/**
|
||
* GET /api/scanner/label-cameras —— 本机该跑的**面单摄像头识别配置**(需长期 JWT)
|
||
*
|
||
* 只返回本主体、散单、启用中的工作台里被设为「面单摄像头」的那几路,
|
||
* 附上报通道与录像机凭据(RTSP 路径由设备端按录像机起始数字通道推算)。
|
||
* `revision` 是内容指纹:变了设备端就重建识别任务。
|
||
*/
|
||
async labelCameras() {
|
||
const { ctx } = this;
|
||
const data = await ctx.service.labelScan.configOf(ctx.state.scanner);
|
||
ctx.logger.info(
|
||
'[label-scan] 设备 %s 拉取面单配置:%d 路(warning %d)',
|
||
ctx.state.scanner.sn, data.items.length, data.warnings.length,
|
||
);
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
}
|
||
|
||
module.exports = ScanController;
|