'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/cameras`) * * 摄像头由「NVR + 通道」产生,归属看所属 NVR 的 user_id。 * 添加走 `/api/web/nvrs/:id/channels`(批量导入),这里只做列表 / 改名 / 启停 / 删除。 */ class WebCameraController extends Controller { /** GET /api/web/cameras?page=&pageSize=&nvrId=&keyword=&status= */ async index() { const { ctx } = this; const { page, pageSize, nvrId, keyword, status } = ctx.query; const data = await ctx.service.camera.list({ page, pageSize, nvrId: nvrId ? Number(nvrId) : undefined, keyword, status, subjectId: ctx.state.subjectId, }); ctx.body = { ok: true, data }; } /** GET /api/web/cameras/:id */ async show() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const camera = await ctx.service.camera.findOwned(id, ctx.state.subjectId); ctx.body = { ok: true, data: camera.toJSON() }; } /** PUT /api/web/cameras/:id —— 改名 / 启停 / 备注 / 是否带云台 */ async update() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const { name, status, remark } = ctx.request.body || {}; const data = await ctx.service.camera.update(id, ctx.state.subjectId, { name, status, remark }); ctx.body = { ok: true, data: data.toJSON() }; } /** DELETE /api/web/cameras/:id —— 软删除并解除工作台绑定 */ async destroy() { const { ctx } = this; const id = toId(ctx.params.id); if (!id) ctx.throw(400, 'id 必须是正整数'); const data = await ctx.service.camera.remove(id, ctx.state.subjectId); ctx.body = { ok: true, data }; } /** POST /api/web/cameras/batch-delete —— { ids: number[] } 批量删除 */ async batchDestroy() { const { ctx } = this; const { ids } = ctx.request.body || {}; const data = await ctx.service.camera.batchRemove({ subjectId: ctx.state.subjectId, ids }); ctx.body = { ok: true, data }; } /** GET /api/web/cameras/options —— 我的全部摄像头(工作台绑定选择器,带 boundWorkstation) */ async options() { const { ctx } = this; const list = await ctx.service.camera.optionsOfUser(ctx.state.subjectId); ctx.body = { ok: true, data: { list } }; } } module.exports = WebCameraController;