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

334 lines
9.8 KiB
JavaScript

import { WechatSDK } from 'oak-external-sdk';
import { assert } from 'oak-domain/lib/utils/assert';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import { UploadShipException } from '../types/Exception';
import dayjs from 'dayjs';
export const 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
*/
export 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;
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;
assert(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = 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: dayjs().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 UploadShipException(result?.message);
}
}
if (orderId) {
//订单
}
}
/**
* 获取小程序物流状态
* @param context
* @param deposit
* @param order
* @returns
*/
export async function getShipState(context, deposit, order) {
const application = context.getApplication();
const { type, config } = application;
assert(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = 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) {
}
}
/**刷新shipping的ship的小程序物流状态
* @param ship
* @param context
* @returns
*/
export async function refreshtShipState(ship, context) {
let ship2 = ship;
if (!ship2.iState || (!ship2.deposit$ship && !ship2.shipOrder$ship)) {
const ships = await context.select('ship', {
data: 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;
assert(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = 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 generateNewIdAsync(),
action,
data: {},
filter: {
id: ship2.id,
}
}, {});
}
}
}
}
}