forked from yoone/API
1
0
Fork 0
API/src/service/freightcom.service.ts

143 lines
3.6 KiB
TypeScript

import { Provide, Config, Inject, sleep } from '@midwayjs/decorator';
import axios, { AxiosRequestConfig } from 'axios';
import { ShippingDetailsDTO } from '../dto/freightcom.dto';
import { Service } from '../entity/service.entity';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { instanceToPlain } from 'class-transformer';
import { LogisticsService } from './logistics.service';
import { ShipmentType } from '../enums/base.enum';
@Provide()
export class FreightcomService {
@Config('freightcom.url') private apiUrl: string;
@Config('freightcom.token') private token: string;
@Inject() logger;
@InjectEntityModel(Service)
serviceModel: Repository<Service>;
@Inject()
logisticsService: LogisticsService;
async syncServices() {
const config: AxiosRequestConfig = {
method: 'GET',
url: `${this.apiUrl}/services`,
headers: {
Authorization: `${this.token}`,
},
};
const response = await axios.request(config);
const services: Service[] = response.data;
return await this.serviceModel.upsert(services, ['id']);
}
generateCurlCommand(config) {
let curl = `curl -X ${config.method.toUpperCase()}`;
curl += ` '${config.url}'`;
if (config.headers) {
for (const key in config.headers) {
curl += ` -H '${key}: ${config.headers[key]}'`;
}
}
if (config.data) {
curl += ` --data '${JSON.stringify(config.data)}'`;
}
return curl;
}
// 请求费率估算
async getRateEstimate(shippingDetails: ShippingDetailsDTO) {
const services = await this.logisticsService.getActiveServices();
const response = await axios.request({
url: `${this.apiUrl}/rate`,
method: 'POST',
data: {
services,
details: instanceToPlain(shippingDetails),
},
headers: {
Authorization: `${this.token}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
// 获取预估运费
async getRates(rate_id: string) {
const response = await axios.request({
url: `${this.apiUrl}/rate/${rate_id}`,
method: 'GET',
headers: {
Authorization: `${this.token}`,
},
});
return (
response.data?.rates?.map(v => ({
...v,
type: ShipmentType.FREIGHTCOM,
})) || []
);
}
// 创建运单
async createShipment(data) {
const response = await axios.request({
url: `${this.apiUrl}/shipment`,
method: 'POST',
data,
headers: {
Authorization: `${this.token}`,
},
});
return response.data;
}
// 查询运单详细信息
async getShipment(shipment_id: string) {
let { status, data } = await axios.request({
url: `${this.apiUrl}/shipment/${shipment_id}`,
method: 'GET',
headers: {
Authorization: `${this.token}`,
},
});
if (status === 400) {
throw new Error(data);
}
if (status === 202) {
await sleep(2000);
data = this.getShipment(shipment_id);
}
return data;
}
// 取消发货
async cancelShipment(shipment_id: string) {
const response = await axios.request({
url: `${this.apiUrl}/shipment/${shipment_id}`,
method: 'DELETE',
headers: {
Authorization: `${this.token}`,
},
});
return response.data;
}
//获取支付方式
async getPaymentMethods() {
const response = await axios.request({
url: `${this.apiUrl}/finance/payment-methods`,
method: 'GET',
headers: {
Authorization: `${this.token}`,
},
});
return response.data;
}
}