fix(logistics): 修复物流服务中订单状态和唯一ID的赋值逻辑 #58
|
|
@ -15,20 +15,20 @@ export class SyncTmsJob implements IJob {
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
logisticsService: LogisticsService;
|
logisticsService: LogisticsService;
|
||||||
|
|
||||||
@InjectEntityModel(Shipment)
|
@InjectEntityModel(Shipment)
|
||||||
shipmentModel: Repository<Shipment>
|
shipmentModel: Repository<Shipment>
|
||||||
|
|
||||||
async onTick() {
|
async onTick() {
|
||||||
const shipments:Shipment[] = await this.shipmentModel.findBy({ tracking_provider: 'freightwaves',finished: false });
|
const shipments: Shipment[] = await this.shipmentModel.findBy({ tracking_provider: 'freightwaves', finished: false });
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
shipments.map(async shipment => {
|
shipments.map(async shipment => {
|
||||||
return await this.logisticsService.updateFreightwavesShipmentState(shipment);
|
return await this.logisticsService.updateFreightwavesShipmentState(shipment);
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
this.logger.info(`更新运单状态完毕 ${JSON.stringify(results)}`);
|
this.logger.info(`更新运单状态完毕 ${JSON.stringify(results)}`);
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
onComplete(result: any) {
|
onComplete(result: any) {
|
||||||
|
|
|
||||||
|
|
@ -140,20 +140,20 @@ interface QueryOrderResponseData {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改订单响应数据接口
|
// 修改订单响应数据接口
|
||||||
interface ModifyOrderResponseData extends CreateOrderResponseData {}
|
interface ModifyOrderResponseData extends CreateOrderResponseData { }
|
||||||
|
|
||||||
// 订单退款响应数据接口
|
// 订单退款响应数据接口
|
||||||
interface RefundOrderResponseData {}
|
interface RefundOrderResponseData { }
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
export class FreightwavesService {
|
export class FreightwavesService {
|
||||||
@Inject() logger;
|
@Inject() logger;
|
||||||
|
|
||||||
// 默认配置
|
// 默认配置
|
||||||
private config: FreightwavesConfig = {
|
private config: FreightwavesConfig = {
|
||||||
appSecret: 'gELCHguGmdTLo!zfihfM91hae8G@9Sz23Mh6pHrt',
|
appSecret: 'gELCHguGmdTLo!zfihfM91hae8G@9Sz23Mh6pHrt',
|
||||||
apiBaseUrl: 'http://tms.freightwaves.ca:8901/',
|
apiBaseUrl: 'http://tms.freightwaves.ca:8901/',
|
||||||
partner: '25072621035200000060'
|
partner: '25072621035200000060'
|
||||||
};
|
};
|
||||||
|
|
||||||
// 初始化配置
|
// 初始化配置
|
||||||
|
|
@ -180,19 +180,19 @@ export class FreightwavesService {
|
||||||
};
|
};
|
||||||
|
|
||||||
// 记录请求前的详细信息
|
// 记录请求前的详细信息
|
||||||
console.log(`Sending request to: ${this.config.apiBaseUrl}${url}`,JSON.stringify({
|
console.log(`Sending request to: ${this.config.apiBaseUrl}${url}`, JSON.stringify({
|
||||||
headers,
|
headers,
|
||||||
data
|
data
|
||||||
}))
|
}))
|
||||||
console.log('Request data:', `${this.config.apiBaseUrl}${url}`, data,headers);
|
console.log('Request data:', `${this.config.apiBaseUrl}${url}`, data, headers);
|
||||||
// 发送请求 - 临时禁用SSL证书验证以解决UNABLE_TO_VERIFY_LEAF_SIGNATURE错误
|
// 发送请求 - 临时禁用SSL证书验证以解决UNABLE_TO_VERIFY_LEAF_SIGNATURE错误
|
||||||
const response = await axios.post<FreightwavesResponse<T>>(
|
const response = await axios.post<FreightwavesResponse<T>>(
|
||||||
`${this.config.apiBaseUrl}${url}`,
|
`${this.config.apiBaseUrl}${url}`,
|
||||||
data,
|
data,
|
||||||
{
|
{
|
||||||
headers,
|
headers,
|
||||||
httpsAgent: new (require('https').Agent)({
|
httpsAgent: new (require('https').Agent)({
|
||||||
rejectUnauthorized: false
|
rejectUnauthorized: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
@ -400,12 +400,12 @@ export class FreightwavesService {
|
||||||
// 调用创建订单方法
|
// 调用创建订单方法
|
||||||
this.log('开始测试创建订单...');
|
this.log('开始测试创建订单...');
|
||||||
this.log('测试参数:', testParams);
|
this.log('测试参数:', testParams);
|
||||||
|
|
||||||
// 注意:在实际环境中取消注释以下行来执行真实请求
|
// 注意:在实际环境中取消注释以下行来执行真实请求
|
||||||
const result = await this.createOrder(testParams);
|
const result = await this.createOrder(testParams);
|
||||||
this.log('创建订单成功:', result);
|
this.log('创建订单成功:', result);
|
||||||
|
|
||||||
|
|
||||||
// 返回模拟结果
|
// 返回模拟结果
|
||||||
return {
|
return {
|
||||||
partnerOrderNumber: testParams.partnerOrderNumber,
|
partnerOrderNumber: testParams.partnerOrderNumber,
|
||||||
|
|
@ -485,14 +485,14 @@ export class FreightwavesService {
|
||||||
// 调用费用试算方法
|
// 调用费用试算方法
|
||||||
this.log('开始测试费用试算...');
|
this.log('开始测试费用试算...');
|
||||||
this.log('测试参数:', testParams);
|
this.log('测试参数:', testParams);
|
||||||
|
|
||||||
// 注意:在实际环境中取消注释以下行来执行真实请求
|
// 注意:在实际环境中取消注释以下行来执行真实请求
|
||||||
const result = await this.rateTry(testParams);
|
const result = await this.rateTry(testParams);
|
||||||
this.log('费用试算成功:', result);
|
this.log('费用试算成功:', result);
|
||||||
|
|
||||||
this.log('测试完成:费用试算方法调用成功(模拟)');
|
this.log('测试完成:费用试算方法调用成功(模拟)');
|
||||||
this.log('提示:在实际环境中,取消注释代码中的rateTry调用行来执行真实请求');
|
this.log('提示:在实际环境中,取消注释代码中的rateTry调用行来执行真实请求');
|
||||||
|
|
||||||
// 返回模拟结果
|
// 返回模拟结果
|
||||||
return {
|
return {
|
||||||
shipCompany: 'DHL',
|
shipCompany: 'DHL',
|
||||||
|
|
@ -529,13 +529,13 @@ export class FreightwavesService {
|
||||||
// 调用查询订单方法
|
// 调用查询订单方法
|
||||||
this.log('开始测试查询订单...');
|
this.log('开始测试查询订单...');
|
||||||
this.log('测试参数:', testParams);
|
this.log('测试参数:', testParams);
|
||||||
|
|
||||||
// 注意:在实际环境中取消注释以下行来执行真实请求
|
// 注意:在实际环境中取消注释以下行来执行真实请求
|
||||||
const result = await this.queryOrder(testParams);
|
const result = await this.queryOrder(testParams);
|
||||||
this.log('查询订单成功:', result);
|
this.log('查询订单成功:', result);
|
||||||
|
|
||||||
this.log('测试完成:查询订单方法调用成功(模拟)');
|
this.log('测试完成:查询订单方法调用成功(模拟)');
|
||||||
|
|
||||||
// 返回模拟结果
|
// 返回模拟结果
|
||||||
return {
|
return {
|
||||||
thirdOrderId: 'thirdOrder-123456789',
|
thirdOrderId: 'thirdOrder-123456789',
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ export class LogisticsService {
|
||||||
@Inject()
|
@Inject()
|
||||||
uniExpressService: UniExpressService;
|
uniExpressService: UniExpressService;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
freightwavesService: FreightwavesService;
|
freightwavesService: FreightwavesService;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
|
|
@ -141,7 +141,7 @@ export class LogisticsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//"expressFinish": 0, //是否快递创建完成(1:完成 0:未完成,需要轮询 2:失败)
|
//"expressFinish": 0, //是否快递创建完成(1:完成 0:未完成,需要轮询 2:失败)
|
||||||
async updateFreightwavesShipmentState(shipment: Shipment) {
|
async updateFreightwavesShipmentState(shipment: Shipment) {
|
||||||
try {
|
try {
|
||||||
const data = await this.freightwavesService.queryOrder({ shipOrderId: shipment.order_id.toString() });
|
const data = await this.freightwavesService.queryOrder({ shipOrderId: shipment.order_id.toString() });
|
||||||
|
|
@ -151,9 +151,9 @@ export class LogisticsService {
|
||||||
throw new Error('获取运单状态失败,原因为' + data.expressFailMsg)
|
throw new Error('获取运单状态失败,原因为' + data.expressFailMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.expressFinish === 0) {
|
if (data.expressFinish === 0) {
|
||||||
shipment.state = '203';
|
shipment.state = '203';
|
||||||
shipment.finished = true;
|
shipment.finished = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.shipmentModel.save(shipment);
|
this.shipmentModel.save(shipment);
|
||||||
|
|
@ -372,7 +372,7 @@ export class LogisticsService {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
resShipmentOrder =await this.mepShipment(data,order);
|
resShipmentOrder = await this.mepShipment(data, order);
|
||||||
|
|
||||||
// if (data.shipmentPlatform === 'uniuni') {
|
// if (data.shipmentPlatform === 'uniuni') {
|
||||||
// // 添加运单
|
// // 添加运单
|
||||||
|
|
@ -385,7 +385,7 @@ export class LogisticsService {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// 记录物流信息,并将订单状态转到完成
|
// 记录物流信息,并将订单状态转到完成
|
||||||
if (resShipmentOrder.status === 'SUCCESS'||resShipmentOrder.code === '00000200') {
|
if (resShipmentOrder.status === 'SUCCESS' || resShipmentOrder.code === '00000200') {
|
||||||
order.orderStatus = ErpOrderStatus.COMPLETED;
|
order.orderStatus = ErpOrderStatus.COMPLETED;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('运单生成失败');
|
throw new Error('运单生成失败');
|
||||||
|
|
@ -398,12 +398,18 @@ export class LogisticsService {
|
||||||
const shipmentRepo = manager.getRepository(Shipment);
|
const shipmentRepo = manager.getRepository(Shipment);
|
||||||
const tracking_provider = data.shipmentPlatform; // todo: id未确定,后写进常数
|
const tracking_provider = data.shipmentPlatform; // todo: id未确定,后写进常数
|
||||||
|
|
||||||
let co:any;
|
let co: any;
|
||||||
if (data.shipmentPlatform === 'uniuni') {
|
let unique_id: any;
|
||||||
|
let state: any;
|
||||||
|
if (data.shipmentPlatform === 'uniuni') {
|
||||||
co = resShipmentOrder.data.tno;
|
co = resShipmentOrder.data.tno;
|
||||||
}else{
|
unique_id = resShipmentOrder.data.uni_order_sn;
|
||||||
|
state = resShipmentOrder.data.uni_status_code;
|
||||||
|
} else {
|
||||||
co = resShipmentOrder.data?.shipOrderId;
|
co = resShipmentOrder.data?.shipOrderId;
|
||||||
}
|
unique_id = resShipmentOrder.data?.shipOrderId;
|
||||||
|
state = ErpOrderStatus.COMPLETED;
|
||||||
|
}
|
||||||
// 同步物流信息到woocommerce
|
// 同步物流信息到woocommerce
|
||||||
const site = await this.siteService.get(Number(order.siteId), true);
|
const site = await this.siteService.get(Number(order.siteId), true);
|
||||||
const res = await this.wpService.createFulfillment(site, order.externalOrderId, {
|
const res = await this.wpService.createFulfillment(site, order.externalOrderId, {
|
||||||
|
|
@ -415,10 +421,10 @@ export class LogisticsService {
|
||||||
const shipment = await shipmentRepo.save({
|
const shipment = await shipmentRepo.save({
|
||||||
tracking_provider: tracking_provider,
|
tracking_provider: tracking_provider,
|
||||||
tracking_id: res.data.tracking_id,
|
tracking_id: res.data.tracking_id,
|
||||||
unique_id: resShipmentOrder.data.uni_order_sn,
|
unique_id: unique_id,
|
||||||
stockPointId: String(data.stockPointId), // todo
|
stockPointId: String(data.stockPointId), // todo
|
||||||
state: resShipmentOrder.data.uni_status_code,
|
state: state,
|
||||||
return_tracking_number: resShipmentOrder.data.tno,
|
return_tracking_number: co,
|
||||||
fee: data.details.shipmentFee,
|
fee: data.details.shipmentFee,
|
||||||
order: order
|
order: order
|
||||||
});
|
});
|
||||||
|
|
@ -427,15 +433,15 @@ export class LogisticsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 同步订单状态到woocommerce
|
// 同步订单状态到woocommerce
|
||||||
if(order.source_type!="shopyy"){
|
if (order.source_type != "shopyy") {
|
||||||
if (order.status !== OrderStatus.COMPLETED) {
|
if (order.status !== OrderStatus.COMPLETED) {
|
||||||
await this.wpService.updateOrder(site, order.externalOrderId, {
|
await this.wpService.updateOrder(site, order.externalOrderId, {
|
||||||
status: OrderStatus.COMPLETED,
|
status: OrderStatus.COMPLETED,
|
||||||
});
|
});
|
||||||
order.status = OrderStatus.COMPLETED;
|
order.status = OrderStatus.COMPLETED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
order.orderStatus = ErpOrderStatus.COMPLETED;
|
order.orderStatus = ErpOrderStatus.COMPLETED;
|
||||||
|
|
||||||
await orderRepo.save(order);
|
await orderRepo.save(order);
|
||||||
|
|
@ -687,7 +693,7 @@ export class LogisticsService {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async mepShipment(data: ShipmentBookDTO,order:Order) {
|
async mepShipment(data: ShipmentBookDTO, order: Order) {
|
||||||
try {
|
try {
|
||||||
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId });
|
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId });
|
||||||
let resShipmentOrder;
|
let resShipmentOrder;
|
||||||
|
|
@ -728,9 +734,9 @@ export class LogisticsService {
|
||||||
if (data.shipmentPlatform === 'freightwaves') {
|
if (data.shipmentPlatform === 'freightwaves') {
|
||||||
// 根据TMS系统对接说明文档格式化参数
|
// 根据TMS系统对接说明文档格式化参数
|
||||||
const reqBody: any = {
|
const reqBody: any = {
|
||||||
shipCompany: 'UPSYYZ7000NEW',
|
shipCompany: 'UPSYYZ7000NEW',
|
||||||
partnerOrderNumber: order.externalOrderId ,
|
partnerOrderNumber: order.siteId + '-' + order.externalOrderId,
|
||||||
warehouseId: '25072621030107400060',
|
warehouseId: '25072621030107400060',
|
||||||
shipper: {
|
shipper: {
|
||||||
name: data.details.origin.contact_name, // 姓名
|
name: data.details.origin.contact_name, // 姓名
|
||||||
phone: data.details.origin.phone_number.number, // 电话(提取number属性转换为字符串)
|
phone: data.details.origin.phone_number.number, // 电话(提取number属性转换为字符串)
|
||||||
|
|
@ -772,7 +778,7 @@ export class LogisticsService {
|
||||||
weightUnit: (data.details.packaging_properties.packages[0].measurements.weight.unit === 'kg' ? 'KG' : 'LB') as 'KG' | 'LB' // 重量单位(LB,KG)
|
weightUnit: (data.details.packaging_properties.packages[0].measurements.weight.unit === 'kg' ? 'KG' : 'LB') as 'KG' | 'LB' // 重量单位(LB,KG)
|
||||||
},
|
},
|
||||||
currency: 'CAD', // 币种(默认CAD)
|
currency: 'CAD', // 币种(默认CAD)
|
||||||
description: '订单编号:'+order.externalOrderId // 包裹描述(确保是字符串类型)
|
description: '订单编号:' + order.externalOrderId // 包裹描述(确保是字符串类型)
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
signService: 0
|
signService: 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue