小飞侠接口
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { CourierConfigService } from '../../integrations/courier/courier.config';
|
||||
import { CourierApiError } from '../../integrations/courier/courier.error';
|
||||
import { CourierService } from '../../integrations/courier/courier.service';
|
||||
import { CourierPayMode } from '../../integrations/courier/courier.types';
|
||||
import type {
|
||||
BatchShipmentQuery,
|
||||
CreateShipmentInput,
|
||||
ShipmentQuery,
|
||||
} from '../../integrations/courier/courier.types';
|
||||
import type {
|
||||
XiaofeixiaBatchShipmentQueryDto,
|
||||
XiaofeixiaCheckCoverageDto,
|
||||
XiaofeixiaCreateShipmentDto,
|
||||
XiaofeixiaEstimateFreightDto,
|
||||
XiaofeixiaShipmentQueryDto,
|
||||
} from './dto/admin-courier.dto';
|
||||
|
||||
function maskSecret(value: string, visible = 4) {
|
||||
if (!value) return '';
|
||||
if (value.length <= visible) return '*'.repeat(value.length);
|
||||
return `${value.slice(0, visible)}${'*'.repeat(Math.min(8, value.length - visible))}`;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AdminXiaofeixiaService {
|
||||
constructor(
|
||||
private readonly courier: CourierService,
|
||||
private readonly courierConfig: CourierConfigService,
|
||||
) {}
|
||||
|
||||
getConfig() {
|
||||
const cfg = this.courierConfig.load();
|
||||
const xfx = cfg.xiaofeixia;
|
||||
return {
|
||||
provider: cfg.provider,
|
||||
activeProvider: this.courier.activeProvider,
|
||||
apiUrl: xfx.apiUrl,
|
||||
appId: xfx.appId ?? null,
|
||||
mchId: xfx.mchId || null,
|
||||
mchIdMasked: xfx.mchId ? maskSecret(xfx.mchId) : null,
|
||||
hasApiKey: Boolean(xfx.apiKey),
|
||||
signType: xfx.signType,
|
||||
ready: Boolean(xfx.mchId && xfx.apiKey),
|
||||
};
|
||||
}
|
||||
|
||||
async estimateFreight(dto: XiaofeixiaEstimateFreightDto) {
|
||||
return this.wrap(() => this.courier.estimateFreight(dto.weight));
|
||||
}
|
||||
|
||||
async checkCoverage(dto: XiaofeixiaCheckCoverageDto) {
|
||||
return this.wrap(() => this.courier.checkDeliveryCoverage(dto.toAddress));
|
||||
}
|
||||
|
||||
async createShipment(dto: XiaofeixiaCreateShipmentDto) {
|
||||
const input = this.mapCreateInput(dto);
|
||||
return this.wrap(() => this.courier.createShipment(input));
|
||||
}
|
||||
|
||||
async cancelShipment(dto: XiaofeixiaShipmentQueryDto) {
|
||||
const query = this.mapShipmentQuery(dto);
|
||||
return this.wrap(async () => {
|
||||
await this.courier.cancelShipment(query);
|
||||
return { cancelled: true };
|
||||
});
|
||||
}
|
||||
|
||||
async getShipment(dto: XiaofeixiaShipmentQueryDto) {
|
||||
const query = this.mapShipmentQuery(dto);
|
||||
return this.wrap(() => this.courier.getShipment(query));
|
||||
}
|
||||
|
||||
async batchGetShipments(dto: XiaofeixiaBatchShipmentQueryDto) {
|
||||
const query: BatchShipmentQuery = {
|
||||
trackingNumbers: dto.trackingNumbers?.filter(Boolean),
|
||||
outNumbers: dto.outNumbers?.filter(Boolean),
|
||||
};
|
||||
return this.wrap(() => this.courier.batchGetShipments(query));
|
||||
}
|
||||
|
||||
async getTrack(dto: XiaofeixiaShipmentQueryDto) {
|
||||
const query = this.mapShipmentQuery(dto);
|
||||
return this.wrap(() => this.courier.getTrack(query));
|
||||
}
|
||||
|
||||
private mapShipmentQuery(dto: XiaofeixiaShipmentQueryDto): ShipmentQuery {
|
||||
if (!dto.trackingNumber && !dto.outNumber) {
|
||||
throw new BadRequestException('运单号与商家单号至少填一个');
|
||||
}
|
||||
return {
|
||||
trackingNumber: dto.trackingNumber,
|
||||
outNumber: dto.outNumber,
|
||||
};
|
||||
}
|
||||
|
||||
private mapCreateInput(dto: XiaofeixiaCreateShipmentDto): CreateShipmentInput {
|
||||
const coord = (lng?: number, lat?: number) =>
|
||||
lng != null && lat != null ? { lng, lat } : undefined;
|
||||
|
||||
return {
|
||||
outNumber: dto.outNumber,
|
||||
customerId: dto.customerId,
|
||||
from: {
|
||||
name: dto.fromName,
|
||||
mobile: dto.fromMobile,
|
||||
address: dto.fromAddress,
|
||||
addressDetail: dto.fromAddressDetail,
|
||||
coordinate: coord(dto.fromLng, dto.fromLat),
|
||||
},
|
||||
to: {
|
||||
name: dto.toName,
|
||||
mobile: dto.toMobile,
|
||||
address: dto.toAddress,
|
||||
addressDetail: dto.toAddressDetail,
|
||||
coordinate: coord(dto.toLng, dto.toLat),
|
||||
},
|
||||
goodsName: dto.goodsName,
|
||||
goodsNum: dto.goodsNum,
|
||||
weight: dto.weight,
|
||||
insuredSumPrice: dto.insuredSumPrice,
|
||||
collectionPrice: dto.collectionPrice,
|
||||
payMode: dto.payMode as CourierPayMode,
|
||||
remark: dto.remark,
|
||||
};
|
||||
}
|
||||
|
||||
private async wrap<T>(fn: () => Promise<T>) {
|
||||
const startedAt = Date.now();
|
||||
try {
|
||||
const data = await fn();
|
||||
return {
|
||||
ok: true,
|
||||
elapsedMs: Date.now() - startedAt,
|
||||
data,
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof CourierApiError) {
|
||||
return {
|
||||
ok: false,
|
||||
elapsedMs: Date.now() - startedAt,
|
||||
error: err.message,
|
||||
code: err.code,
|
||||
provider: err.providerCode,
|
||||
raw: err.raw,
|
||||
};
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user