feat(mini-user): logistics sign photos, dial, ETA and courier callbacks (v3.4.13)
Extend Courier adapter for XFX sign photos and route callbacks; mini-user logistics UI with timeline, phone dial, and estimated arrival. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,9 +6,12 @@ import { WechatPayCallbackController } from './wechat-pay.controller';
|
||||
import { WechatRefundCallbackController } from './wechat-refund.controller';
|
||||
import { WechatMessageCallbackController } from './wechat-message.controller';
|
||||
import { DeliveryCallbackController } from './delivery-track.controller';
|
||||
import { DeliveryCallbackService } from './delivery-callback.service';
|
||||
import { CourierModule } from '../integrations/courier/courier.module';
|
||||
|
||||
@Module({
|
||||
imports: [IntegrationsModule, TradeModule, PrismaModule],
|
||||
imports: [IntegrationsModule, TradeModule, PrismaModule, CourierModule],
|
||||
providers: [DeliveryCallbackService],
|
||||
controllers: [
|
||||
WechatPayCallbackController,
|
||||
WechatRefundCallbackController,
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../common/prisma/prisma.module';
|
||||
import { CourierService } from '../integrations/courier/courier.service';
|
||||
import { XiaofeixiaProvider } from '../integrations/courier/xiaofeixia/xiaofeixia.provider';
|
||||
import { logCourierCall } from '../integrations/courier/courier-log.util';
|
||||
import { TradeService } from '../modules/trade/trade.service';
|
||||
|
||||
const XFX_PROVIDER_ALIASES = new Set(['xfx', 'xiaofeixia']);
|
||||
|
||||
@Injectable()
|
||||
export class DeliveryCallbackService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly courier: CourierService,
|
||||
private readonly xiaofeixiaProvider: XiaofeixiaProvider,
|
||||
private readonly tradeService: TradeService,
|
||||
) {}
|
||||
|
||||
async handleTrackCallback(providerKey: string, body: unknown, requestUrl: string) {
|
||||
const normalized = providerKey.trim().toLowerCase();
|
||||
const baseLog = {
|
||||
scene: 'TRACK_CALLBACK',
|
||||
requestUrl,
|
||||
requestBody: (body && typeof body === 'object' ? body : { value: body }) as Record<
|
||||
string,
|
||||
unknown
|
||||
>,
|
||||
};
|
||||
|
||||
if (!XFX_PROVIDER_ALIASES.has(normalized) && normalized !== 'logistics') {
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
status: 'FAILED',
|
||||
errorMessage: `不支持的承运商: ${providerKey}`,
|
||||
});
|
||||
return this.courier.buildTrackCallbackResponse(false);
|
||||
}
|
||||
|
||||
if (normalized === 'logistics') {
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
status: 'SUCCESS',
|
||||
errorMessage: '跨城物流回调暂未接入,已记录',
|
||||
});
|
||||
return this.courier.buildTrackCallbackResponse(true);
|
||||
}
|
||||
|
||||
const payload = this.xiaofeixiaProvider.parseTrackCallback(body);
|
||||
if (!payload) {
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
status: 'FAILED',
|
||||
errorMessage: '回调体解析失败',
|
||||
});
|
||||
return this.courier.buildTrackCallbackResponse(false);
|
||||
}
|
||||
|
||||
const order = payload.outNumber
|
||||
? await this.prisma.order.findUnique({ where: { orderNo: payload.outNumber } })
|
||||
: payload.trackingNumber
|
||||
? await this.prisma.order.findFirst({
|
||||
where: { delivery: { trackingNo: payload.trackingNumber } },
|
||||
})
|
||||
: null;
|
||||
|
||||
if (!order) {
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
status: 'FAILED',
|
||||
errorMessage: '订单不存在',
|
||||
externalNo: payload.outNumber || payload.trackingNumber,
|
||||
});
|
||||
return this.courier.buildTrackCallbackResponse(false);
|
||||
}
|
||||
|
||||
const targetStatus = this.xiaofeixiaProvider.mapTrackStatus(payload.status);
|
||||
let applied = false;
|
||||
if (targetStatus && targetStatus !== order.status) {
|
||||
await this.tradeService.applyStatusTransition(
|
||||
order.id,
|
||||
order.status,
|
||||
targetStatus,
|
||||
'COURIER_TRACK_CALLBACK',
|
||||
payload.trackInfo || payload.statusName,
|
||||
);
|
||||
applied = true;
|
||||
}
|
||||
|
||||
const response = this.courier.buildTrackCallbackResponse(true);
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
responseBody: response,
|
||||
status: 'SUCCESS',
|
||||
externalNo: order.orderNo,
|
||||
ref: { refType: 'ORDER', refId: order.id },
|
||||
errorMessage: applied ? undefined : '状态未变更',
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -1,71 +1,27 @@
|
||||
import { Body, Controller, Post } from '@nestjs/common';
|
||||
import { TradeService } from '../modules/trade/trade.service';
|
||||
import { CourierService } from '../integrations/courier/courier.service';
|
||||
import { PrismaService } from '../common/prisma/prisma.module';
|
||||
import { logCourierCall } from '../integrations/courier/courier-log.util';
|
||||
import { Body, Controller, Param, Post } from '@nestjs/common';
|
||||
import { DeliveryCallbackService } from './delivery-callback.service';
|
||||
|
||||
@Controller('callbacks/delivery')
|
||||
@Controller('callbacks')
|
||||
export class DeliveryCallbackController {
|
||||
constructor(
|
||||
private readonly tradeService: TradeService,
|
||||
private readonly courier: CourierService,
|
||||
private readonly prisma: PrismaService,
|
||||
) {}
|
||||
constructor(private readonly deliveryCallbackService: DeliveryCallbackService) {}
|
||||
|
||||
@Post('track')
|
||||
async track(@Body() body: { orderNo?: string; orderId?: string; status?: string }) {
|
||||
const baseLog = {
|
||||
scene: 'TRACK_CALLBACK',
|
||||
requestUrl: '/api/v1/callbacks/delivery/track',
|
||||
requestBody: body as Record<string, unknown>,
|
||||
};
|
||||
/** 小飞侠/物流路由变化回调(适配器入口) */
|
||||
@Post('courier/:provider/track')
|
||||
trackByProvider(@Param('provider') provider: string, @Body() body: unknown) {
|
||||
return this.deliveryCallbackService.handleTrackCallback(
|
||||
provider,
|
||||
body,
|
||||
`/api/v1/callbacks/courier/${provider}/track`,
|
||||
);
|
||||
}
|
||||
|
||||
if (!body.orderId && !body.orderNo) {
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
status: 'FAILED',
|
||||
errorMessage: '缺少 orderId / orderNo',
|
||||
});
|
||||
return this.courier.buildTrackCallbackResponse(false);
|
||||
}
|
||||
|
||||
const order = body.orderId
|
||||
? await this.prisma.order.findUnique({ where: { id: BigInt(body.orderId) } })
|
||||
: await this.prisma.order.findUnique({ where: { orderNo: body.orderNo! } });
|
||||
|
||||
if (!order) {
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
status: 'FAILED',
|
||||
errorMessage: '订单不存在',
|
||||
externalNo: body.orderNo,
|
||||
});
|
||||
return this.courier.buildTrackCallbackResponse(false);
|
||||
}
|
||||
|
||||
const statusMap: Record<string, string> = {
|
||||
SHIPPED: 'SHIPPING',
|
||||
OUT_WAREHOUSE: 'OUT_WAREHOUSE',
|
||||
DELIVERED: 'COMPLETED',
|
||||
COMPLETED: 'COMPLETED',
|
||||
};
|
||||
const target = statusMap[body.status ?? ''] ?? body.status;
|
||||
let applied = false;
|
||||
if (target && target !== order.status) {
|
||||
await this.tradeService.applyStatusTransition(order.id, order.status, target, 'DELIVERY_CALLBACK');
|
||||
applied = true;
|
||||
}
|
||||
|
||||
const response = this.courier.buildTrackCallbackResponse(true);
|
||||
await logCourierCall(this.prisma, {
|
||||
...baseLog,
|
||||
responseBody: response,
|
||||
status: 'SUCCESS',
|
||||
externalNo: order.orderNo,
|
||||
ref: { refType: 'ORDER', refId: order.id },
|
||||
errorMessage: applied ? undefined : '状态未变更',
|
||||
});
|
||||
|
||||
return response;
|
||||
/** 兼容旧路径,默认按小飞侠解析 */
|
||||
@Post('delivery/track')
|
||||
trackLegacy(@Body() body: unknown) {
|
||||
return this.deliveryCallbackService.handleTrackCallback(
|
||||
'xfx',
|
||||
body,
|
||||
'/api/v1/callbacks/delivery/track',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user