forked from yoone/API
289 lines
7.3 KiB
TypeScript
289 lines
7.3 KiB
TypeScript
import {
|
|
Inject,
|
|
Controller,
|
|
Post,
|
|
Body,
|
|
Get,
|
|
Put,
|
|
Param,
|
|
Del,
|
|
Query,
|
|
} from '@midwayjs/core';
|
|
import { Context } from '@midwayjs/koa';
|
|
import { ApiOkResponse } from '@midwayjs/swagger';
|
|
import {
|
|
BooleanRes,
|
|
RateLitRes,
|
|
ServiceListRes,
|
|
ShippingAddressListRes,
|
|
} from '../dto/reponse.dto';
|
|
import { FreightcomService } from '../service/freightcom.service';
|
|
import { errorResponse, successResponse } from '../utils/response.util';
|
|
import { LogisticsService } from '../service/logistics.service';
|
|
import { ShippingDetailsDTO } from '../dto/freightcom.dto';
|
|
import { ShippingAddress } from '../entity/shipping_address.entity';
|
|
import { QueryServiceDTO, ShipmentBookDTO } from '../dto/logistics.dto';
|
|
import { User } from '../decorator/user.decorator';
|
|
|
|
@Controller('/logistics')
|
|
export class LogisticsController {
|
|
@Inject()
|
|
ctx: Context;
|
|
|
|
@Inject()
|
|
freightcomService: FreightcomService;
|
|
|
|
@Inject()
|
|
logisticsService: LogisticsService;
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Post('/syncServices')
|
|
async syncServices() {
|
|
try {
|
|
await this.freightcomService.syncServices();
|
|
return successResponse(true);
|
|
} catch (error) {
|
|
return errorResponse('同步失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
description: '服务商列表',
|
|
type: ServiceListRes,
|
|
})
|
|
@Get('/getServiceList')
|
|
async getServiceList(
|
|
@Query()
|
|
param: QueryServiceDTO
|
|
) {
|
|
try {
|
|
const data = await this.logisticsService.getServiceList(param);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Post('/toggleActive')
|
|
async toggleActive(@Body() body: { id: string; isActive: boolean }) {
|
|
try {
|
|
await this.logisticsService.toggleServiceActive(body.id, body.isActive);
|
|
return successResponse(true);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: RateLitRes,
|
|
})
|
|
@Post('/getRateList')
|
|
async getRateList(@Body() details: ShippingDetailsDTO) {
|
|
try {
|
|
const rates = await this.logisticsService.getRateList(details);
|
|
return successResponse(rates);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Post('/createShippingAddress')
|
|
async createShippingAddress(@Body() shippingAddress: ShippingAddress) {
|
|
try {
|
|
await this.logisticsService.createShippingAddress(shippingAddress);
|
|
return successResponse(true);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '创建失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Put('/updateShippingAddress/:id')
|
|
async updateShippingAddress(
|
|
@Body() shippingAddress: ShippingAddress,
|
|
@Param('id') id: number
|
|
) {
|
|
try {
|
|
await this.logisticsService.updateShippingAddress(id, shippingAddress);
|
|
return successResponse(true);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '更新失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ShippingAddressListRes,
|
|
})
|
|
@Get('/getShippingAddressList')
|
|
async getShippingAddressList() {
|
|
try {
|
|
const data = await this.logisticsService.getShippingAddressList();
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Del('/delShippingAddress/:id')
|
|
async delShippingAddress(@Param('id') id: number) {
|
|
try {
|
|
const boolen = await this.logisticsService.delShippingAddress(id);
|
|
return successResponse(boolen);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '删除失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Post('/deleteShipment/:id')
|
|
async deleteShipment(@Param('id') id: number) {
|
|
try {
|
|
const res = await this.logisticsService.removeShipment(id);
|
|
return successResponse(res);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '删除运单失败' );
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Post('/createShipment/:orderId')
|
|
async createShipment(
|
|
@Param('orderId') orderId: number,
|
|
@Body() data: ShipmentBookDTO,
|
|
@User() user
|
|
) {
|
|
try {
|
|
const res: any = await this.logisticsService.createShipment(orderId, data, user.id);
|
|
return successResponse(res.data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '创建失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse(
|
|
{type: BooleanRes}
|
|
)
|
|
@Post('/getShipmentFee')
|
|
async getShipmentFee(
|
|
@Body() data: ShipmentBookDTO
|
|
) {
|
|
try {
|
|
const fee = await this.logisticsService.getShipmentFee(data);
|
|
return successResponse(fee);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '创建失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse(
|
|
{type: BooleanRes}
|
|
)
|
|
@Post('/getShipmentLabel/:shipmentId')
|
|
async getShipmentLabel(
|
|
@Param('shipmentId') shipmentId: number
|
|
) {
|
|
try {
|
|
const res = await this.logisticsService.getShipmentLabel(shipmentId);
|
|
|
|
if (res.data.data[0].status === 'Success') {
|
|
return successResponse({ content: res.data.data[0].labelContent });
|
|
} else {
|
|
return errorResponse(res.data.data[0].errors);
|
|
}
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '创建失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Post('/getPaymentMethods')
|
|
async getpaymentmethods() {
|
|
try {
|
|
const data = await this.freightcomService.getPaymentMethods();
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Post('/updateState/:id')
|
|
async updateShipmentState(
|
|
@Param('shipmentId') shipmentId: number
|
|
) {
|
|
try {
|
|
const data = await this.logisticsService.updateShipmentStateById(shipmentId);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '更新运单状态失败')
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Del('/shipment/:id')
|
|
async delShipment(@Param('id') id: number, @User() user) {
|
|
try {
|
|
const data = await this.logisticsService.delShipment(id, user.id);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Post('/getTrackingNumber')
|
|
async getTrackingNumber(@Query('number') number: string) {
|
|
try {
|
|
return successResponse(
|
|
await this.logisticsService.getTrackingNumber(number)
|
|
);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Post('/getListByTrackingId')
|
|
async getListByTrackingId(@Query('shipment_id') shipment_id: number) {
|
|
try {
|
|
return successResponse(
|
|
await this.logisticsService.getListByTrackingId(shipment_id)
|
|
);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
description: '物流列表',
|
|
})
|
|
@Get('/list')
|
|
async getList(
|
|
@Query()
|
|
param: Record<string, any>
|
|
) {
|
|
try {
|
|
const data = await this.logisticsService.getList(param);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取失败');
|
|
}
|
|
}
|
|
}
|