shipClazz/wechatMpShip中syncState、syncPaths方法实现

This commit is contained in:
lxy 2025-03-11 14:37:23 +08:00
parent 1c9d3ad826
commit 5e87132d27
14 changed files with 483 additions and 20 deletions

View File

@ -5,8 +5,8 @@ export default interface ShipClazz<ED extends EntityDict & BaseEntityDict, Conte
available(shipServiceId: string, orderIds: string[], context: Context): Promise<boolean>;
eOrder(shipId: string, context: Context): Promise<string>;
cancelOrder(shipId: string, context: Context): Promise<void>;
syncState(shipId: string): Promise<EntityDict['ship']['OpSchema']['iState']>;
syncPaths(shipId: string): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
syncState(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['iState']>;
syncPaths(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
getPrintInfo(shipId: string): Promise<{
type: 'html';
data: string;

View File

@ -5,4 +5,6 @@ import DebugClazz from './WechatMpShip.debug';
export default class WechatMpShip<ED extends EntityDict & BaseEntityDict, Context extends BackendRuntimeContext<ED>> extends DebugClazz<ED, Context> {
eOrder(shipId: string, context: Context): Promise<string>;
cancelOrder(shipId: string, context: Context): Promise<void>;
syncState(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['iState']>;
syncPaths(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
}

View File

@ -24,10 +24,29 @@ export default class WechatMpShipDebug<ED extends EntityDict & BaseEntityDict, C
waybill_id: NonNullable<ED["ship"]["Schema"]["extraShipId"]>;
order_id: string;
}>;
protected prepareGetWaybillToken(shipId: string, context: Context): Promise<{
openid: string | undefined;
sender_phone: string;
receiver_phone: string;
delivery_id: string;
waybill_id: NonNullable<ED["ship"]["Schema"]["extraShipId"]>;
goods_info: {
detail_list: {
goods_name: string;
goods_img_url: string;
goods_desc?: string | undefined;
}[];
};
}>;
protected prepareGetPath(shipId: string, context: Context): Promise<{
openid: string | undefined;
delivery_id: string;
waybill_id: NonNullable<ED["ship"]["Schema"]["extraShipId"]>;
}>;
eOrder(shipId: string, context: Context): Promise<string>;
cancelOrder(shipId: string, context: Context): Promise<void>;
syncState(shipId: string): Promise<EntityDict["ship"]["OpSchema"]["iState"]>;
syncPaths(extraShipId: string): Promise<EntityDict["ship"]["OpSchema"]["extraPaths"]>;
syncState(shipId: string, context: Context): Promise<EntityDict["ship"]["OpSchema"]["iState"]>;
syncPaths(shipId: string, context: Context): Promise<EntityDict["ship"]["OpSchema"]["extraPaths"]>;
getPrintInfo(shipId: string): Promise<{
type: "html";
data: string;

View File

@ -204,6 +204,89 @@ export default class WechatMpShipDebug {
order_id: shipId,
};
}
async prepareGetWaybillToken(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
from: {
id: 1,
phone: 1,
},
to: {
id: 1,
phone: 1,
},
shipService: {
shipCompanyId: 1,
},
extraShipId: 1,
wechatMpShip: {
applicationId: 1,
},
shipOrder$ship: {
$entity: 'shipOrder',
data: {
id: 1,
orderId: 1,
},
},
},
filter: {
id: shipId,
}
}, { dontCollect: true });
assert(ship);
const { extraShipId, from, to, shipService, shipOrder$ship: shipOrders, wechatMpShip } = ship;
const orderIds = shipOrders.map(ele => ele.orderId);
const { openId } = await this.getReceiverInfo(orderIds, wechatMpShip.applicationId, context);
const extraData = await this.getExtraData(shipId, context);
const detailList = extraData.shop.detail_list;
//todo order_detail_path
return {
openid: openId,
sender_phone: from.phone,
receiver_phone: from.phone,
delivery_id: shipService.shipCompanyId,
waybill_id: extraShipId,
goods_info: {
detail_list: detailList,
},
// trans_id: '',
// order_detail_path: '',
};
}
async prepareGetPath(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
extraShipId: 1,
shipService: {
shipCompanyId: 1,
},
shipOrder$ship: {
$entity: 'shipOrder',
data: {
orderId: 1,
},
},
wechatMpShip: {
applicationId: 1,
},
},
filter: {
id: shipId,
}
}, { dontCollect: true });
assert(ship);
const { extraShipId, shipService, shipOrder$ship: shipOrders, wechatMpShip } = ship;
const orderIds = shipOrders.map(ele => ele.orderId);
const { openId } = await this.getReceiverInfo(orderIds, wechatMpShip.applicationId, context);
return {
openid: openId,
delivery_id: shipService.shipCompanyId,
waybill_id: extraShipId,
};
}
async eOrder(shipId, context) {
const data = await this.prepareOrder(shipId, context);
console.log('mock eOrder to wechatMpShip, data:', data);
@ -213,10 +296,10 @@ export default class WechatMpShipDebug {
const data = await this.prepareCancelOrder(shipId, context);
console.log('mock cancelOrder to wechatMpShip, data:', data);
}
syncState(shipId) {
syncState(shipId, context) {
throw new Error("Method not implemented.");
}
syncPaths(extraShipId) {
syncPaths(shipId, context) {
throw new Error("Method not implemented.");
}
getPrintInfo(shipId) {

View File

@ -11,4 +11,57 @@ export default class WechatMpShip extends DebugClazz {
const data = await this.prepareCancelOrder(shipId, context);
await instance.cancelExpressOrder(data);
}
async syncState(shipId, context) {
const instance = await this.getInstance(context);
const data = await this.prepareGetWaybillToken(shipId, context);
const waybillToken = await instance.getWaybillToken(data);
const result = await instance.getExpressState(waybillToken);
const { errcode, errmsg, waybill_info } = result;
if (errcode !== 0) {
throw new Error(errmsg);
}
else {
/**
* 0 运单不存在或未揽收
* 1 已揽件
* 2 运输中
* 3 派件中
* 4 已签收
* 5 异常
* 6 代签收
*/
const status = waybill_info.status;
let state = undefined;
switch (status) {
case 1:
case 2:
case 3: {
state = 'shipping';
break;
}
case 4:
case 6: {
state = 'received';
break;
}
case 5: {
state = 'unknow';
}
default: {
state = undefined;
}
}
return state;
}
}
async syncPaths(shipId, context) {
const instance = await this.getInstance(context);
const data = await this.prepareGetPath(shipId, context);
const result = await instance.getExpressPath(data);
const paths = result.path_item_list?.map((path) => ({
time: path.action_time,
action: path.action_msg,
}));
return paths;
}
}

View File

@ -5,8 +5,8 @@ export default interface ShipClazz<ED extends EntityDict & BaseEntityDict, Conte
available(shipServiceId: string, orderIds: string[], context: Context): Promise<boolean>;
eOrder(shipId: string, context: Context): Promise<string>;
cancelOrder(shipId: string, context: Context): Promise<void>;
syncState(shipId: string): Promise<EntityDict['ship']['OpSchema']['iState']>;
syncPaths(shipId: string): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
syncState(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['iState']>;
syncPaths(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
getPrintInfo(shipId: string): Promise<{
type: 'html';
data: string;

View File

@ -5,4 +5,6 @@ import DebugClazz from './WechatMpShip.debug';
export default class WechatMpShip<ED extends EntityDict & BaseEntityDict, Context extends BackendRuntimeContext<ED>> extends DebugClazz<ED, Context> {
eOrder(shipId: string, context: Context): Promise<string>;
cancelOrder(shipId: string, context: Context): Promise<void>;
syncState(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['iState']>;
syncPaths(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
}

View File

@ -24,10 +24,29 @@ export default class WechatMpShipDebug<ED extends EntityDict & BaseEntityDict, C
waybill_id: NonNullable<ED["ship"]["Schema"]["extraShipId"]>;
order_id: string;
}>;
protected prepareGetWaybillToken(shipId: string, context: Context): Promise<{
openid: string | undefined;
sender_phone: string;
receiver_phone: string;
delivery_id: string;
waybill_id: NonNullable<ED["ship"]["Schema"]["extraShipId"]>;
goods_info: {
detail_list: {
goods_name: string;
goods_img_url: string;
goods_desc?: string | undefined;
}[];
};
}>;
protected prepareGetPath(shipId: string, context: Context): Promise<{
openid: string | undefined;
delivery_id: string;
waybill_id: NonNullable<ED["ship"]["Schema"]["extraShipId"]>;
}>;
eOrder(shipId: string, context: Context): Promise<string>;
cancelOrder(shipId: string, context: Context): Promise<void>;
syncState(shipId: string): Promise<EntityDict["ship"]["OpSchema"]["iState"]>;
syncPaths(extraShipId: string): Promise<EntityDict["ship"]["OpSchema"]["extraPaths"]>;
syncState(shipId: string, context: Context): Promise<EntityDict["ship"]["OpSchema"]["iState"]>;
syncPaths(shipId: string, context: Context): Promise<EntityDict["ship"]["OpSchema"]["extraPaths"]>;
getPrintInfo(shipId: string): Promise<{
type: "html";
data: string;

View File

@ -207,6 +207,89 @@ class WechatMpShipDebug {
order_id: shipId,
};
}
async prepareGetWaybillToken(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
from: {
id: 1,
phone: 1,
},
to: {
id: 1,
phone: 1,
},
shipService: {
shipCompanyId: 1,
},
extraShipId: 1,
wechatMpShip: {
applicationId: 1,
},
shipOrder$ship: {
$entity: 'shipOrder',
data: {
id: 1,
orderId: 1,
},
},
},
filter: {
id: shipId,
}
}, { dontCollect: true });
(0, assert_1.default)(ship);
const { extraShipId, from, to, shipService, shipOrder$ship: shipOrders, wechatMpShip } = ship;
const orderIds = shipOrders.map(ele => ele.orderId);
const { openId } = await this.getReceiverInfo(orderIds, wechatMpShip.applicationId, context);
const extraData = await this.getExtraData(shipId, context);
const detailList = extraData.shop.detail_list;
//todo order_detail_path
return {
openid: openId,
sender_phone: from.phone,
receiver_phone: from.phone,
delivery_id: shipService.shipCompanyId,
waybill_id: extraShipId,
goods_info: {
detail_list: detailList,
},
// trans_id: '',
// order_detail_path: '',
};
}
async prepareGetPath(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
extraShipId: 1,
shipService: {
shipCompanyId: 1,
},
shipOrder$ship: {
$entity: 'shipOrder',
data: {
orderId: 1,
},
},
wechatMpShip: {
applicationId: 1,
},
},
filter: {
id: shipId,
}
}, { dontCollect: true });
(0, assert_1.default)(ship);
const { extraShipId, shipService, shipOrder$ship: shipOrders, wechatMpShip } = ship;
const orderIds = shipOrders.map(ele => ele.orderId);
const { openId } = await this.getReceiverInfo(orderIds, wechatMpShip.applicationId, context);
return {
openid: openId,
delivery_id: shipService.shipCompanyId,
waybill_id: extraShipId,
};
}
async eOrder(shipId, context) {
const data = await this.prepareOrder(shipId, context);
console.log('mock eOrder to wechatMpShip, data:', data);
@ -216,10 +299,10 @@ class WechatMpShipDebug {
const data = await this.prepareCancelOrder(shipId, context);
console.log('mock cancelOrder to wechatMpShip, data:', data);
}
syncState(shipId) {
syncState(shipId, context) {
throw new Error("Method not implemented.");
}
syncPaths(extraShipId) {
syncPaths(shipId, context) {
throw new Error("Method not implemented.");
}
getPrintInfo(shipId) {

View File

@ -14,5 +14,58 @@ class WechatMpShip extends WechatMpShip_debug_1.default {
const data = await this.prepareCancelOrder(shipId, context);
await instance.cancelExpressOrder(data);
}
async syncState(shipId, context) {
const instance = await this.getInstance(context);
const data = await this.prepareGetWaybillToken(shipId, context);
const waybillToken = await instance.getWaybillToken(data);
const result = await instance.getExpressState(waybillToken);
const { errcode, errmsg, waybill_info } = result;
if (errcode !== 0) {
throw new Error(errmsg);
}
else {
/**
* 0 运单不存在或未揽收
* 1 已揽件
* 2 运输中
* 3 派件中
* 4 已签收
* 5 异常
* 6 代签收
*/
const status = waybill_info.status;
let state = undefined;
switch (status) {
case 1:
case 2:
case 3: {
state = 'shipping';
break;
}
case 4:
case 6: {
state = 'received';
break;
}
case 5: {
state = 'unknow';
}
default: {
state = undefined;
}
}
return state;
}
}
async syncPaths(shipId, context) {
const instance = await this.getInstance(context);
const data = await this.prepareGetPath(shipId, context);
const result = await instance.getExpressPath(data);
const paths = result.path_item_list?.map((path) => ({
time: path.action_time,
action: path.action_msg,
}));
return paths;
}
}
exports.default = WechatMpShip;

View File

@ -13,10 +13,10 @@ export default interface ShipClazz<ED extends EntityDict & BaseEntityDict, Conte
cancelOrder(shipId: string, context: Context): Promise<void>;
// 同步状态
syncState(shipId: string): Promise<EntityDict['ship']['OpSchema']['iState']>;
syncState(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['iState']>;
// 同步轨迹
syncPaths(shipId: string): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
syncPaths(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['extraPaths']>;
// 返回打印数据
getPrintInfo(shipId: string): Promise<{

View File

@ -22,7 +22,6 @@ export const fullShipProjection: EntityDict['ship']['Projection'] = {
wechatMpName: 1,
}
},
serial: 1,
deposit$ship: {
$entity: 'deposit',
data: {

View File

@ -115,11 +115,11 @@ export default class WechatMpShipDebug<ED extends EntityDict & BaseEntityDict, C
parent: {
id: 1,
name: 1,
},
},
},
},
},
to: {
to: {
id: 1,
detail: 1,
name: 1,
@ -243,6 +243,101 @@ export default class WechatMpShipDebug<ED extends EntityDict & BaseEntityDict, C
};
}
protected async prepareGetWaybillToken(shipId: string, context: Context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
from: {
id: 1,
phone: 1,
},
to: {
id: 1,
phone: 1,
},
shipService: {
shipCompanyId: 1,
},
extraShipId: 1,
wechatMpShip: {
applicationId: 1,
},
shipOrder$ship: {
$entity: 'shipOrder',
data: {
id: 1,
orderId: 1,
},
},
},
filter: {
id: shipId,
}
}, { dontCollect: true });
assert(ship);
const { extraShipId, from, to, shipService, shipOrder$ship: shipOrders, wechatMpShip } = ship;
const orderIds = shipOrders!.map(ele => ele.orderId);
const { openId } = await this.getReceiverInfo(orderIds, wechatMpShip!.applicationId!, context);
const extraData = await this.getExtraData(shipId, context);
const detailList = extraData.shop.detail_list! as Array<{
goods_name: string;
goods_img_url: string;
goods_desc?: string;
}>;
//todo order_detail_path
return {
openid: openId,
sender_phone: from!.phone!,
receiver_phone: from!.phone!,
delivery_id: shipService!.shipCompanyId!,
waybill_id: extraShipId!,
goods_info: {
detail_list: detailList,
},
// trans_id: '',
// order_detail_path: '',
};
}
protected async prepareGetPath(shipId: string, context: Context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
extraShipId: 1,
shipService: {
shipCompanyId: 1,
},
shipOrder$ship: {
$entity: 'shipOrder',
data: {
orderId: 1,
},
},
wechatMpShip: {
applicationId: 1,
},
},
filter: {
id: shipId,
}
}, { dontCollect: true });
assert(ship);
const { extraShipId, shipService, shipOrder$ship: shipOrders, wechatMpShip } = ship;
const orderIds = shipOrders!.map(ele => ele.orderId);
const { openId } = await this.getReceiverInfo(orderIds, wechatMpShip!.applicationId!, context);
return {
openid: openId,
delivery_id: shipService!.shipCompanyId!,
waybill_id: extraShipId!,
};
}
async eOrder(shipId: string, context: Context): Promise<string> {
const data = await this.prepareOrder(shipId, context);
@ -253,13 +348,13 @@ export default class WechatMpShipDebug<ED extends EntityDict & BaseEntityDict, C
async cancelOrder(shipId: string, context: Context): Promise<void> {
const data = await this.prepareCancelOrder(shipId, context);
console.log('mock cancelOrder to wechatMpShip, data:', data);
console.log('mock cancelOrder to wechatMpShip, data:', data);
}
syncState(shipId: string): Promise<EntityDict["ship"]["OpSchema"]["iState"]> {
syncState(shipId: string, context: Context): Promise<EntityDict["ship"]["OpSchema"]["iState"]> {
throw new Error("Method not implemented.");
}
syncPaths(extraShipId: string): Promise<EntityDict["ship"]["OpSchema"]["extraPaths"]> {
syncPaths(shipId: string, context: Context): Promise<EntityDict["ship"]["OpSchema"]["extraPaths"]> {
throw new Error("Method not implemented.");
}
getPrintInfo(shipId: string): Promise<{ type: "html"; data: string; }> {

View File

@ -18,4 +18,59 @@ export default class WechatMpShip<ED extends EntityDict & BaseEntityDict, Contex
const data = await this.prepareCancelOrder(shipId, context);
await instance.cancelExpressOrder(data);
}
async syncState(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['iState']> {
const instance = await this.getInstance(context);
const data = await this.prepareGetWaybillToken(shipId, context);
const waybillToken = await instance.getWaybillToken(data);
const result = await instance.getExpressState(waybillToken);
const { errcode, errmsg, waybill_info } = result;
if (errcode !== 0) {
throw new Error(errmsg);
} else {
/**
* 0
* 1
* 2
* 3
* 4
* 5
* 6
*/
const status = waybill_info!.status!;
let state: EntityDict['ship']['OpSchema']['iState'] = undefined;
switch (status) {
case 1:
case 2:
case 3: {
state = 'shipping';
break;
}
case 4:
case 6: {
state = 'received';
break;
}
case 5: {
state = 'unknow';
}
default: {
state = undefined;
}
}
return state;
}
}
async syncPaths(shipId: string, context: Context): Promise<EntityDict['ship']['OpSchema']['extraPaths']> {
const instance = await this.getInstance(context);
const data = await this.prepareGetPath(shipId, context);
const result = await instance.getExpressPath(data);
const paths = result.path_item_list?.map((path) => ({
time: path.action_time,
action: path.action_msg,
}));
return paths;
}
}