348 lines
12 KiB
JavaScript
348 lines
12 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const tslib_1 = require("tslib");
|
|
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
const WechatSDK_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/WechatSDK"));
|
|
/**
|
|
* 微信物流支持的下单服务类型,参见:
|
|
* https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express-old/expressinfo.html
|
|
*/
|
|
const ShipServiceCodeDict = {
|
|
'sf-sftk': 2,
|
|
'sf-sfbk': 3,
|
|
'sf-sfjr': 1,
|
|
'dbl-tkzj': 2,
|
|
'dbl-djkd360': 1,
|
|
'yto-bzkd': 0,
|
|
'yto-yzd': 1,
|
|
'zto-bzkd': 0,
|
|
'sto-bzkd': 1,
|
|
};
|
|
class WechatMpShipDebug {
|
|
wechatMpShipId;
|
|
wechatMpShip;
|
|
getReceiverInfo;
|
|
getExtraData;
|
|
constructor(wechatMpShipId, getReceiverInfo, getExtraData) {
|
|
this.wechatMpShipId = wechatMpShipId;
|
|
this.getReceiverInfo = getReceiverInfo;
|
|
this.getExtraData = getExtraData;
|
|
}
|
|
async getWechatMpShip(context) {
|
|
if (this.wechatMpShip) {
|
|
return this.wechatMpShip;
|
|
}
|
|
const [wechatMpShip] = await context.select('wechatMpShip', {
|
|
data: {
|
|
id: 1,
|
|
applicationId: 1,
|
|
application: {
|
|
id: 1,
|
|
type: 1,
|
|
config: 1,
|
|
}
|
|
},
|
|
filter: {
|
|
id: this.wechatMpShipId,
|
|
}
|
|
}, { dontCollect: true });
|
|
this.wechatMpShip = wechatMpShip;
|
|
return this.wechatMpShip;
|
|
}
|
|
async available(shipServiceId, orderIds, context) {
|
|
if (!ShipServiceCodeDict.hasOwnProperty(shipServiceId)) {
|
|
return false;
|
|
}
|
|
const wechatMpShip = await this.getWechatMpShip(context);
|
|
const { openId, appWxId } = await this.getReceiverInfo(orderIds, wechatMpShip.applicationId, context);
|
|
if (!openId && !appWxId) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
async getInstance(context) {
|
|
const wechatMpShip = await this.getWechatMpShip(context);
|
|
const { config } = wechatMpShip.application;
|
|
(0, assert_1.default)(config.type === 'wechatMp');
|
|
const { appId, appSecret } = config;
|
|
const instance = WechatSDK_1.default.getInstance(appId, 'wechatMp', appSecret);
|
|
return instance;
|
|
}
|
|
async prepareOrder(shipId, context) {
|
|
const [ship] = await context.select('ship', {
|
|
data: {
|
|
id: 1,
|
|
entity: 1,
|
|
entityId: 1,
|
|
from: {
|
|
id: 1,
|
|
detail: 1,
|
|
name: 1,
|
|
phone: 1,
|
|
area: {
|
|
id: 1,
|
|
name: 1,
|
|
parent: {
|
|
id: 1,
|
|
name: 1,
|
|
parent: {
|
|
id: 1,
|
|
name: 1,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
to: {
|
|
id: 1,
|
|
detail: 1,
|
|
name: 1,
|
|
phone: 1,
|
|
area: {
|
|
id: 1,
|
|
name: 1,
|
|
parent: {
|
|
id: 1,
|
|
name: 1,
|
|
parent: {
|
|
id: 1,
|
|
name: 1,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wechatMpShip: {
|
|
id: 1,
|
|
application: {
|
|
id: 1,
|
|
config: 1,
|
|
},
|
|
},
|
|
shipOrder$ship: {
|
|
$entity: 'shipOrder',
|
|
data: {
|
|
id: 1,
|
|
orderId: 1,
|
|
},
|
|
},
|
|
shipService: {
|
|
id: 1,
|
|
name: 1,
|
|
shipCompany: {
|
|
id: 1,
|
|
abbr: 1,
|
|
},
|
|
},
|
|
},
|
|
filter: {
|
|
id: shipId,
|
|
}
|
|
}, { dontCollect: true });
|
|
(0, assert_1.default)(ship);
|
|
const { entity, entityId, shipOrder$ship, wechatMpShip, from, to, shipService } = ship;
|
|
(0, assert_1.default)(entity === 'wechatMpShip' && entityId === this.wechatMpShipId);
|
|
const orderIds = shipOrder$ship.map(ele => ele.orderId);
|
|
const { openId, appWxId } = await this.getReceiverInfo(orderIds, wechatMpShip.applicationId, context);
|
|
(0, assert_1.default)(openId || appWxId);
|
|
const extraData = await this.getExtraData(shipId, context);
|
|
const data = Object.assign({
|
|
order_id: ship.id,
|
|
openid: openId,
|
|
wx_appid: appWxId,
|
|
add_source: openId ? 0 : 2,
|
|
delivery_id: shipService.shipCompanyId,
|
|
service: {
|
|
service_type: ShipServiceCodeDict[shipService.id],
|
|
service_name: shipService.name,
|
|
},
|
|
sender: {
|
|
name: from.name,
|
|
mobile: from.phone,
|
|
country: from.area.parent.parent.name,
|
|
province: from.area.parent.name,
|
|
area: from.area.name,
|
|
address: from.detail,
|
|
},
|
|
receiver: {
|
|
name: to.name,
|
|
mobile: to.phone,
|
|
country: to.area.parent.parent.name,
|
|
province: to.area.parent.name,
|
|
area: to.area.name,
|
|
address: to.detail,
|
|
},
|
|
expect_time: 0,
|
|
}, extraData);
|
|
return data;
|
|
}
|
|
async prepareCancelOrder(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,
|
|
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 prepareGetOrder(shipId, context) {
|
|
const [ship] = await context.select('ship', {
|
|
data: {
|
|
id: 1,
|
|
extraShipId: 1,
|
|
shipService: {
|
|
shipCompanyId: 1,
|
|
},
|
|
shipOrder$ship: {
|
|
$entity: 'shipOrder',
|
|
data: {
|
|
orderId: 1,
|
|
order: {}
|
|
},
|
|
},
|
|
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 {
|
|
order_id: shipId,
|
|
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);
|
|
return 'mockExtraShipId';
|
|
}
|
|
async cancelOrder(shipId, context) {
|
|
const data = await this.prepareCancelOrder(shipId, context);
|
|
console.log('mock cancelOrder to wechatMpShip, data:', data);
|
|
}
|
|
syncState(shipId, context) {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
syncPaths(shipId, context) {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
getPrintInfo(shipId, context) {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
}
|
|
exports.default = WechatMpShipDebug;
|
|
;
|