191 lines
6.0 KiB
JavaScript
191 lines
6.0 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
|
||
/** 路径参数转正整数,非法返回 null */
|
||
function toId(value) {
|
||
const id = Number(value);
|
||
return Number.isInteger(id) && id > 0 ? id : null;
|
||
}
|
||
|
||
/**
|
||
* web 用户端:**我的工作台**(`/api/web/workstations`)
|
||
*
|
||
* 一个工作台可绑定多个摄像头 + 多个扫码通道:
|
||
* - `POST /:id/cameras` { cameraIds: number[] } 追加绑定
|
||
* - `DELETE /:id/cameras/:cameraId` 解绑
|
||
* - `POST /:id/scan-channels` { items: [{ scannerId, channelId, channelName? }] } 追加绑定
|
||
* - `DELETE /:id/scan-channels/:linkId` 解绑(按绑定记录 id)
|
||
* - `GET /api/web/workstation-scan-channels` 可绑定通道选择器
|
||
*/
|
||
class WebWorkstationController extends Controller {
|
||
/** GET /api/web/workstations?page=&pageSize=&keyword=&status=&wsType= */
|
||
async index() {
|
||
const { ctx } = this;
|
||
const { page, pageSize, keyword, status, wsType } = ctx.query;
|
||
|
||
const data = await ctx.service.workstation.list({
|
||
page,
|
||
pageSize,
|
||
keyword,
|
||
status,
|
||
wsType,
|
||
subjectId: ctx.state.subjectId,
|
||
});
|
||
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** GET /api/web/workstations/:id —— 详情(含已绑摄像头 / 扫码通道) */
|
||
async show() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
if (!id) ctx.throw(400, 'id 必须是正整数');
|
||
|
||
ctx.body = { ok: true, data: await ctx.service.workstation.detail(id, ctx.state.subjectId) };
|
||
}
|
||
|
||
/** POST /api/web/workstations —— { name?, wsType?, recordBeforeSec?, recordAfterSec?, recordMaxSec?, remark? } */
|
||
async create() {
|
||
const { ctx } = this;
|
||
const { code, name, wsType, recordBeforeSec, recordAfterSec, recordMaxSec, remark } = ctx.request.body || {};
|
||
|
||
const data = await ctx.service.workstation.create({
|
||
subjectId: ctx.state.subjectId,
|
||
userId: ctx.state.user.id,
|
||
code,
|
||
name,
|
||
wsType,
|
||
recordBeforeSec,
|
||
recordAfterSec,
|
||
recordMaxSec,
|
||
remark,
|
||
});
|
||
|
||
ctx.status = 201;
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** PUT /api/web/workstations/:id */
|
||
async update() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
if (!id) ctx.throw(400, 'id 必须是正整数');
|
||
|
||
const { name, wsType, recordBeforeSec, recordAfterSec, recordMaxSec, remark, status } = ctx.request.body || {};
|
||
const data = await ctx.service.workstation.update(id, ctx.state.subjectId, {
|
||
name, wsType, recordBeforeSec, recordAfterSec, recordMaxSec, remark, status,
|
||
});
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** DELETE /api/web/workstations/:id */
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
if (!id) ctx.throw(400, 'id 必须是正整数');
|
||
|
||
const data = await ctx.service.workstation.remove(id, ctx.state.subjectId);
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** POST /api/web/workstations/:id/cameras —— { cameraIds: [] } 追加绑定 */
|
||
async bindCameras() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
if (!id) ctx.throw(400, 'id 必须是正整数');
|
||
|
||
const { cameraIds } = ctx.request.body || {};
|
||
const data = await ctx.service.workstation.bindCameras({
|
||
workstationId: id,
|
||
subjectId: ctx.state.subjectId,
|
||
cameraIds,
|
||
});
|
||
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** DELETE /api/web/workstations/:id/cameras/:cameraId */
|
||
async unbindCamera() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
const cameraId = toId(ctx.params.cameraId);
|
||
if (!id || !cameraId) ctx.throw(400, 'id / cameraId 必须是正整数');
|
||
|
||
const data = await ctx.service.workstation.unbindCamera({
|
||
workstationId: id,
|
||
subjectId: ctx.state.subjectId,
|
||
cameraId,
|
||
});
|
||
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/**
|
||
* PUT /api/web/workstations/:id/cameras/:cameraId/layout
|
||
* body: { x?, y?, width?, height?, streamType?, zIndex? }
|
||
*
|
||
* 合成画面里这块摄像头的矩形(像素,相对 1920×1080 画布)与取流方式。
|
||
*/
|
||
async updateCameraLayout() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
const cameraId = toId(ctx.params.cameraId);
|
||
if (!id || !cameraId) ctx.throw(400, 'id / cameraId 必须是正整数');
|
||
|
||
const { x, y, width, height, streamType, zIndex } = ctx.request.body || {};
|
||
const data = await ctx.service.workstation.updateCameraLayout({
|
||
workstationId: id,
|
||
subjectId: ctx.state.subjectId,
|
||
cameraId,
|
||
patch: { x, y, width, height, streamType, zIndex },
|
||
});
|
||
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** POST /api/web/workstations/:id/scan-channels —— { items: [{ scannerId, channelId, channelName? }] } */
|
||
async bindScanChannels() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
if (!id) ctx.throw(400, 'id 必须是正整数');
|
||
|
||
const { items } = ctx.request.body || {};
|
||
const data = await ctx.service.workstation.bindScanChannels({
|
||
workstationId: id,
|
||
subjectId: ctx.state.subjectId,
|
||
items,
|
||
});
|
||
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** DELETE /api/web/workstations/:id/scan-channels/:linkId */
|
||
async unbindScanChannel() {
|
||
const { ctx } = this;
|
||
const id = toId(ctx.params.id);
|
||
const linkId = toId(ctx.params.linkId);
|
||
if (!id || !linkId) ctx.throw(400, 'id / linkId 必须是正整数');
|
||
|
||
const data = await ctx.service.workstation.unbindScanChannel({
|
||
workstationId: id,
|
||
subjectId: ctx.state.subjectId,
|
||
linkId,
|
||
});
|
||
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** GET /api/web/workstation-scan-channels?keyword= —— 当前用户可绑定的扫码通道 */
|
||
async availableScanChannels() {
|
||
const { ctx } = this;
|
||
const list = await ctx.service.workstation.availableScanChannels({
|
||
subjectId: ctx.state.subjectId,
|
||
keyword: ctx.query.keyword,
|
||
});
|
||
ctx.body = { ok: true, data: { list } };
|
||
}
|
||
}
|
||
|
||
module.exports = WebWorkstationController;
|