oms/backend/app/router.js
liufumingren 77da5e1c69 feat(backend): 发货单批量操作与店铺必填/推送校验
- 新增批量审核 audit-batch、批量推送 push-batch、切换发货仓库 change-warehouse、设置所属店铺 change-shop

  统一由 _buildScope 解析范围(scope_by=ids 按勾选 / filter 按筛选条件),支持 dry_run 预览影响范围

- 手工单必须选店铺:create/update 必填且不允许清空、audit 校验、pushOne 无店铺直接跳过;shop_nick 为空时按 shop_id 兜底补齐并回写快照

- 修复范围解析与「目标店铺」字段重名导致命中 0 条:店铺筛选取 from_shop_id,changeShop 前剔除 shop_id

- 契约新增 changeDeliveryWarehouseRequest/changeDeliveryShopRequest/orderDeliveryBatchRequest 等,createOrderDeliveryRequest 的 warehouse_id/shop_id 改为必填
2026-09-13 02:42:23 +08:00

146 lines
14 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);
// 菜单树需在 /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);
// 以下资源均归属某个主体,需做主体校验与数据隔离,防止跨主体操作
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);
// 快递公司字典(通用字典,不区分主体):下拉需在 resources 之前注册,避免 /select 被当作 :id
router.get('/api/express-companies/select', jwtAuth, controller.expressCompany.select);
router.resources('expressCompanies', '/api/express-companies', jwtAuth, controller.expressCompany);
// 收件人信息智能识别(通用能力,仅需登录):需在 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);
// 文件资源: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);
};