oms/backend/app/router.js
liufumingren fc58f79b42 feat(backend): 新增商品导出任务
- 新增 export_tasks 表与模型(迁移 20260916000000),记录导出类型、状态、进度、
  文件名与错误信息
- app/service/exportTask.js:生成 CSV(exceljs)并落盘到 upload/exports/<主体>/<年>/<月>/<日>
- app/schedule/exportWorker.js 定时执行待处理任务,cleanupExport.js 清理过期文件
- 接口:POST /api/export-tasks 提交、GET /api/export-tasks 列表与进度、
  GET /api/export-tasks/:id/download 下载
- app/utils/exportDefs.js 定义各导出类型的数据源;warehouseProduct / stockRecord 补充
  库存相关数据
- 契约补 exportTask 系列与 apikey 安全定义;.eslintrc 放行相关文件与 apikey 标签
- package-lock 同步
2026-09-14 22:14:23 +08:00

162 lines
16 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);
// 出入库记录(库存流水):只读列表 + 业务动作选项(下拉),按主体隔离;记录由业务事务写入,不提供增删改接口
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);
// 仓库快递映射(外部传入的自定义快递编码 → 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);
};