diff --git a/backend/app/controller/device/edge.js b/backend/app/controller/device/edge.js index e4304b8..0156b96 100644 --- a/backend/app/controller/device/edge.js +++ b/backend/app/controller/device/edge.js @@ -47,9 +47,43 @@ class DeviceEdgeController extends Controller { const body = ctx.request.body || {}; await ctx.service.edgeTask.submitResult(id, body); + // 跨网下载失败时,设备把 uploadTaskId 带回来 —— 立刻落 failed, + // 查询机不用干等到超时。 + if (body.ok === false && body.uploadTaskId) { + await ctx.service.edgeTask.markFailed(String(body.uploadTaskId), body.error || '设备下载失败'); + } + ctx.logger.info('[edge] 设备 %s 回任务 %s:ok=%s', ctx.state.scanner.sn, id, body.ok); ctx.body = { ok: true }; } + + /** + * 上传成片分片(跨网下载用) + * + * `POST /api/scanner/tasks/:id/upload?last=0|1`,body 是**原始字节**。 + * 用原始流而不是 JSON/base64:几 MB 的分片做 base64 会膨胀 33%,没有意义。 + * 该路由已在 config.bodyParser.ignore 里排除,所以这里直接读 `ctx.req`。 + * + * ⚠️ 分片必须按顺序追加(设备侧是顺序读文件的),不校验 seq 是为了少一次往返。 + */ + async upload() { + const { ctx } = this; + const id = String(ctx.params.id || '').trim(); + if (!id) ctx.throw(400, '缺少任务 id'); + + const last = String(ctx.query.last || '0') === '1'; + + try { + const meta = await ctx.service.edgeTask.appendChunk(id, ctx.req, { last }); + ctx.body = { ok: true, data: { size: meta.size, state: meta.state } }; + } catch (err) { + ctx.logger.warn('[edge] 上传分片失败 %s:%s', id, err.message); + + // 上传失败要落状态,否则查询机要白等两分钟 + await ctx.service.edgeTask.markFailed(id, err.message); + ctx.throw(500, `上传失败:${err.message}`); + } + } } module.exports = DeviceEdgeController; diff --git a/backend/app/controller/web/edge.js b/backend/app/controller/web/edge.js index 74242ca..bc18a34 100644 --- a/backend/app/controller/web/edge.js +++ b/backend/app/controller/web/edge.js @@ -1,6 +1,8 @@ 'use strict'; const { Controller } = require('egg'); +const fs = require('fs'); +const path = require('path'); /** * web 用户端:边缘能力(`/api/web/edge/*`) @@ -35,6 +37,131 @@ class WebEdgeController extends Controller { ctx.body = { ok: true, data }; } + + /** + * POST /api/web/edge/download { code } + * + * 跨网下载:云端让能访问 NVR 的扫码服务去拉流合并,传回云端中转, + * 查询机拿到 `taskId` 后再取文件(不会占住这个请求)。 + */ + async download() { + const { ctx } = this; + const body = ctx.request.body || {}; + const code = String(body.code || ctx.query.code || '').trim(); + if (!code) ctx.throw(422, '缺少运单号'); + + const data = await ctx.service.edgeTask.createDownloadTask({ + subjectId: ctx.state.subjectId, + code, + }); + + ctx.logger.info('[edge] 主体 %s 跨网下载 %s → %s', ctx.state.subjectId, code, data.state); + ctx.body = { ok: true, data }; + } + + /** + * GET /api/web/edge/download/:taskId —— 查进度 + * + * 只能查自己主体创建的任务(元数据里记了 subjectId)。 + */ + async downloadStatus() { + const { ctx } = this; + const meta = await this.requireOwnTask(ctx.params.taskId); + + ctx.body = { + ok: true, + data: { + state: meta.state, + size: meta.size || 0, + error: meta.error || null, + scannerSn: meta.scannerSn || null, + }, + }; + } + + /** + * GET /api/web/edge/download/:taskId/file —— 取成片 + * + * 支持 `Range`(浏览器 `