Egg 的 BaseContextClass 构造时会把 `this.config = app.config` 挂到实例上, 控制器里叫 `config()` 的动作会被这个实例属性覆盖:路由能匹配上,但 egg-router 拿到的不是函数,最终静默返回 404「接口不存在」。 - controller/web/extension.js:动作改名为 show() - router.js:/api/web/extension/config 指向 controller.web.extension.show - 插件:同步周期文案改为「每 5 分钟」,版本 1.1.1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
265 lines
21 KiB
JavaScript
265 lines
21 KiB
JavaScript
'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 subjectAdmin = middleware.subjectAdmin();
|
||
const subjectActive = middleware.subjectActive();
|
||
const internalAuth = middleware.internalAuth();
|
||
const scannerAuth = middleware.scannerAuth();
|
||
const platformAdmin = middleware.platformAdmin();
|
||
|
||
// ==================== 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);
|
||
|
||
// ---------- 安装包分发(软件自动更新) ----------
|
||
// 安装包本体由 egg-static 提供(config.static):
|
||
// 桌面端 `GET /download/desktop/YuncangTraceSetup-x.y.z.msi`
|
||
// Android `GET /download/android_client/YuncangScan-x.y.z.apk`
|
||
// version.json **必须走路由**:static 命中后不会 next(),改不了缓存头(会被缓存一年)。
|
||
router.get('/download/version.json', controller.download.version);
|
||
router.get('/download/android_client/version.json', controller.download.androidVersion);
|
||
|
||
// ---------- 协议(公开读取;同意记录在登录时落库) ----------
|
||
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);
|
||
|
||
// ---------- 微信登录网关(扫码登录 / 绑定微信;文档 https://gateway.gztbyc.com/#/docs) ----------
|
||
// 网关回调:**公开接口**,靠 `sign` + `timestamp` 鉴权(见 controller/gateway/notify.js)。
|
||
// ⚠️ 网关管理台里的「服务端通知地址」要配成能公网访问的 `<域名>/api/wechat-gateway/notify`。
|
||
router.post('/api/wechat-gateway/notify', controller.gateway.notify.index);
|
||
// 登录页扫码:申请二维码(公开)→ 长轮询结果
|
||
router.post('/api/web/auth/wechat-gateway/qrcode', controller.web.wxGateway.createLoginQr);
|
||
router.get('/api/web/auth/wechat-gateway/login', controller.web.wxGateway.loginStatus);
|
||
// 二维码图片:SVG 给浏览器,GIF 给桌面端(WPF 不能渲染 SVG)
|
||
router.get('/api/wechat-gateway/qrcode.svg', controller.web.wxGateway.qrSvg);
|
||
router.get('/api/wechat-gateway/qrcode.gif', controller.web.wxGateway.qrGif);
|
||
|
||
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);
|
||
|
||
// 个人中心「绑定微信」(走微信登录网关,扫码绑定 / 解绑)
|
||
router.post('/api/web/me/wechat/qrcode', apiAuth, controller.web.wxGateway.createBindQr);
|
||
router.get('/api/web/me/wechat/status', apiAuth, controller.web.wxGateway.bindStatus);
|
||
router.get('/api/web/me/wechat/bindings', apiAuth, controller.web.wxGateway.bindings);
|
||
router.delete('/api/web/me/wechat/bindings/:id', apiAuth, controller.web.wxGateway.unbind);
|
||
|
||
// ---------- 平台管理员(能看所有主体 / 订单;能改授权期限、确认订单、加平台管理员) ----------
|
||
// 鉴权:apiAuth(登录)+ platformAdmin(admin_members 里启用,或 INTERNAL_ADMIN_IDS 内置名单)
|
||
router.get('/api/web/admin/me', apiAuth, platformAdmin, controller.web.adminMember.me);
|
||
router.get('/api/web/admin/members', apiAuth, platformAdmin, controller.web.adminMember.index);
|
||
router.post('/api/web/admin/members', apiAuth, platformAdmin, controller.web.adminMember.create);
|
||
router.delete('/api/web/admin/members/:id', apiAuth, platformAdmin, controller.web.adminMember.destroy);
|
||
router.get('/api/web/admin/subjects', apiAuth, platformAdmin, controller.web.adminSubject.index);
|
||
router.put('/api/web/admin/subjects/:id', apiAuth, platformAdmin, controller.web.adminSubject.update);
|
||
router.get('/api/web/admin/orders', apiAuth, platformAdmin, controller.web.adminOrder.index);
|
||
// 确认订单 = 支付回调的后续动作(同一套 payNotify:标记已支付 + 主体顺延 + 失效缓存)
|
||
router.post('/api/web/admin/orders/:orderNo/confirm', apiAuth, platformAdmin, controller.web.adminOrder.confirm);
|
||
// Chrome 插件的集中配置:只维护「前缀 / 生效站点 / 模板」,单号正则由服务端生成
|
||
router.get('/api/web/admin/extension', apiAuth, platformAdmin, controller.web.adminExtension.show);
|
||
router.put('/api/web/admin/extension', apiAuth, platformAdmin, controller.web.adminExtension.update);
|
||
|
||
// ---------- Chrome 插件读取(公开只读,无令牌) ----------
|
||
// 只下发:前缀 + 生成好的正则 + 生效站点 + 跳转模板 + 安装包地址;不含任何账号/业务数据。
|
||
// 生产若配了 CORS_ORIGINS,记得把 `chrome-extension://<扩展ID>` 加进去(默认 `*` 不用管)。
|
||
router.get('/api/web/extension/config', controller.web.extension.show);
|
||
|
||
// ---------- 主体(租户):不输主体 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.put('/api/web/subjects/:id/members/:memberId/role', apiAuth, controller.web.subject.updateMemberRole);
|
||
// 给未绑定账号/微信的成员出一张扫码绑定二维码(微信登录网关:成员用微信扫码即可绑定)
|
||
router.post('/api/web/subjects/:id/members/:memberId/bind-qr', apiAuth, controller.web.subject.memberBindQr);
|
||
router.get('/api/web/subjects/:id/members/:memberId/bind-qr/status', apiAuth, controller.web.subject.memberBindQrStatus);
|
||
// 解除成员的账号绑定(绑错了 / 手机换人时用)
|
||
router.post('/api/web/subjects/:id/members/:memberId/unbind', apiAuth, controller.web.subject.unbindMember);
|
||
|
||
// 续期订单:选套餐下单不改变期限,支付结果由渠道回调 `POST /api/pay/notify` 驱动
|
||
// ⚠️ 不挂 subjectAuth:主体已过期也要能下单续期
|
||
router.get('/api/web/subject-plans', apiAuth, controller.web.subjectPlan.index);
|
||
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/web/subjects/:id/orders/:orderNo', apiAuth, controller.web.subjectOrder.show);
|
||
router.post('/api/web/subjects/:id/orders/:orderNo/close', apiAuth, controller.web.subjectOrder.close);
|
||
|
||
// 首页统计(只统计当前主体的数据)
|
||
router.get('/api/web/stats', apiAuth, subjectAuth, controller.web.stats.index);
|
||
|
||
// 本主体设备:列表 / 详情 / 通道 / 解绑 / 删除(越权访问一律 404)
|
||
// ⚠️ 业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)**只有主体管理员能看**,
|
||
// 普通成员访问一律 403(subjectAdmin),前端菜单也同步隐藏
|
||
// (例外:回放/下载要用的 `/nvrs/:id/credential` 普通成员也能读,见下面)
|
||
// ⚠️ /devices/active 与 /devices/activate 必须在 /devices/:id 之前
|
||
router.get('/api/web/devices', apiAuth, subjectAuth, subjectAdmin, controller.web.device.index);
|
||
router.get('/api/web/devices/active', apiAuth, subjectAuth, subjectAdmin, controller.web.device.active);
|
||
router.post('/api/web/devices/activate', apiAuth, subjectAuth, subjectAdmin, controller.web.device.activate);
|
||
router.get('/api/web/devices/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.device.show);
|
||
router.get('/api/web/devices/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.device.channels);
|
||
router.post('/api/web/devices/:id/unbind', apiAuth, subjectAuth, subjectAdmin, controller.web.device.unbind);
|
||
router.post('/api/web/devices/:id/logout', apiAuth, subjectAuth, subjectAdmin, controller.web.device.logout);
|
||
router.delete('/api/web/devices/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.device.destroy);
|
||
|
||
// 本主体扫码记录(范围限定在当前主体的设备;仅主体管理员)
|
||
router.get('/api/web/records', apiAuth, subjectAuth, subjectAdmin, controller.web.record.index);
|
||
|
||
// 运单回放查询:运单号 → 当时的工位快照(录像机/摄像头/布局)+ 要拉的回放区间
|
||
// 桌面端拿到后按 streams 去各 NVR 拉那段录像(凭据走 /api/web/nvrs/:id/credential)
|
||
// ⚠️ 这个不挂 subjectAdmin:它是打包工位的日常查询入口,普通成员也要能用
|
||
router.get('/api/web/pack-query', apiAuth, subjectAuth, controller.web.pack.query);
|
||
|
||
// 边缘能力:查询机在局域网找不到「设备管理服务」时,来这里问云端
|
||
// (云端转给打包仓库那台扫码服务,由它真拉一段确认有没有录像)
|
||
router.post('/api/web/edge/check', apiAuth, subjectAuth, controller.web.edge.check);
|
||
// 跨网下载:创建任务 → 查进度 → 取成片(支持 Range)
|
||
router.post('/api/web/edge/download', apiAuth, subjectAuth, controller.web.edge.download);
|
||
router.get('/api/web/edge/download/:taskId', apiAuth, subjectAuth, controller.web.edge.downloadStatus);
|
||
router.get('/api/web/edge/download/:taskId/file', apiAuth, subjectAuth, controller.web.edge.downloadFile);
|
||
|
||
// 回放/下载要录像机登录凭据:**普通成员也要能取**(只是不能取别的主体的)。
|
||
// 按运单回放/下载 = 打包现场日常动作,不该只有主体管理员能做;
|
||
// 范围由 `nvr.credentialOf(id, subjectId)` 兜住:不属于当前主体的 NVR 一律 404。
|
||
// ⚠️ 所以这条不能挂 subjectAdmin,其余 /nvrs 管理接口仍然只给管理员。
|
||
router.get('/api/web/nvrs/:id/credential', apiAuth, subjectAuth, controller.web.nvr.credential);
|
||
|
||
// ---------- 视频接入:NVR / 摄像头 / 工作台(只碰本主体的数据;仅主体管理员)----------
|
||
// 拉取通道 = 前端调本机软件(localhost:17300/api/hik/channels)→ 勾选 → POST /nvrs/:id/channels 落库
|
||
router.get('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.index);
|
||
router.post('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.create);
|
||
router.get('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.show);
|
||
router.put('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.update);
|
||
router.delete('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.destroy);
|
||
router.get('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.cameras);
|
||
router.post('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.syncChannels);
|
||
|
||
// 摄像头:/options、/batch-delete 必须在 /:id 之前注册,否则会被当成 id
|
||
router.get('/api/web/cameras/options', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.options);
|
||
router.post('/api/web/cameras/batch-delete', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.batchDestroy);
|
||
router.get('/api/web/cameras', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.index);
|
||
router.get('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.show);
|
||
router.put('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.update);
|
||
router.delete('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.destroy);
|
||
|
||
// 工作台:CRUD + 绑定摄像头 + 绑定扫码通道
|
||
// 扫码登入工作台:二维码按需生成(微信登录网关,一次性),管理员在成员弹窗里显示;
|
||
// 用户手机微信扫码确认后由网关回调落地登录记录(见 controller/gateway/notify.js)。
|
||
router.get('/api/web/workstation-scan-channels', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.availableScanChannels);
|
||
router.get('/api/web/workstations', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.index);
|
||
router.post('/api/web/workstations', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.create);
|
||
router.get('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.show);
|
||
router.put('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.update);
|
||
router.delete('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.destroy);
|
||
// 成员登录记录:二维码扫码登入(网关)+ 当前/历史成员 + 手动下线 + 一键全部下线
|
||
router.post('/api/web/workstations/:id/login-qr', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.loginQr);
|
||
router.get('/api/web/workstations/:id/login-qr/status', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.loginQrStatus);
|
||
router.get('/api/web/workstations/:id/members', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.members);
|
||
router.post('/api/web/workstations/:id/members/offline-all', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.offlineAll);
|
||
router.post('/api/web/workstations/:id/members/:recordId/offline', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.offlineMember);
|
||
router.post('/api/web/workstations/:id/cameras', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.bindCameras);
|
||
router.delete('/api/web/workstations/:id/cameras/:cameraId', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.unbindCamera);
|
||
// 合成画面布局(拖拽/缩放后保存)
|
||
router.put('/api/web/workstations/:id/cameras/:cameraId/layout', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.updateCameraLayout);
|
||
router.put('/api/web/workstations/:id/cameras/:cameraId/label', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.setLabelCamera);
|
||
router.post('/api/web/workstations/:id/scan-channels', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.bindScanChannels);
|
||
router.delete('/api/web/workstations/:id/scan-channels/:linkId', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.unbindScanChannel);
|
||
|
||
// 前端「测试页」的模拟接口(模拟设备扫码登录 / 模拟中控上报),与真机走同一套 service
|
||
// 只在非生产环境注册:生产不暴露模拟链路
|
||
if (app.config.env !== 'prod') {
|
||
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/internal/wechat-bindings', internalAuth, controller.internal.wechat.bindings);
|
||
router.delete('/api/internal/wechat-bindings/:id', internalAuth, controller.internal.wechat.unbindBinding);
|
||
|
||
// 系统配置(读接口带 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)----------
|
||
// 登录页扫码登录已改为「微信登录网关」(见上方 /api/web/auth/wechat-gateway/*),
|
||
// 这里保留的是**设备扫码登录**用到的同一套公开接口(6 位串码 + 手机页确认)。
|
||
// 登录码二维码(SVG,本地生成;手机页面/设备端直接 img 引用)
|
||
router.get('/api/wechat/qrcode.svg', controller.auth.wechat.qrcode);
|
||
// 查登录码状态(无需登录)
|
||
router.get('/api/wechat/status', controller.auth.wechat.status);
|
||
// 设备发码 + 长轮询:**这一步才注册/绑定**并直接拿到长期 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);
|
||
// 边缘任务通道:设备主动出站领任务(跨公司取流用,云端从不反向连设备)
|
||
router.post('/api/scanner/tasks/poll', scannerAuth, controller.device.edge.poll);
|
||
router.post('/api/scanner/tasks/:id/result', scannerAuth, controller.device.edge.result);
|
||
// 成片分片上传(原始字节流,bodyParser 已在 config 里对该路由禁用)
|
||
router.post('/api/scanner/tasks/:id/upload', scannerAuth, controller.device.edge.upload);
|
||
// 上报(写)额外检查主体是否过期/停用:过期后返回 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);
|
||
// 面单摄像头识别配置(读,不写业务数据,所以不挂 subjectActive)
|
||
router.get('/api/scanner/label-cameras', scannerAuth, controller.device.scan.labelCameras);
|
||
};
|