oms/backend/app/router.js
liufumingren 1847e17ba7 feat(auth): 接入微信扫码登录与微信绑定
- 新增网关客户端与通知验签(sha256(appSecret) 派生 HMAC-SHA256,5 分钟防重放),
  扫码会话与一次性结果存 Redis,openid 仅服务端保存
- 新增 user_wechat_binds 表与迁移:openid 唯一(一个微信只绑一个账号)、
  user_id 唯一(重新绑定覆盖旧微信)
- 新增 /api/auth/wechat/* 接口与 /api/wx/notify 网关回调;登录二维码按 IP 限流,
  未配置网关时相关接口返回 400 提示,不影响账号密码 / 邮箱验证码登录
- 前端:「微信登录」tab(二维码 + 倒计时 + 轮询)与「我的」页微信绑定 / 解绑
- 开发与生产各出一份 WX_GATEWAY_* 配置样板,生产权威副本走 shared-config 覆盖
- 同步补充 swagger 契约与请求日志脱敏字段(bind_code / sign)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-01 23:17:11 +08:00

214 lines
20 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 } = app;
const jwtAuth = app.middleware.jwtAuth();
const principalCheck = app.middleware.principalCheck;
router.get('/', controller.home.index);
// 注册、登录、邮箱验证码接口无需鉴权
router.post('/api/auth/email-code', controller.auth.sendEmailCode);
// 邮箱验证码登录:发送登录验证码 + 校验验证码直接登录(公开接口,无需登录)
router.post('/api/auth/email-login/code', controller.auth.sendLoginCode);
router.post('/api/auth/email-login', controller.auth.loginByEmailCode);
// 忘记密码:发送 4 位重置验证码 + 校验验证码重置密码(公开接口,无需登录)
router.post('/api/auth/reset-password/code', controller.auth.sendResetCode);
router.post('/api/auth/reset-password', controller.auth.resetPasswordByCode);
router.post('/api/auth/register', controller.auth.register);
router.post('/api/auth/login', controller.auth.login);
// 以下接口需要登录:请求头 Authorization: Bearer <token>
router.get('/api/auth/profile', jwtAuth, controller.auth.profile);
// 自助服务:修改当前登录用户自己的资料与密码,所有登录账号均可访问
router.put('/api/auth/profile', jwtAuth, controller.auth.updateProfile);
router.put('/api/auth/password', jwtAuth, controller.auth.changePassword);
// 微信网关(公众号扫码):登录无需鉴权,绑定 / 解绑需登录
router.get('/api/auth/wechat/enabled', controller.wechatAuth.enabled);
router.post('/api/auth/wechat/login/code', controller.wechatAuth.createLoginCode);
router.get('/api/auth/wechat/login/status', controller.wechatAuth.loginStatus);
router.post('/api/auth/wechat/bind/code', jwtAuth, controller.wechatAuth.createBindCode);
router.get('/api/auth/wechat/bind/status', jwtAuth, controller.wechatAuth.bindStatus);
router.get('/api/auth/wechat/bind', jwtAuth, controller.wechatAuth.bindInfo);
router.delete('/api/auth/wechat/bind', jwtAuth, controller.wechatAuth.unbind);
// 微信网关扫码结果通知:由网关服务端回调,按签名校验(不用 JWT)
router.post('/api/wx/notify', controller.wechatAuth.notify);
// 菜单树需在 /api/menus/:id 之前注册,避免 tree 被当作 id 匹配
router.get('/api/menus/tree', jwtAuth, controller.menu.tree);
// 订单状态/外部 WMS 状态回写:按订单所属主体做跨主体隔离
router.put('/api/orders/:id/status', jwtAuth, principalCheck({ model: 'Order' }), controller.order.updateStatus);
router.put('/api/orders/:id/wms-status', jwtAuth, principalCheck({ model: 'Order' }), controller.order.updateWmsStatus);
router.put('/api/users/:id/password', jwtAuth, controller.user.resetPassword);
// RESTful 资源路由:index / show / create / update / destroy(均需鉴权)
router.resources('users', '/api/users', jwtAuth, controller.user);
// 头部主体切换下拉(带缓存),需在 /api/principals/:id 之前注册,避免 options 被当作 id 匹配
router.get('/api/principals/options', jwtAuth, controller.principal.options);
// 管理端主体列表(不缓存,保证分页与筛选实时)
router.resources('principals', '/api/principals', jwtAuth, controller.principal);
// 主体用户关联:需校验主体绑定关系
router.resources('principalUsers', '/api/principal-users', jwtAuth, principalCheck({ model: 'PrincipalUser' }), controller.principalUser);
// 邀请开通主体账号:创建新用户(随机密码)并绑定到指定主体
router.post('/api/principal-users/invite', jwtAuth, principalCheck({ model: 'PrincipalUser' }), controller.principalUser.invite);
// 商品快速搜索(聚合:商品编码/货品编码/条码/商品名称),需在 resources('products') 之前注册避免被 :id 误匹配
router.get('/api/products/search', jwtAuth, principalCheck({ model: 'Product' }), controller.product.search);
// 订单(状态分表):索引表 order_index(订单 ID 全局唯一 + current_table/current_status)
// + 6 张同构状态表(待审核/异常/委外中/配送/已作废/已取消)+ 历史表;页面按状态分视图,状态由业务事务推动
router.get('/api/order-status/list', jwtAuth, principalCheck(), controller.orderStatus.index);
router.get('/api/order-status/filters', jwtAuth, principalCheck(), controller.orderStatus.filters);
// 新建订单:写入索引表(取全局唯一 id)与待审核表;带 items 时为发货单口径创建(明细落 order_status_items)
router.post('/api/order-status', jwtAuth, principalCheck(), controller.orderStatus.create);
// 收件人文本智能识别(与老发货单同一能力)
router.post('/api/order-status/parse-receiver', jwtAuth, controller.orderStatus.parseReceiver);
// 商品明细:查询 / 整体保存(仅待审核、异常可维护)
router.get('/api/order-status/items', jwtAuth, principalCheck(), controller.orderStatus.items);
router.post('/api/order-status/items', jwtAuth, principalCheck(), controller.orderStatus.saveItems);
// 状态流转专用动作需在 /:id 之前注册,避免被误匹配:
// 审核(待审核/异常 → 委外中,含锁定库存)/ 推送外部标记 / 转回待审核 / 归档到历史
router.post('/api/order-status/audit', jwtAuth, principalCheck(), controller.orderStatus.audit);
router.post('/api/order-status/push', jwtAuth, principalCheck(), controller.orderStatus.push);
router.post('/api/order-status/return-to-pending', jwtAuth, principalCheck(), controller.orderStatus.returnToPending);
router.post('/api/order-status/archive', jwtAuth, principalCheck(), controller.orderStatus.archive);
// 侧路流转:驳回(换号+释放库存)/ 取消 / 作废 / 恢复
router.post('/api/order-status/reject', jwtAuth, principalCheck(), controller.orderStatus.reject);
router.post('/api/order-status/cancel', jwtAuth, principalCheck(), controller.orderStatus.cancel);
router.post('/api/order-status/void', jwtAuth, principalCheck(), controller.orderStatus.voidOrder);
router.post('/api/order-status/restore', jwtAuth, principalCheck(), controller.orderStatus.restore);
// 推送仓库(WMS deliveryorder.create):单条 / 批量
router.post('/api/order-status/push-wms', jwtAuth, principalCheck(), controller.orderStatus.pushWmsBatch);
// 批量切换仓库 / 设置所属店铺(同步刷新快照)
router.post('/api/order-status/change-warehouse', jwtAuth, principalCheck(), controller.orderStatus.changeWarehouse);
router.post('/api/order-status/change-shop', jwtAuth, principalCheck(), controller.orderStatus.changeShop);
// 订单详情 / 编辑 / 删除(删除仅限待审核、异常、已取消)/ 推送仓库(单条)
router.get('/api/order-status/:id', jwtAuth, principalCheck(), controller.orderStatus.show);
router.put('/api/order-status/:id', jwtAuth, principalCheck(), controller.orderStatus.update);
router.delete('/api/order-status/:id', jwtAuth, principalCheck(), controller.orderStatus.destroy);
router.post('/api/order-status/:id/push-wms', jwtAuth, principalCheck(), controller.orderStatus.pushWms);
// 以下资源均归属某个主体,需做主体校验与数据隔离,防止跨主体操作
router.resources('products', '/api/products', jwtAuth, principalCheck({ model: 'Product' }), controller.product);
router.resources('comboProducts', '/api/combo-products', jwtAuth, principalCheck({ model: 'ComboProduct' }), controller.comboProduct);
router.resources('menus', '/api/menus', jwtAuth, controller.menu);
router.resources('principalExternalApis', '/api/principal-external-apis', jwtAuth, principalCheck({ model: 'PrincipalExternalApi' }), controller.principalExternalApi);
// 仓库(仅基础信息)与仓库接口配置(一个仓库可有多条接口配置)
router.get('/api/warehouses/select', jwtAuth, principalCheck({ model: 'Warehouse' }), controller.warehouse.select);
// 仓库下的商品(是否已推送到该仓库):需在 resources('warehouses') 之前注册,避免 /products 被当作 :id
router.get('/api/warehouses/:id/products', jwtAuth, principalCheck({ model: 'Warehouse' }), controller.warehouseProduct.index);
// 仓库商品推送(商品资料推送到仓库 WMS):需在 resources('warehouses') 之前注册
router.post('/api/warehouses/:id/products/push', jwtAuth, principalCheck({ model: 'Warehouse' }), controller.warehouseProduct.push);
// 仓库商品 WMS 库存查询(按已推送商品的 WMS itemId 批量查询库存数量):需在 resources('warehouses') 之前注册
router.post('/api/warehouses/:id/products/wms-inventory', jwtAuth, principalCheck({ model: 'Warehouse' }), controller.warehouseProduct.wmsInventory);
// 库存同步:用外部 WMS 库存覆盖系统库存(差异以完成的出入库单平衡)
router.post('/api/warehouses/:id/inventory/sync', jwtAuth, principalCheck({ model: 'Warehouse' }), controller.warehouseProduct.syncInventory);
router.resources('warehouses', '/api/warehouses', jwtAuth, principalCheck({ model: 'Warehouse' }), controller.warehouse);
// 仓库接口配置项:定义(硬编码)与配置项值分开维护,需在 resources('warehouse-apis') 之前注册
router.get('/api/warehouse-api-fields', jwtAuth, controller.warehouseApiField.index);
router.get('/api/warehouse-apis/:id/fields', jwtAuth, principalCheck({ model: 'WarehouseApi' }), controller.warehouseApiField.show);
router.put('/api/warehouse-apis/:id/fields', jwtAuth, principalCheck({ model: 'WarehouseApi' }), controller.warehouseApiField.update);
// 接口连通性测试(index.ping),需在 resources('warehouseApis') 之前注册
router.post('/api/warehouse-apis/:id/test', jwtAuth, principalCheck({ model: 'WarehouseApi' }), controller.warehouseApi.test);
router.resources('warehouseApis', '/api/warehouse-apis', jwtAuth, principalCheck({ model: 'WarehouseApi' }), controller.warehouseApi);
// 对外 API 调用日志(OMS → 外部 WMS 的请求记录),按主体隔离(principal_id 由前端拦截器注入 query)
router.get('/api/outbound-logs', jwtAuth, principalCheck(), controller.apiOutboundLog.index);
// 订单整单解析需在 resources('orders') 之前注册,避免被 /api/orders/:id 误匹配
router.post('/api/orders/:id/parse-items', jwtAuth, principalCheck({ model: 'Order' }), controller.order.parseItems);
router.resources('orders', '/api/orders', jwtAuth, principalCheck({ model: 'Order' }), controller.order);
// 订单明细(订单副表):详情解析需在 resources('orderItems') 之前注册
router.post('/api/order-items/:id/parse', jwtAuth, principalCheck({ model: 'OrderItem' }), controller.orderItem.parse);
router.resources('orderItems', '/api/order-items', jwtAuth, principalCheck({ model: 'OrderItem' }), controller.orderItem);
// 店铺与店铺商品(采集的商品可绑定到系统商品或组合商品)
router.resources('shops', '/api/shops', jwtAuth, principalCheck({ model: 'Shop' }), controller.shop);
router.resources('shopProducts', '/api/shop-products', jwtAuth, principalCheck({ model: 'ShopProduct' }), controller.shopProduct);
router.post('/api/shop-products/:id/bind', jwtAuth, principalCheck({ model: 'ShopProduct' }), controller.shopProduct.bind);
// 出入库单与明细(明细共用 stock_items 表,以 bill_type 区分:1入库 2出库)
// 单据状态由业务事务推动(审核/推送/完成/驳回/作废/取消/恢复),不提供直接修改状态的接口
// 状态流转(按序校验):审核 / 推送 / 完成 / 取消 / 恢复 / 作废,需在 resources 之前注册避免被 :id 误匹配
router.post('/api/inbound-orders/:id/audit', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder.audit);
router.post('/api/inbound-orders/:id/push', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder.push);
router.post('/api/inbound-orders/:id/complete', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder.complete);
router.post('/api/inbound-orders/:id/cancel', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder.cancel);
router.post('/api/inbound-orders/:id/restore', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder.restore);
// 作废:已审核(2) 的单据只能作废(禁止删除),记录作废人/时间/原因
router.post('/api/inbound-orders/:id/void', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder.voidOrder);
// 驳回:已审核(2)/已推送(3) 且未完成,驳回后回到待处理(1);已推送的先走接口取消外部单据
router.post('/api/inbound-orders/:id/reject', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder.reject);
router.post('/api/outbound-orders/:id/audit', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder.audit);
router.post('/api/outbound-orders/:id/push', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder.push);
router.post('/api/outbound-orders/:id/complete', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder.complete);
router.post('/api/outbound-orders/:id/cancel', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder.cancel);
router.post('/api/outbound-orders/:id/restore', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder.restore);
router.post('/api/outbound-orders/:id/void', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder.voidOrder);
// 驳回:已审核(2)/已推送(3) 且未完成,驳回后回到待处理(1);已推送的先走接口取消外部单据
router.post('/api/outbound-orders/:id/reject', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder.reject);
router.resources('inboundOrders', '/api/inbound-orders', jwtAuth, principalCheck({ model: 'InboundOrder' }), controller.inboundOrder);
router.resources('outboundOrders', '/api/outbound-orders', jwtAuth, principalCheck({ model: 'OutboundOrder' }), controller.outboundOrder);
router.resources('stockItems', '/api/stock-items', jwtAuth, principalCheck({ model: 'StockItem' }), controller.stockItem);
// 出入库记录(库存流水):只读列表 + 业务动作选项(下拉),按主体隔离;记录由业务事务写入,不提供增删改接口
router.get('/api/stock-records/biz-types', jwtAuth, principalCheck(), controller.stockRecord.bizTypes);
router.get('/api/stock-records', jwtAuth, principalCheck(), controller.stockRecord.index);
// 仪表盘报表(只读聚合:概览/单据状态/趋势/库存 Top/最近流水),按主体隔离(principal_id 由前端拦截器注入 query)
router.get('/api/dashboard/report', jwtAuth, principalCheck(), controller.dashboard.report);
// 快递公司字典(通用字典,不区分主体):下拉需在 resources 之前注册,避免 /select 被当作 :id
router.get('/api/express-companies/select', jwtAuth, controller.expressCompany.select);
router.resources('expressCompanies', '/api/express-companies', jwtAuth, controller.expressCompany);
// 商品属性字典(分类/品牌/单位,按主体隔离):下拉需在 resources 之前注册,避免 /select 被当作 :id
router.get('/api/product-categories/select', jwtAuth, principalCheck({ model: 'ProductCategory' }), controller.productCategory.select);
router.resources('productCategories', '/api/product-categories', jwtAuth, principalCheck({ model: 'ProductCategory' }), controller.productCategory);
router.get('/api/product-brands/select', jwtAuth, principalCheck({ model: 'ProductBrand' }), controller.productBrand.select);
router.resources('productBrands', '/api/product-brands', jwtAuth, principalCheck({ model: 'ProductBrand' }), controller.productBrand);
router.get('/api/product-units/select', jwtAuth, principalCheck({ model: 'ProductUnit' }), controller.productUnit.select);
router.resources('productUnits', '/api/product-units', jwtAuth, principalCheck({ model: 'ProductUnit' }), controller.productUnit);
// 仓库快递映射(外部传入的自定义快递编码 → OMS 默认快递编码,按仓库维度;供回传解析与「外部仓库」页配置)
router.resources('warehouseExpressMappings', '/api/warehouse-express-mappings', jwtAuth, principalCheck({ model: 'WarehouseExpressMapping' }), controller.warehouseExpressMapping);
// 收件人信息智能识别(通用能力,仅需登录):需在 resources 之前注册,避免被 /:id 误匹配
router.post('/api/order-deliveries/parse-receiver', jwtAuth, controller.orderDelivery.parseReceiver);
// 批量切换发货仓库(按勾选 ids 或筛选条件;dry_run=true 仅预览影响范围)
router.post('/api/order-deliveries/change-warehouse', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.changeWarehouse);
// 批量设置所属店铺(shopNick 取店铺名称;dry_run=true 仅预览影响范围)
router.post('/api/order-deliveries/change-shop', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.changeShop);
// 发货单(订单):字段口径对齐 WMS 发货单(wms_order_delivery)与奇门 deliveryOrder,独立于出库单
// 状态流转专用动作需在 resources 之前注册,避免被 /api/order-deliveries/:id 误匹配
router.post('/api/order-deliveries/audit-batch', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.auditBatch);
router.post('/api/order-deliveries/push-batch', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.pushBatch);
router.post('/api/order-deliveries/:id/audit', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.audit);
router.post('/api/order-deliveries/:id/push', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.push);
router.post('/api/order-deliveries/:id/reject', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.reject);
router.post('/api/order-deliveries/:id/cancel', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.cancel);
router.post('/api/order-deliveries/:id/restore', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.restore);
router.post('/api/order-deliveries/:id/void', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery.voidOrder);
router.resources('orderDeliveries', '/api/order-deliveries', jwtAuth, principalCheck({ model: 'OrderDelivery' }), controller.orderDelivery);
// 发货单明细(子表商品明细,对应奇门 orderLines):明细锁定校验在控制器内按单据状态判断
router.resources('orderDeliveryItems', '/api/order-delivery-items', jwtAuth, principalCheck({ model: 'OrderDeliveryItem' }), controller.orderDeliveryItem);
// 导出任务(全局任务池):提交即返回任务ID,定时任务逐个处理,前端轮询进度,完成后触发下载
// 文件按 年/月/日 分目录存储;列表/提交按主体隔离,详情/下载按任务所属主体鉴权
router.get('/api/export-tasks/types', jwtAuth, controller.exportTask.types);
router.post('/api/export-tasks', jwtAuth, principalCheck(), controller.exportTask.submit);
router.get('/api/export-tasks', jwtAuth, principalCheck(), controller.exportTask.index);
router.get('/api/export-tasks/:id', jwtAuth, principalCheck({ model: 'ExportTask' }), controller.exportTask.progress);
router.get('/api/export-tasks/:id/download', jwtAuth, principalCheck({ model: 'ExportTask' }), controller.exportTask.download);
// 文件资源:check/upload 需登录并按主体隔离;raw 为公开读取(URL 含内容 hash,不可枚举)
// raw 通配路由需在 resources('files') 之前注册,避免被 /api/files/:id 误匹配
router.post('/api/files/check', jwtAuth, principalCheck(), controller.file.check);
router.post('/api/files/upload', jwtAuth, principalCheck(), controller.file.upload);
router.get('/api/files/raw/(.*)', controller.file.raw);
router.resources('files', '/api/files', jwtAuth, principalCheck({ model: 'File' }), controller.file);
// WMS 出站通知回调(公开接口,无需登录):按 app_key 校验仓库接口配置 + MD5 签名校验
router.post('/wms/notify', controller.wmsNotify.handle);
// 对外通用回调接口(公开接口,无需登录):外部系统以 method/app_key/v/timestamp/sign 调用,
// 按 app_key 定位仓库接口配置与仓库后按 method 分发;协议与 WMS /openapi 对齐,已暴露 index.ping
router.post('/openapi', controller.openapi.index);
};