95 lines
3.1 KiB
JavaScript
95 lines
3.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 }] }
|
||
* 同一中控 + 同一 channelId 为同一个通道,重复上报即更新通道名
|
||
*/
|
||
async reportChannels() {
|
||
const { ctx } = this;
|
||
const body = ctx.request.body || {};
|
||
const channels = pickList(body, 'channels', 'channelId');
|
||
if (!channels.length) ctx.throw(422, 'channels 不能为空,body 形如 { channels: [{ channelId, channelName }] }');
|
||
|
||
const data = await ctx.service.scan.reportChannels(ctx.state.scanner, channels);
|
||
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;
|