packServer/backend/app/router.js
liufumingren 81a5a8888d 打包:开发/生产环境配置 + 版本号 + 自动更新(version.json)+ 保留5个包
一、打包配置(desktop/installer/package.config.json)
- 两个环境各自的域名:Development = http://127.0.0.1:7300,Production = 占位正式域名(发布前替换)。
- keepVersions = 5。
- build.ps1 -Environment Production 自动用生产域名打 exe 与下载地址;-ServerBaseUrl 可临时覆盖。

二、build.ps1
- 新增 -Environment / -Version / -Keep / -ReleaseNotes;版本默认取 csproj,可覆盖,
  通过 -p:Version 注入程序集(AssemblyVersion/FileVersion 一起变)。
- 打完后:算 MD5、写 version.json(version/downloadUrl/md5/size/publishedAt/buildId/environment/releaseNotes),
  复制 MSI → backend/app/public/download/,version.json → backend/app/public/version/。
- 本地 out/ 与后端目录各只保留最近 5 个安装包(按版本号排序清理)。
- 补丁版保护:后端已有更高版本时,不覆盖 version.json(避免补打旧版把版本降回去)。

三、后端静态分发 + 缓存(关键)
- Egg 的 static 生产默认强缓存一年,version.json 若走静态会**永远检测不到更新**。
  且实测 koa-static 命中文件后**不再 next()**,后置中间件改不了它的响应头(先写了 middleware 验证不可行,已删)。
  → version.json 放到 app/public/version/(不在静态目录),由路由 GET /download/version.json 提供:
    Cache-Control: no-store, no-cache, must-revalidate, max-age=0 + Pragma/Expires,且不产生 ETag/Last-Modified。
- MSI 仍走 egg-static(prefix /download/,maxAge 31536000),文件名带版本号可长缓存。
- middleware/frontend.js 放行 /download/*,否则会被 SPA 入口接管。
- 实测响应头:version.json 无 ETag/Last-Modified、cache-control: no-store…;
  MSI cache-control: public, max-age=31536000。

四、桌面端自动更新(Services/UpdateChecker.cs + App)
- 启动登录后后台请求 {后端}/download/version.json?t=毫秒时间戳(带 no-cache 头),
  比较程序集版本;有新版则弹窗(版本/说明/大小),点是 → 浏览器打开 downloadUrl。
- 失败静默(网络问题不打扰);日志记录发现新版本/已打开下载。
- 实测:安装 1.0.1 → version.json 1.0.1 不提示;改成 1.0.2 后重启 →
  日志「发现新版本 1.0.2(当前 1.0.1.0)→ 已打开下载」。

五、.gitignore
- 忽略后端分发产物:backend/app/public/{download,version}/* 但保留 .gitkeep。

⚠️ backend/config/config.default.js 本次提交同时带入了该文件里已有的未提交改动
   (后端默认端口 7001 → 7300 及注释),因为 static 配置必须写在同一个文件里;
   其它 backend/extension/frontend 的未提交 WIP 未包含在本次提交中。
2026-10-17 12:07:13 +08:00

111 lines
6.8 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 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);
// ---------- 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);
// 首页统计(只统计当前用户名下的数据)
router.get('/api/web/stats', apiAuth, controller.web.stats.index);
// 我的设备:列表 / 详情 / 通道 / 解绑 / 删除(越权访问一律 404)
router.get('/api/web/devices', apiAuth, controller.web.device.index);
router.get('/api/web/devices/:id', apiAuth, controller.web.device.show);
router.get('/api/web/devices/:id/channels', apiAuth, controller.web.device.channels);
router.post('/api/web/devices/:id/unbind', apiAuth, controller.web.device.unbind);
router.delete('/api/web/devices/:id', apiAuth, controller.web.device.destroy);
// 我的扫码记录(范围限定在当前用户名下的设备)
router.get('/api/web/records', apiAuth, controller.web.record.index);
// 前端「测试页」的模拟接口(模拟设备扫码登录 / 模拟中控上报),与真机走同一套 service
router.post('/api/web/test/device-login', apiAuth, controller.web.test.deviceLogin);
router.post('/api/web/test/scan', apiAuth, 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);
// ---------- 公开的登录接口(auth)----------
// 登录码二维码(SVG,本地生成;前端登录页直接 img 引用)
router.get('/api/wechat/qrcode.svg', controller.auth.wechat.qrcode);
// 扫码登录(前端/客户端接口):不带参数发串码,带 ?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);
// ---------- 用户态(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/scan/channels', scannerAuth, controller.device.scan.reportChannels);
router.get('/api/scan/channels', scannerAuth, controller.device.scan.channels);
router.post('/api/scan/records', scannerAuth, controller.device.scan.reportRecords);
router.get('/api/scan/records', scannerAuth, controller.device.scan.records);
};