fix(order): 异步链路显式传递仓库上下文,避免 Qimen 无登录用户静默失败

- pendingOrder/orderStorageAllocation: 优先取显式传入的 warehouse_id,缺失时回退 getWarehouseId

- deliveryOrder: buildExpressAllocateRequest 携带 warehouse_id,并补充自动分配开关判定日志
This commit is contained in:
liufumingren 2026-08-22 17:22:46 +08:00
parent 37b9a3d30d
commit f2d1ed0849
3 changed files with 30 additions and 2 deletions

View File

@ -1891,7 +1891,12 @@ class PendingOrderService extends OrderBaseService {
};
// 快照 ctx 数据(setImmediate 回调中 ctx 可能已失效)
const warehouseId = this.getWarehouseId();
// 显式传入的仓库ID优先:Qimen 创建订单等无登录用户上下文的异步链路 ctx.user 为空,
// 调用方必须显式传 warehouseId,否则回传任务会因"缺少仓库上下文"静默失败
const explicitWarehouseId = Number(requestData.warehouse_id ?? requestData.warehouseId ?? 0);
const warehouseId = Number.isFinite(explicitWarehouseId) && explicitWarehouseId > 0
? explicitWarehouseId
: this.getWarehouseId();
const operator = this.getOrderLogOperator();
const logWarehouseId = this.getOrderLogWarehouseId(warehouseId);

View File

@ -1856,6 +1856,8 @@ class DeliveryOrderService extends Service {
return {
allow_storage_location: true,
order_ids: [ Number(context && context.orderId || 0) ],
// 显式携带仓库上下文,避免依赖 ctx.user(Qimen 链路无登录用户)
warehouse_id: Number(context && context.warehouseId || 0),
};
}
@ -2506,6 +2508,20 @@ class DeliveryOrderService extends Service {
const isAutoAllocateExpressEnabled = !isCutoffActive && !needApproval && this.normalizePolicyFlag(policyConfig.auto_allocate_express);
const isAutoAllocateStorageEnabled = !isCutoffActive && !needApproval && this.normalizePolicyFlag(policyConfig.auto_allocate_storage);
// 记录开关判定结果,便于排查自动分配快递/储位未触发的问题
this.ctx.logger.info('[DeliveryOrderService.asyncProcessOrderCreated] 自动流程开关判定', {
warehouseId: context.warehouseId,
orderId: context.orderId,
storeId: context.storeId,
cutoffEnabled,
isCutoffActive,
needApproval,
autoAllocateExpressFlag: policyConfig && policyConfig.auto_allocate_express,
autoAllocateStorageFlag: policyConfig && policyConfig.auto_allocate_storage,
isAutoAllocateExpressEnabled,
isAutoAllocateStorageEnabled,
});
if (isAutoAllocateExpressEnabled) {
await this.runAsyncProcessStep('allocateExpress', async () => {
const request = this.buildExpressAllocateRequest(context);

View File

@ -479,7 +479,14 @@ class OrderStorageAllocationService extends MainService {
const progressCallback = typeof requestData.progress_callback === 'function'
? requestData.progress_callback
: null;
const warehouseId = toNumber(ctx.user?.store_id ?? ctx.user?.warehouse_id, 0);
// 优先使用调用方显式传入的仓库ID(Qimen 等无登录用户上下文的链路 ctx.user 为空)
const warehouseId = toNumber(
requestData.warehouse_id
?? requestData.warehouseId
?? ctx.user?.store_id
?? ctx.user?.warehouse_id,
0
);
const orderIds = normalizeOrderIds(requestData);
const allowStorageLocation = normalizeBooleanFlag(
requestData.allow_storage_location