'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` 可绑定通道选择器 * * 成员登录(扫码登入工作台,见 `service/workstation_login.js` / `service/wx_gateway.js`): * - `POST /:id/login-qr` 生成一张扫码登入二维码(微信登录网关,一次性) * - `GET /:id/login-qr/status?state=&wait=` 长轮询扫码结果(确认后返回登录的成员) * - `GET /:id/members?status=&page=&pageSize=` 当前/历史成员(`status=1` 只看在线) * - `POST /:id/members/:recordId/offline` 手动下线某个成员 * - `POST /:id/members/offline-all` 把该工作台所有在线成员下线 * * ⚠️ 旧的「工作台登录码」(`WSC-`)与 `POST /api/web/workstation-login` 已废弃: * 现场登入改为「按需生成二维码 → 微信扫码确认」。 */ 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?, idleTimeoutSec?, remark? } */ async create() { const { ctx } = this; const { code, name, wsType, recordBeforeSec, recordAfterSec, recordMaxSec, idleTimeoutSec, 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, idleTimeoutSec, 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, idleTimeoutSec, remark, status } = ctx.request.body || {}; const data = await ctx.service.workstation.update(id, ctx.state.subjectId, { name, wsType, recordBeforeSec, recordAfterSec, recordMaxSec, idleTimeoutSec, 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 }; } /** * PUT /api/web/workstations/:id/cameras/:cameraId/label * body: { isLabelCamera?: boolean }(不传 = 设为面单摄像头) * * 仅散单工作台可设置,一台工作台最多一路。 */ async setLabelCamera() { const { ctx } = this; const id = toId(ctx.params.id); const cameraId = toId(ctx.params.cameraId); if (!id || !cameraId) ctx.throw(400, 'id / cameraId 必须是正整数'); const { isLabelCamera } = ctx.request.body || {}; const data = await ctx.service.workstation.setLabelCamera({ workstationId: id, subjectId: ctx.state.subjectId, cameraId, isLabelCamera, }); 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 } }; } /** * POST /api/web/workstations/:id/login-qr —— 生成扫码登入二维码 * * 返回 `{ state, pageUrl, qrUrl, expiresIn }`:前端把 `qrUrl` 当图片显示, * 然后拿 `state` 去 `GET /:id/login-qr/status` 长轮询。 */ async loginQr() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); // 5xx 的 ctx.throw 会被统一错误处理遮成「服务器内部错误」,这里直接给明确提示 if (!ctx.service.wxGateway.enabled) { ctx.status = 503; ctx.body = { ok: false, message: '未开通微信扫码登入(服务端未配置微信登录网关)' }; return; } const data = await ctx.service.workstation.createLoginQr(id, ctx.state.subjectId); ctx.body = { ok: true, data }; } /** * GET /api/web/workstations/:id/login-qr/status?state=&wait= —— 长轮询扫码结果 * * `data.status`:pending(还在等扫码)/ confirmed(附登录的成员)/ failed(附原因)/ expired(重新申请二维码) */ async loginQrStatus() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); // 仍然校验工作台归属:二维码能扫,但只有本主体管理员能看结果 await ctx.service.workstation.findOwned(id, ctx.state.subjectId); const result = await ctx.service.wxGateway.awaitSession(String(ctx.query.state || ''), { wait: ctx.query.wait, }); if (result.status === 'aborted') { // 客户端已经断开(关弹窗 / 换页):不用写响应 ctx.respond = false; return; } ctx.body = { ok: true, data: result }; } /** POST /api/web/workstations/:id/members/offline-all —— 把该工作台所有在线成员下线 */ async offlineAll() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const data = await ctx.service.workstation.offlineAllMembers(id, ctx.state.subjectId); ctx.body = { ok: true, data }; } /** GET /api/web/workstations/:id/members?status=1&startTime=&endTime=&page=&pageSize= —— 当前 / 历史成员 */ async members() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const { status, startTime, endTime, page, pageSize } = ctx.query; const data = await ctx.service.workstationLogin.list({ workstationId: id, subjectId: ctx.state.subjectId, status, startTime, endTime, page, pageSize, }); ctx.body = { ok: true, data }; } /** POST /api/web/workstations/:id/members/:recordId/offline —— 手动下线某个成员 */ async offlineMember() { const { ctx } = this; const id = toId(ctx.params.id); const recordId = toId(ctx.params.recordId); if (!id || !recordId) ctx.throw(400, 'id / recordId 必须是正整数'); const data = await ctx.service.workstationLogin.offlineOne({ workstationId: id, subjectId: ctx.state.subjectId, recordId, }); ctx.body = { ok: true, data }; } } module.exports = WebWorkstationController;