'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 }; } /** * GET /api/scanner/workstations —— 本机**工作台 + 全部绑定摄像头**(OSD 字幕用,需长期 JWT) * * 只返回绑定了本机扫码通道的工作台,每项带 `channels`(本机通道)与 `cameras` * (摄像头名 / 录像机通道号 / 录像机凭据)。设备端据此把「摄像头名 + 运单号」 * 写进对应通道的 OSD(= 录像机通道名),1 分钟没有扫码就还原成摄像头自己的名字。 */ async workstations() { const { ctx } = this; const data = await ctx.service.labelScan.workstationConfigOf(ctx.state.scanner); const cameraCount = data.items.reduce((sum, item) => sum + item.cameras.length, 0); ctx.logger.info( '[osd] 设备 %s 拉取工作台配置:%d 个工作台 / %d 路摄像头(warning %d)', ctx.state.scanner.sn, data.items.length, cameraCount, data.warnings.length, ); ctx.body = { ok: true, data }; } } module.exports = ScanController;