liufumingren 03b42b4874 fix: 外部WMS已打包判断不再依据 status='finished'
- 移除 checkOrderStatus 中 status='finished' 作为已打包的条件
- 已打包仅以打包状态判断:pack_flag=1 / pack_status='packed' / 'fin'
- 避免出库完成但未打包(pack_status=none)的订单被误判拦截
2026-08-31 00:50:32 +08:00

195 lines
8.0 KiB
JavaScript
Raw Permalink 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.

const Service = require('egg').Service;
const axios = require('axios');
// ========== 配置常量 ==========
const CONFIG = {
// 外部 WMS API 地址
API: {
uri: 'http://qimen.wms.gztbyc.com/system', // WMS API
method: 'workbench.business', // query 参数: 业务模块
version: '1.0', // query 参数: 版本号
warehouseId: '1000028', // 仓库ID 常量
},
// 请求配置
REQUEST: {
TIMEOUT: 5000,
HEADERS: {
'Content-Type': 'application/json',
'platform': 'web',
'project': 'all',
},
},
// 订单类型标识
ORDER_TYPE: 4, // 4 = 外部WMS订单
// 打包状态值
PACK_STATUS: 'packed',
};
class NewWms extends Service {
// ========== 扫码查单 ==========
async queryOrder(barcode, WAREHOUSEID) {
try {
// 外部WMS固定使用配置的仓库ID,忽略内部传入的WAREHOUSEID
const warehouseId = CONFIG.API.warehouseId;
const params = {
method: CONFIG.API.method,
v: CONFIG.API.version,
timestamp: Math.round(Date.now() / 1000),
};
const response = await axios.post(CONFIG.API.uri, {
name: 'queryOrder',
data: { barcode, warehouseId },
}, {
params,
headers: CONFIG.REQUEST.HEADERS,
timeout: CONFIG.REQUEST.TIMEOUT,
});
const result = response.data;
// WMS 返回 { flag, code, message, order, express, barcode, batch, ... }
this.ctx.logger.info(`[new_wms] WMS查单原始返回 | barcode=${barcode} | result=`, JSON.stringify(result));
if (result && result.flag === 'success' && result.order) {
return { success: true, data: this._formatOrder(result, warehouseId, barcode) };
}
return { success: false, message: result?.message || 'WMS 返回异常', wmsResult: result };
} catch (err) {
this.ctx.logger.error('[new_wms] 查单失败:', err.message);
return null;
}
}
// ========== 回传打包状态 ==========
async reportPackStatus(orderData) {
try {
const params = {
method: CONFIG.API.method,
v: CONFIG.API.version,
timestamp: Math.round(Date.now() / 1000),
};
const postData = {
name: 'packOrder',
data: {
delivery_order_code: orderData._wms_delivery_order_code || orderData.POSTNUM,
wave_no: orderData.grabnum,
warehouse_id: orderData.WAREHOUSEID,
order_id: orderData.wms_order_ids || '',
workbench_id: orderData.gztid || '',
is_sandan: orderData.isSandan || 0,
wave_total_orders: orderData.wave_total_orders || orderData.dabaos || 0,
package_material_name: orderData.PACKNAME || '',
items_count: orderData.count || 0,
items_total: orderData.GOODSCOUNT || 0,
users: orderData.users || [],
status: CONFIG.PACK_STATUS,
pack_time: Math.round(Date.now() / 1000),
},
};
this.ctx.logger.info('[new_wms] reportPackStatus 传参:', JSON.stringify(postData));
const response = await axios.post(CONFIG.API.uri, postData, {
params,
headers: CONFIG.REQUEST.HEADERS,
timeout: CONFIG.REQUEST.TIMEOUT,
});
return response.data;
} catch (err) {
this.ctx.logger.error('[new_wms] 回传打包状态失败:', err.message);
return null;
}
}
// ========== 结束打包任务 ==========
async finishPack({ workbench_id, warehouse_id }) {
try {
// 外部WMS固定使用配置的仓库ID,忽略内部传入的WAREHOUSEID
const whId = CONFIG.API.warehouseId;
const params = {
method: CONFIG.API.method,
v: CONFIG.API.version,
timestamp: Math.round(Date.now() / 1000),
};
const postData = {
name: 'finishPack',
data: {
workbench_id,
warehouse_id: whId,
finish_time: Math.round(Date.now() / 1000),
},
};
this.ctx.logger.info('[new_wms] finishPack 传参:', JSON.stringify(postData));
const response = await axios.post(CONFIG.API.uri, postData, {
params,
headers: CONFIG.REQUEST.HEADERS,
timeout: CONFIG.REQUEST.TIMEOUT,
});
return response.data;
} catch (err) {
this.ctx.logger.error('[new_wms] 结束打包任务失败:', err.message);
return null;
}
}
// ========== 内部:订单数据格式化为系统统一格式 ==========
_formatOrder(wmsResult, WAREHOUSEID, barcode) {
const order = wmsResult.order || {};
// items 在 WMS 返回的顶层,不在 order 内
const wmsItems = wmsResult.items || [];
const formattedItems = wmsItems.map(item => {
const barcodes = (item.barcodes || []).map(b => b.barcode).join(',');
return [
item.item_code || '',
String(item.plan_qty || 1),
item.item_name || '',
barcodes,
item.packing_requirements || '',
item.image_url || [],
];
});
return {
dabaos: 1,
workDate: new Date().toISOString().slice(0, 10).replace(/-/g, ''),
userId: 0,
types: CONFIG.ORDER_TYPE,
count: order.items_count || 1,
grabnum: order.wave_no || '',
POSTNUM: wmsResult.barcode || barcode,
WAREHOUSEID: order.warehouse_id || WAREHOUSEID,
createtime: Math.round(new Date() / 1000),
workers: '',
GOODSCOUNT: order.items_total || order.items_count || 1,
items: formattedItems,
orders: order.delivery_order_code || '',
wms_order_ids: order.id || '',
PACKNAME: wmsResult.express?.package_material_name || '',
isSandan: wmsResult.isSandan || 0,
wave_total_orders: (wmsResult.batch && wmsResult.batch.total_orders) || 0,
_wms_delivery_order_code: order.delivery_order_code || '',
// 拦截/取消状态
status: order.status || '',
intercept_flag: order.intercept_flag || 0,
intercept_status: order.intercept_status || '',
// 打包状态
pack_flag: order.pack_flag || 0,
pack_status: order.pack_status || '',
};
}
// ========== 内部:判断外部WMS订单是否可打包 ==========
// 返回 { intercepted, packed }
// - intercepted: true 表示订单已取消或被拦截,禁止打包
// - packed: true 表示订单已完成打包,无需重复回传
checkOrderStatus(order) {
const status = (order && order.status) || '';
const intercept_flag = Number((order && order.intercept_flag) || 0);
const pack_flag = Number((order && order.pack_flag) || 0);
const pack_status = (order && order.pack_status) || '';
return {
intercepted: status === 'cancel' || intercept_flag === 1,
// packed: 仅以打包状态判断(pack_status 取值:packed / fin=打包完成)
// 注意:status='finished' 不代表已打包(如 pack_status=none 的出库完成单),不作为已打包依据
packed: pack_flag === 1 || pack_status === 'packed' || pack_status === 'fin',
};
}
}
module.exports = NewWms;