'use strict'; const { Controller } = require('egg'); /** * web 用户端:**测试页**用的模拟接口(`/api/web/test/*`) * * 前端「测试页」把真机链路拆开跑,方便没有实物设备时联调: * - `POST /api/web/test/device-login`:模拟「设备发码 → 有人扫码确认」这一步。 * 请求方(已登录用户)就是**确认人**,等价于手机上点了确认 —— 会真的建/认领设备记录并**换发长期 JWT**。 * - `POST /api/web/test/scan`:模拟「中控上报」(拿 SN + 长期 JWT 调,等价于设备的 `/api/scan/*`)。 * * 两者都不绕开校验:设备令牌仍按 `service.scanner.resolveByToken()` 完整验签 + 比对 jti。 */ class WebTestController extends Controller { /** * POST /api/web/test/device-login * body: `{ sn?, name?, vendor?, model?, firmware?, expiresIn? }` * → `{ ...设备信息, token, tokenType, firstTime }` */ async deviceLogin() { const { ctx } = this; const body = ctx.request.body || {}; const expiresIn = this.resolveDuration(body.expiresIn); const created = await ctx.service.scanner.loginByScan({ sn: body.sn, name: body.name, vendor: body.vendor, model: body.model, firmware: body.firmware, ip: ctx.ip, userId: ctx.state.user.id, subjectId: ctx.state.subjectId, expiresIn, }); // 再查一次,拿到带绑定用户信息的完整数据 const detail = await ctx.service.scanner.findById(created.id); ctx.logger.info('[web-test] 模拟设备扫码登录: %s(确认人 %s,主体 %s)', created.sn, ctx.state.user.id, ctx.state.subjectId); ctx.body = { ok: true, data: Object.assign({}, detail, { token: created.token, tokenType: created.tokenType, firstTime: created.firstTime, }), }; } /** * 令牌时长(可选):只能申请比 `config.jwt.deviceExpiresIn` 更短的,超上限 422 * @return {number|undefined} 秒数;没传返回 undefined(用默认值) */ resolveDuration(raw) { const { ctx } = this; const value = String(raw || '').trim(); if (!value) return undefined; const { jwt } = ctx.app.config; const maxSpec = String(jwt.deviceExpiresIn || jwt.scannerExpiresIn || '365d'); const max = ctx.service.jwt.parseDuration(maxSpec); const seconds = ctx.service.jwt.parseDuration(value); if (!seconds) ctx.throw(422, 'expiresIn 格式不对(支持 30d / 12h / 90m / 3600 这类写法)'); if (max && seconds > max) ctx.throw(422, `expiresIn 不能超过服务端上限 ${maxSpec}`); return seconds; } /** * POST /api/web/test/scan * body: `{ sn, token, channelId, channelName?, barcode, scanTime? }` * → `{ inserted, duplicated, ... }`(与设备上报同结构) */ async scan() { const { ctx } = this; const body = ctx.request.body || {}; const sn = String(body.sn || '').trim(); const token = String(body.token || '').trim(); const channelId = String(body.channelId || '').trim(); const channelName = String(body.channelName || '').trim(); const barcode = String(body.barcode || '').trim(); const scanTime = String(body.scanTime || '').trim(); if (!sn || !token) ctx.throw(422, '设备 SN 与长期 JWT 都不能为空'); if (!channelId) ctx.throw(422, '通道 id 不能为空'); if (!barcode) ctx.throw(422, '条码不能为空'); // 与设备上报走**同一套**校验(验签 + 回查 + jti 比对) const scanner = await ctx.service.scanner.resolveByToken(token, { sn }); await ctx.service.scanner.heartbeat(scanner, ctx.ip); const channelResult = await ctx.service.scan.reportChannels(scanner, [{ channelId, channelName }]); const scanResult = await ctx.service.scan.ingest(scanner, [{ channelId, barcode, scanTime: scanTime || new Date(), }]); ctx.logger.info('[web-test] 模拟上报完成: %s 通道=%s 条码=%s', sn, channelId, barcode); ctx.body = { ok: true, data: Object.assign({ sn, channelId, channelName, barcode }, scanResult, { channelCreated: channelResult.created, channelUpdated: channelResult.updated, }), }; } } module.exports = WebTestController;