liufumingren fe8c0d62a1 chore(ports): 本机服务与边缘代理端口整体迁出 173xx
- 桌面端本机 HTTP/WS:17300/17301 → 16000/16001(前端、浏览器扩展、文档同步)
- 后台服务局域网:UDP 17310/TCP 17311 → UDP 16100/TCP 16101(16010 被向日葵占用,统一避开 160xx)
- 防火墙规则随端口更新,安装/升级时清理旧 17xxx 规则,避免继续占用别人的端口
- 桌面端版本升到 1.0.162

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-18 15:51:41 +08:00

163 lines
4.9 KiB
JavaScript
Raw Permalink 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');
/** 路径参数转正整数,非法返回 null */
function toId(value) {
const id = Number(value);
return Number.isInteger(id) && id > 0 ? id : null;
}
/**
* web 用户端:**我的 NVR**(`/api/web/nvrs`)
*
* 拉取通道是由**前端**发起的两步:
* 1. 页面直接调本机软件 `POST http://localhost:16000/api/hik/channels`(带 host/port/user/password)
* 2. 用户勾选通道后,把结果 POST 到 `/api/web/nvrs/:id/channels` 落库
* 后端服务器**不接触设备**(设备在用户内网)。
*/
class WebNvrController extends Controller {
async index() {
const { ctx } = this;
const { page, pageSize, keyword, status } = ctx.query;
const data = await ctx.service.nvr.list({
page,
pageSize,
keyword,
status,
subjectId: ctx.state.subjectId,
});
ctx.body = { ok: true, data };
}
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.nvr.findOwned(id, ctx.state.subjectId) };
}
/**
* GET /api/web/nvrs/:id/credential —— 拉取通道用的登录凭据
*
* 密码同时存在库里(密文)与前端(下面这步返回的明文),
* 前端拿到后立刻转给本机软件,不再回存到页面状态里。
*/
async credential() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const data = await ctx.service.nvr.credentialOf(id, ctx.state.subjectId);
ctx.logger.info('[web] 主体 %s 读取 NVR %s 登录凭据(拉取通道)', ctx.state.subjectId, id);
ctx.body = { ok: true, data };
}
/** POST /api/web/nvrs —— 新增(同 host+port+username 已存在时更新) */
async create() {
const { ctx } = this;
const { name, host, port, username, user, password, serialNumber, deviceModel, remark } = ctx.request.body || {};
const data = await ctx.service.nvr.create({
subjectId: ctx.state.subjectId,
userId: ctx.state.user.id,
name,
host,
port,
username: username || user,
password,
serialNumber,
deviceModel,
remark,
});
ctx.logger.info('[web] 主体 %s 新增/更新 NVR %s', ctx.state.subjectId, data.id);
ctx.status = 201;
ctx.body = { ok: true, data };
}
/** PUT /api/web/nvrs/:id */
async update() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const { name, host, port, username, user, password, remark, status } = ctx.request.body || {};
const data = await ctx.service.nvr.update(id, ctx.state.subjectId, {
name,
host,
port,
username: username || user,
password,
remark,
status,
});
ctx.body = { ok: true, data };
}
/** DELETE /api/web/nvrs/:id —— 连带删除其下摄像头并解除工作台绑定 */
async destroy() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const data = await ctx.service.nvr.remove(id, ctx.state.subjectId);
ctx.body = { ok: true, data };
}
/** GET /api/web/nvrs/:id/channels —— 该 NVR 已导入的摄像头 */
async cameras() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const list = await ctx.service.camera.listByNvr(id, ctx.state.subjectId);
ctx.body = { ok: true, data: { nvrId: id, list } };
}
/**
* POST /api/web/nvrs/:id/channels —— 导入(勾选)通道为摄像头
*
* body: { serialNumber?, deviceModel?, channels: [{ channel, type, name?, ip?, port?, enabled? }] }
*/
async syncChannels() {
const { ctx } = this;
const id = toId(ctx.params.id);
if (!id) ctx.throw(400, 'id 必须是正整数');
const { serialNumber, deviceModel, channels } = ctx.request.body || {};
if (!Array.isArray(channels) || channels.length === 0) {
await ctx.service.nvr.saveSyncResult(id, ctx.state.subjectId, {
serialNumber,
deviceModel,
error: '未选择任何通道',
});
ctx.throw(422, 'channels 不能为空');
}
const data = await ctx.service.camera.batchCreate({
subjectId: ctx.state.subjectId,
nvrId: id,
channels,
});
if (serialNumber !== undefined || deviceModel !== undefined) {
await ctx.service.nvr.saveSyncResult(id, ctx.state.subjectId, {
serialNumber,
deviceModel,
channelCount: data.list.length,
});
}
ctx.logger.info('[web] 主体 %s 导入 NVR %s 的通道 %d 个(新增 %d / 更新 %d)',
ctx.state.subjectId, id, channels.length, data.created, data.updated);
ctx.body = { ok: true, data };
}
}
module.exports = WebNvrController;