packServer/backend/app/router.js
liufumingren bd2363bf00 chore: 初始化仓库(backend / frontend / extension 三块)
- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权;
  已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF),
  开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。
  单测 91 个通过(不需要本机数据库)。
- frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 /
  首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。
- extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。
- 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、
  .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
2026-10-17 08:50:32 +08:00

106 lines
6.4 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);
// ---------- 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);
};