oak-pay-business/lib/utils/ship.js

341 lines
10 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.refreshtShipState = exports.getShipState = exports.uploadShippingInfo = exports.fullShipProjection = void 0;
const tslib_1 = require("tslib");
const oak_external_sdk_1 = require("oak-external-sdk");
const assert_1 = require("oak-domain/lib/utils/assert");
const uuid_1 = require("oak-domain/lib/utils/uuid");
const Exception_1 = require("../types/Exception");
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
exports.fullShipProjection = {
id: 1,
type: 1,
iState: 1,
shipService: {
id: 1,
name: 1,
shipCompany: {
id: 1,
name: 1,
abbr: 1,
wechatMpName: 1,
}
},
serial: 1,
deposit$ship: {
$entity: 'deposit',
data: {
id: 1,
iState: 1,
accountId: 1,
pay$deposit: {
$entity: 'pay',
data: {
id: 1,
applicationId: 1,
application: {
id: 1,
type: 1,
config: 1,
},
price: 1,
meta: 1,
iState: 1,
entity: 1,
entityId: 1,
}
}
}
},
shipOrder$ship: {
$entity: 'shipOrder',
data: {
id: 1,
orderId: 1,
order: {
id: 1,
iState: 1,
pay$order: {
$entity: 'pay',
data: {
id: 1,
applicationId: 1,
application: {
id: 1,
type: 1,
config: 1,
},
price: 1,
meta: 1,
iState: 1,
entity: 1,
entityId: 1,
}
}
}
}
}
};
/**
* 小程序发货信息录入
* @param shipInfo
* @param context
* @returns
*/
async function uploadShippingInfo(param, context) {
const { depositId, orderId } = param;
if (depositId) {
const [deposit] = await context.select('deposit', {
data: {
id: 1,
iState: 1,
shipId: 1,
ship: {
id: 1,
iState: 1,
type: 1,
},
pay$deposit: {
$entity: 'pay',
data: {
id: 1,
entity: 1,
entityId: 1,
meta: 1,
applicationId: 1,
},
filter: {
iState: 'paid',
},
indexFrom: 0,
count: 1,
},
},
filter: {
id: depositId,
}
}, {
blockTrigger: true,
forUpdate: true,
});
const { pay$deposit: pays, } = deposit || {};
const pay = pays?.[0];
const applicationId = pay?.applicationId;
(0, assert_1.assert)(applicationId);
const [application] = await context.select('application', {
data: {
id: 1,
type: 1,
config: 1.
},
filter: {
id: applicationId,
}
}, {
blockTrigger: true,
forUpdate: true,
});
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
const meta = pay?.meta;
const shipInfo = {
order_key: {
order_number_type: 2,
transaction_id: meta?.transaction_id,
},
logistic_type: 3,
delivery_mode: 1,
shipping_list: [
{
item_desc: '账户充值',
}
],
upload_time: (0, dayjs_1.default)().format(),
payer: {
openid: meta?.payer?.openid,
},
};
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode === 0) {
return true;
}
else {
console.error(JSON.stringify(result));
throw new Exception_1.UploadShipException(result?.message);
}
}
if (orderId) {
//订单
}
}
exports.uploadShippingInfo = uploadShippingInfo;
/**
* 获取小程序物流状态
* @param context
* @param deposit
* @param order
* @returns
*/
async function getShipState(context, deposit, order) {
const application = context.getApplication();
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
if (deposit) {
//充值
let deposit2 = deposit;
const deposits = await context.select('deposit', {
data: {
id: 1,
iState: 1,
pay$deposit: {
$entity: 'pay',
data: {
id: 1,
iState: 1,
meta: 1,
entity: 1,
entityId: 1,
},
filter: {
iState: 'paid',
},
indexFrom: 0,
count: 1,
},
shipId: 1,
ship: {
id: 1,
type: 1,
iState: 1,
}
},
filter: {
id: deposit?.id,
}
}, {
blockTrigger: true,
forUpdate: true,
});
deposit2 = deposits[0];
const { pay$deposit: pays, ship, shipId } = deposit2;
const pay = pays?.[0];
if (shipId && pay) {
const info = {
transaction_id: pay?.meta?.transaction_id
};
const result = await wechatInstance.getOrderState(info);
const { orderState, inComplaint } = result;
if (!inComplaint) {
//未处于纠纷中
let state = undefined;
// let action = undefined;
switch (orderState) {
case 1: //待发货
state = 'unshipped';
break;
case 2: //已发货
state = 'shipping';
// action = 'ship';
break;
case 3: //确认收货
state = 'received';
// action = 'receive';
break;
}
// if (action && ship?.iState !== state) {
// await context.operate('ship', {
// id: await generateNewIdAsync(),
// action,
// data: {},
// filter: {
// id: shipId,
// }
// }, {});
// }
return state;
}
}
}
if (order) {
}
}
exports.getShipState = getShipState;
/**刷新shipping的ship的小程序物流状态
* @param ship
* @param context
* @returns
*/
async function refreshtShipState(ship, context) {
let ship2 = ship;
if (!ship2.iState || (!ship2.deposit$ship && !ship2.shipOrder$ship)) {
const ships = await context.select('ship', {
data: exports.fullShipProjection,
filter: {
id: ship.id,
}
}, {
blockTrigger: true,
forUpdate: true,
});
ship2 = ships[0];
}
let application = undefined, info = undefined;
const { deposit$ship: deposits, shipOrder$ship: shipOrders, } = ship2;
if (deposits && deposits.length > 0) {
//充值
const deposit = deposits[0];
const { pay$deposit } = deposit;
application = pay$deposit?.[0].application;
const meta = pay$deposit?.[0].meta;
info = {
transaction_id: meta?.transaction_id
};
}
else if (shipOrders && shipOrders.length > 0) {
//订单
}
if (application) {
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
if (info) {
const result = await wechatInstance.getOrderState(info);
const { orderState, inComplaint } = result;
if (!inComplaint) {
//未处于纠纷中
let state = undefined;
let action = undefined;
switch (orderState) {
case 1: //待发货
state = 'unshipped';
break;
case 2: //已发货
state = 'shipping';
action = 'ship';
break;
case 3: //确认收货
state = 'received';
action = 'receive';
break;
}
if (action && ship2.iState !== state) {
return await context.operate('ship', {
id: await (0, uuid_1.generateNewIdAsync)(),
action,
data: {},
filter: {
id: ship2.id,
}
}, {});
}
}
}
}
}
exports.refreshtShipState = refreshtShipState;