50 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Controller } = require('egg');
/**
* web 主体端:**当前主体的扫码记录**(`/api/web/records`)
*
* 鉴权:`middleware.apiAuth()` + `middleware.subjectAuth()`;范围限定为「当前主体」,
* 传了 `deviceId` 时还要校验该设备属于当前主体。
*/
class WebRecordController extends Controller {
/**
* GET /api/web/records
* query: `?page=&pageSize=&deviceId=&channelId=&barcode=&startTime=&endTime=`
* - `deviceId` 只能填当前主体下的设备(否则 404);不填 = 所有设备
* - `startTime` / `endTime` 支持 `2026-10-16 12:00:00`、10/13 位时间戳
*/
async index() {
const { ctx } = this;
const { page, pageSize, deviceId, channelId, barcode, startTime, endTime } = ctx.query;
const subjectId = ctx.state.subjectId;
const ids = await ctx.service.scanner.idsOfSubject(subjectId);
let scannerIds = ids;
if (deviceId) {
const id = Number(deviceId);
if (!ids.includes(id)) ctx.throw(404, '设备不存在或不属于当前主体');
scannerIds = [ id ];
}
const data = await ctx.service.scan.listRecords({
page, pageSize, subjectId, scannerIds, channelId, barcode, startTime, endTime,
});
ctx.body = {
ok: true,
data: Object.assign({}, data, {
// 拍平所属设备(前端表格直接显示),免得前端再查一次
list: data.list.map(row => Object.assign({}, row, {
deviceSn: row.scanner ? row.scanner.sn : null,
deviceName: row.scanner ? row.scanner.name : null,
scanner: undefined,
})),
}),
};
}
}
module.exports = WebRecordController;