- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权; 已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF), 开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。 单测 91 个通过(不需要本机数据库)。 - frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 / 首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。 - extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。 - 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、 .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
94 lines
3.0 KiB
JavaScript
94 lines
3.0 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 }] }
|
||
* 同一中控 + 通道 + 条码 + 扫描时间(秒) 重复上报只入库一次,不报错
|
||
*/
|
||
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;
|