packServer/backend/app/router.js
liufumingren 7b11f7052d feat(subject): 主体到期暂停设备上报,新增续期订单与支付回调
- 主体状态缓存(Redis 优先、进程内兜底):设备心跳与上报判断不再查库,续期/停用后主动失效
- 上报写接口按缓存校验期限:过期 403 subject_expired、停用 subject_disabled、主体缺失 subject_missing
- 心跳与 /api/scanner/me 返回 subject 状态,桌面端据此暂停推送并在续期后自动补传积压
- 新增续期订单(subject_orders)与接口,支付回调 POST /api/pay/notify 幂等、支持 HMAC 签名,
  另提供内部令牌模拟支付用于联调
- 顺带提交工作区中已完成的「登录协议」后端改动(agreement):与本次改动共用 router.js / config.default.js

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-20 18:55:57 +08:00

190 lines
13 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';
/**
* 路由定义
* @param {Egg.Application} app - egg application
*/
module.exports = (app) => {
const { router, controller, middleware } = app;
const apiAuth = middleware.apiAuth();
const subjectAuth = middleware.subjectAuth();
const subjectActive = middleware.subjectActive();
const internalAuth = middleware.internalAuth();
const scannerAuth = middleware.scannerAuth();
// ==================== JSON 接口 ====================
// 控制器按「谁在调」分模块(app/controller/<模块>/):
// web/ 前端 SPA(用户端,toC:用户自己管自己的设备/记录)—— header 令牌(user JWT)
// device/ 设备(长期 JWT,typ=scanner)
// auth/ 公开的登录码接口(发码/查码/确认)
// internal/ 运维/内部(用户与配置的 CRUD)—— 需内部令牌
// home / health 放在根(公共)
//
// ⚠️ 后端**不再渲染任何页面**:`/` 这一侧由 app/middleware/frontend.js 处理
// (开发时反代到前端 dev server,生产时托管 frontend/dist)。
router.get('/api/info', controller.home.index);
router.get('/health', controller.health.index);
// ---------- 安装包分发(软件自动更新) ----------
// 安装包本体 `GET /download/YuncangTraceSetup-x.y.z.msi` 由 egg-static 提供(config.static);
// version.json **必须走路由**:static 命中后不会 next(),改不了缓存头(会被缓存一年)。
router.get('/download/version.json', controller.download.version);
// ---------- 协议(公开读取;同意记录在登录时落库) ----------
router.get('/api/agreements/current', controller.agreement.current);
// ---------- web 用户端(前端 SPA 调;登录/注册等公开,其余要令牌) ----------
router.post('/api/web/auth/login', controller.web.auth.login);
router.post('/api/web/auth/register/code', controller.web.auth.sendRegisterCode);
router.post('/api/web/auth/register', controller.web.auth.register);
router.post('/api/web/auth/forgot', controller.web.auth.forgot);
router.post('/api/web/auth/reset', controller.web.auth.reset);
router.get('/api/web/me', apiAuth, controller.web.profile.show);
router.put('/api/web/me', apiAuth, controller.web.profile.update);
router.put('/api/web/me/password', apiAuth, controller.web.profile.changePassword);
// ---------- 主体(租户):不输主体 id,靠「当前用户是否是成员」判定 ----------
// 除这几个以外的业务接口都要额外带 `X-Subject-Id`(middleware/subjectAuth)
router.get('/api/web/subjects', apiAuth, controller.web.subject.index);
router.post('/api/web/subjects', apiAuth, controller.web.subject.create);
router.get('/api/web/subjects/:id', apiAuth, controller.web.subject.show);
router.put('/api/web/subjects/:id', apiAuth, controller.web.subject.update);
router.get('/api/web/subjects/:id/members', apiAuth, controller.web.subject.members);
router.post('/api/web/subjects/:id/members', apiAuth, controller.web.subject.addMember);
router.delete('/api/web/subjects/:id/members/:memberId', apiAuth, controller.web.subject.removeMember);
// 给未绑定账号/微信的成员生成绑定码(手机扫码后绑定)
router.post('/api/web/subjects/:id/members/:memberId/bind-code', apiAuth, controller.web.subject.memberBindCode);
// 续期订单:下单不改变期限,支付结果由渠道回调 `POST /api/pay/notify` 驱动
// ⚠️ 不挂 subjectAuth:主体已过期也要能下单续期
router.get('/api/web/subjects/:id/orders', apiAuth, controller.web.subjectOrder.index);
router.post('/api/web/subjects/:id/orders', apiAuth, controller.web.subjectOrder.create);
// 成员绑定:手机扫码页(公开查状态)+ 登录后确认绑定
router.get('/api/subject/member-bind', controller.auth.memberBind.status);
router.post('/api/subject/member-bind/confirm', apiAuth, controller.auth.memberBind.confirm);
// 首页统计(只统计当前主体的数据)
router.get('/api/web/stats', apiAuth, subjectAuth, controller.web.stats.index);
// 本主体设备:列表 / 详情 / 通道 / 解绑 / 删除(越权访问一律 404)
// ⚠️ /devices/active 与 /devices/activate 必须在 /devices/:id 之前
router.get('/api/web/devices', apiAuth, subjectAuth, controller.web.device.index);
router.get('/api/web/devices/active', apiAuth, subjectAuth, controller.web.device.active);
router.post('/api/web/devices/activate', apiAuth, subjectAuth, controller.web.device.activate);
router.get('/api/web/devices/:id', apiAuth, subjectAuth, controller.web.device.show);
router.get('/api/web/devices/:id/channels', apiAuth, subjectAuth, controller.web.device.channels);
router.post('/api/web/devices/:id/unbind', apiAuth, subjectAuth, controller.web.device.unbind);
router.post('/api/web/devices/:id/logout', apiAuth, subjectAuth, controller.web.device.logout);
router.delete('/api/web/devices/:id', apiAuth, subjectAuth, controller.web.device.destroy);
// 本主体扫码记录(范围限定在当前主体的设备)
router.get('/api/web/records', apiAuth, subjectAuth, controller.web.record.index);
// 运单回放查询:运单号 → 当时的工位快照(录像机/摄像头/布局)+ 要拉的回放区间
// 桌面端拿到后按 streams 去各 NVR 拉那段录像(凭据走 /api/web/nvrs/:id/credential)
router.get('/api/web/pack-query', apiAuth, subjectAuth, controller.web.pack.query);
// ---------- 视频接入:NVR / 摄像头 / 工作台(只碰本主体的数据)----------
// 拉取通道 = 前端调本机软件(localhost:17300/api/hik/channels)→ 勾选 → POST /nvrs/:id/channels 落库
router.get('/api/web/nvrs', apiAuth, subjectAuth, controller.web.nvr.index);
router.post('/api/web/nvrs', apiAuth, subjectAuth, controller.web.nvr.create);
router.get('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.show);
router.put('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.update);
router.delete('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.destroy);
router.get('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, controller.web.nvr.cameras);
router.post('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, controller.web.nvr.syncChannels);
router.get('/api/web/nvrs/:id/credential', apiAuth, subjectAuth, controller.web.nvr.credential);
// 摄像头:/options、/batch-delete 必须在 /:id 之前注册,否则会被当成 id
router.get('/api/web/cameras/options', apiAuth, subjectAuth, controller.web.camera.options);
router.post('/api/web/cameras/batch-delete', apiAuth, subjectAuth, controller.web.camera.batchDestroy);
router.get('/api/web/cameras', apiAuth, subjectAuth, controller.web.camera.index);
router.get('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.show);
router.put('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.update);
router.delete('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.destroy);
// 工作台:CRUD + 绑定摄像头 + 绑定扫码通道
router.get('/api/web/workstation-scan-channels', apiAuth, subjectAuth, controller.web.workstation.availableScanChannels);
router.get('/api/web/workstations', apiAuth, subjectAuth, controller.web.workstation.index);
router.post('/api/web/workstations', apiAuth, subjectAuth, controller.web.workstation.create);
router.get('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.show);
router.put('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.update);
router.delete('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.destroy);
router.post('/api/web/workstations/:id/cameras', apiAuth, subjectAuth, controller.web.workstation.bindCameras);
router.delete('/api/web/workstations/:id/cameras/:cameraId', apiAuth, subjectAuth, controller.web.workstation.unbindCamera);
// 合成画面布局(拖拽/缩放后保存)
router.put('/api/web/workstations/:id/cameras/:cameraId/layout', apiAuth, subjectAuth, controller.web.workstation.updateCameraLayout);
router.post('/api/web/workstations/:id/scan-channels', apiAuth, subjectAuth, controller.web.workstation.bindScanChannels);
router.delete('/api/web/workstations/:id/scan-channels/:linkId', apiAuth, subjectAuth, controller.web.workstation.unbindScanChannel);
// 前端「测试页」的模拟接口(模拟设备扫码登录 / 模拟中控上报),与真机走同一套 service
router.post('/api/web/test/device-login', apiAuth, subjectAuth, controller.web.test.deviceLogin);
router.post('/api/web/test/scan', apiAuth, subjectAuth, controller.web.test.scan);
// ---------- 运维/内部(脚本、后台系统;**需内部令牌**,见 middleware/internal_auth.js)----------
router.get('/api/users', internalAuth, controller.internal.user.index);
router.get('/api/users/:id', internalAuth, controller.internal.user.show);
router.post('/api/users', internalAuth, controller.internal.user.create);
router.put('/api/users/:id', internalAuth, controller.internal.user.update);
router.delete('/api/users/:id', internalAuth, controller.internal.user.destroy);
// 微信授权(绑定关系)
router.get('/api/users/:id/wechats', internalAuth, controller.internal.wechat.indexByUser);
router.get('/api/wechat/:appid/:openid', internalAuth, controller.internal.wechat.show);
router.post('/api/wechat/bind', internalAuth, controller.internal.wechat.bind);
router.put('/api/wechat/:id/tokens', internalAuth, controller.internal.wechat.updateTokens);
router.delete('/api/wechat/:id', internalAuth, controller.internal.wechat.unbind);
// 系统配置(读接口带 Redis 缓存,写接口自动失效)
router.get('/api/configs', internalAuth, controller.internal.config.index);
router.put('/api/configs', internalAuth, controller.internal.config.upsert);
router.get('/api/configs/:group', internalAuth, controller.internal.config.showGroup);
router.get('/api/configs/:group/:key', internalAuth, controller.internal.config.show);
router.delete('/api/configs/:group/:key', internalAuth, controller.internal.config.destroy);
// 设备管理(运维视角:可看/可管所有人的设备):绑定 / 解绑 / 列表 / 删除
router.post('/api/scanner/bind', internalAuth, controller.internal.device.bind);
router.post('/api/scanner/unbind', internalAuth, controller.internal.device.unbind);
router.get('/api/scanners', internalAuth, controller.internal.device.index);
router.get('/api/scanners/:id', internalAuth, controller.internal.device.show);
router.delete('/api/scanners/:id', internalAuth, controller.internal.device.destroy);
// ---------- 支付渠道通知(公开,签名见 config.pay)----------
// 渠道接入前用它可以模拟「已付款」:{ orderNo, transactionId, amount } → 续期
router.post('/api/pay/notify', controller.pay.notify.index);
// 运维模拟支付(内部令牌,不需要渠道签名)
router.post('/api/internal/subject-orders/:orderNo/pay', internalAuth, controller.internal.subjectOrder.pay);
// ---------- 公开的登录接口(auth)----------
// 登录码二维码(SVG,本地生成;前端登录页直接 img 引用)
router.get('/api/wechat/qrcode.svg', controller.auth.wechat.qrcode);
// 同一张二维码的 GIF 版(桌面端 WPF 不能渲染 SVG)
router.get('/api/wechat/qrcode.gif', controller.auth.wechat.qrcodeGif);
// 扫码登录(前端/客户端接口):不带参数发串码,带 ?code=&wait= 长轮询并拿用户 JWT
router.get('/api/wechat/login', controller.auth.wechat.login);
// 查登录码状态(无需登录)
router.get('/api/wechat/status', controller.auth.wechat.status);
// 微信公众号网页授权回调(微信把手机浏览器重定向到这里,处理完再转回前端 /wechat 页)
router.get('/api/wechat/callback', controller.auth.wechat.callback);
// 设备发码 + 长轮询:**这一步才注册/绑定**并直接拿到长期 JWT(发码不需要任何凭据)
router.get('/api/device/login', controller.device.auth.login);
// 刷新访问令牌:凭据是刷新令牌本身(访问令牌可能已过期,所以不能挂 scannerAuth)
router.post('/api/device/refresh', controller.device.auth.refresh);
// ---------- 用户态(client):小程序 / Win 客户端 / SPA ----------
// 用登录码触发登录(**不认 cookie**:header 带 Authorization: Bearer <user JWT> 就行)
router.post('/api/wechat/confirm', apiAuth, controller.client.wechat.confirm);
// ---------- 设备态(device):需长期 JWT ----------
router.get('/api/scanner/me', scannerAuth, controller.device.self.me);
router.post('/api/scanner/heartbeat', scannerAuth, controller.device.self.heartbeat);
// 上报(写)额外检查主体是否过期/停用:过期后返回 403 subject_expired,设备暂停推送并靠心跳探测续期
router.post('/api/scan/channels', scannerAuth, subjectActive, controller.device.scan.reportChannels);
router.get('/api/scan/channels', scannerAuth, controller.device.scan.channels);
router.post('/api/scan/records', scannerAuth, subjectActive, controller.device.scan.reportRecords);
router.get('/api/scan/records', scannerAuth, controller.device.scan.records);
};