修改小程序发货信息录入

This commit is contained in:
lxy 2025-03-14 14:26:27 +08:00
parent 62021c7721
commit 3c8298e08d
12 changed files with 136 additions and 89 deletions

3
es/utils/ship.d.ts vendored
View File

@ -16,8 +16,7 @@ export declare function uploadShippingInfo(shipId: string, context: BRC): Promis
/**
*
* @param context
* @param deposit
* @param order
* @param shipId
* @returns
*/
export declare function getOrderShipState(context: BRC, shipId: string): Promise<string | undefined>;

View File

@ -146,11 +146,15 @@ export async function uploadShippingInfo(shipId, context) {
//发货信息录入前检查小程序订单发货状态
const shipState = await getOrderShipState(context, shipId);
const now = dayjs().format();
const application = context.getApplication();
const { type, config } = application;
assert(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = WechatSDK.getInstance(appId, 'wechatMp', appSecret);
// const application = context.getApplication();
// const { type, config } = application!;
// assert(type === 'wechatMp');
// const { appId, appSecret } = config as WechatMpConfig;
// const wechatInstance = WechatSDK.getInstance(
// appId,
// 'wechatMp',
// appSecret,
// ) as WechatMpInstance;
if (deposits && deposits.length > 0 && shipState === 'unshipped') {
//充值
assert(deposits.length === 1);
@ -175,10 +179,15 @@ export async function uploadShippingInfo(shipId, context) {
openid: meta?.payer?.openid,
},
};
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
const { type, config } = pay?.application;
if (type === 'wechatMp') {
const { appId, appSecret } = config;
const wechatInstance = WechatSDK.getInstance(appId, 'wechatMp', appSecret);
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
}
}
}
else if (shipOrder$ship && shipOrder$ship.length > 0 && shipState && ['unshipped', 'shipping'].includes(shipState)) {
@ -223,10 +232,15 @@ export async function uploadShippingInfo(shipId, context) {
openid: meta?.payer?.openid,
},
};
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
const { type, config } = wechatPay?.application;
if (type === 'wechatMp') {
const { appId, appSecret } = config;
const wechatInstance = WechatSDK.getInstance(appId, 'wechatMp', appSecret);
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
}
}
}
}
@ -234,8 +248,7 @@ export async function uploadShippingInfo(shipId, context) {
/**
* 获取小程序物流状态
* @param context
* @param deposit
* @param order
* @param shipId
* @returns
*/
export async function getOrderShipState(context, shipId) {
@ -250,18 +263,16 @@ export async function getOrderShipState(context, shipId) {
});
assert(ship);
const { deposit$ship: deposits, shipOrder$ship } = ship;
const application = context.getApplication();
const { type, config } = application;
assert(type === 'wechatMp');
const { appId, appSecret } = config;
const wechatInstance = WechatSDK.getInstance(appId, 'wechatMp', appSecret);
let info = undefined;
let info = undefined, wechatInstance = undefined;
if (deposits && deposits.length > 0) {
//充值
const [deposit] = deposits;
const { pay$deposit: pays, } = deposit;
const pay = pays?.[0];
if (shipId && pay) {
const { config } = pay.application;
const { appId, appSecret } = config;
wechatInstance = WechatSDK.getInstance(appId, 'wechatMp', appSecret);
info = {
transaction_id: pay?.meta?.transaction_id
};
@ -272,12 +283,15 @@ export async function getOrderShipState(context, shipId) {
const { pay$order: pays, } = order;
const pay = pays?.find((ele) => ele.entity === 'wpProduct');
if (shipId && pay) {
const { config } = pay.application;
const { appId, appSecret } = config;
wechatInstance = WechatSDK.getInstance(appId, 'wechatMp', appSecret);
info = {
transaction_id: pay?.meta?.transaction_id
};
}
}
if (info) {
if (info && wechatInstance) {
const result = await wechatInstance.getOrderState(info);
const { orderState, inComplaint } = result;
if (!inComplaint) {

View File

@ -313,8 +313,8 @@ export default class WechatMpShipDebug {
async getPrintInfo(shipId, context) {
const data = await this.prepareGetOrder(shipId, context);
console.log('mock getPrintInfo to wechatMpShip, data:', data);
const mockData = 'PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PjxoMT5IZWxsbywgV29ybGQhPC9oMT48L2JvZHk+PC9odG1sPg==';
const mockHtml = atob(mockData);
const mockData = 'PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PjxoMT5tb2NrIOmdouWNlTwvaDE+PC9ib2R5PjwvaHRtbD4=';
const mockHtml = decodeURIComponent(escape(window.atob(mockData)));
return {
type: 'html',
data: mockHtml

View File

@ -71,7 +71,7 @@ export default class WechatMpShip extends DebugClazz {
const data = await this.prepareGetOrder(shipId, context);
const result = await instance.getExpressOrder(data);
const base64Html = result.print_html;
const html = Buffer.from(base64Html, 'base64').toString('utf8');
const html = decodeURIComponent(escape(window.atob(base64Html)));
return {
type: 'html',
data: html,

3
lib/utils/ship.d.ts vendored
View File

@ -16,8 +16,7 @@ export declare function uploadShippingInfo(shipId: string, context: BRC): Promis
/**
*
* @param context
* @param deposit
* @param order
* @param shipId
* @returns
*/
export declare function getOrderShipState(context: BRC, shipId: string): Promise<string | undefined>;

View File

@ -151,11 +151,15 @@ async function uploadShippingInfo(shipId, context) {
//发货信息录入前检查小程序订单发货状态
const shipState = await getOrderShipState(context, shipId);
const now = (0, dayjs_1.default)().format();
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);
// const application = context.getApplication();
// const { type, config } = application!;
// assert(type === 'wechatMp');
// const { appId, appSecret } = config as WechatMpConfig;
// const wechatInstance = WechatSDK.getInstance(
// appId,
// 'wechatMp',
// appSecret,
// ) as WechatMpInstance;
if (deposits && deposits.length > 0 && shipState === 'unshipped') {
//充值
(0, assert_1.assert)(deposits.length === 1);
@ -180,10 +184,15 @@ async function uploadShippingInfo(shipId, context) {
openid: meta?.payer?.openid,
},
};
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new Exception_1.UploadShipException(result?.message);
const { type, config } = pay?.application;
if (type === 'wechatMp') {
const { appId, appSecret } = config;
const wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new Exception_1.UploadShipException(result?.message);
}
}
}
else if (shipOrder$ship && shipOrder$ship.length > 0 && shipState && ['unshipped', 'shipping'].includes(shipState)) {
@ -228,10 +237,15 @@ async function uploadShippingInfo(shipId, context) {
openid: meta?.payer?.openid,
},
};
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new Exception_1.UploadShipException(result?.message);
const { type, config } = wechatPay?.application;
if (type === 'wechatMp') {
const { appId, appSecret } = config;
const wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new Exception_1.UploadShipException(result?.message);
}
}
}
}
@ -240,8 +254,7 @@ exports.uploadShippingInfo = uploadShippingInfo;
/**
* 获取小程序物流状态
* @param context
* @param deposit
* @param order
* @param shipId
* @returns
*/
async function getOrderShipState(context, shipId) {
@ -256,18 +269,16 @@ async function getOrderShipState(context, shipId) {
});
(0, assert_1.assert)(ship);
const { deposit$ship: deposits, shipOrder$ship } = ship;
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);
let info = undefined;
let info = undefined, wechatInstance = undefined;
if (deposits && deposits.length > 0) {
//充值
const [deposit] = deposits;
const { pay$deposit: pays, } = deposit;
const pay = pays?.[0];
if (shipId && pay) {
const { config } = pay.application;
const { appId, appSecret } = config;
wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
info = {
transaction_id: pay?.meta?.transaction_id
};
@ -278,12 +289,15 @@ async function getOrderShipState(context, shipId) {
const { pay$order: pays, } = order;
const pay = pays?.find((ele) => ele.entity === 'wpProduct');
if (shipId && pay) {
const { config } = pay.application;
const { appId, appSecret } = config;
wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
info = {
transaction_id: pay?.meta?.transaction_id
};
}
}
if (info) {
if (info && wechatInstance) {
const result = await wechatInstance.getOrderState(info);
const { orderState, inComplaint } = result;
if (!inComplaint) {

View File

@ -316,8 +316,8 @@ class WechatMpShipDebug {
async getPrintInfo(shipId, context) {
const data = await this.prepareGetOrder(shipId, context);
console.log('mock getPrintInfo to wechatMpShip, data:', data);
const mockData = 'PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PjxoMT5IZWxsbywgV29ybGQhPC9oMT48L2JvZHk+PC9odG1sPg==';
const mockHtml = atob(mockData);
const mockData = 'PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PjxoMT5tb2NrIOmdouWNlTwvaDE+PC9ib2R5PjwvaHRtbD4=';
const mockHtml = decodeURIComponent(escape(window.atob(mockData)));
return {
type: 'html',
data: mockHtml

View File

@ -74,7 +74,7 @@ class WechatMpShip extends WechatMpShip_debug_1.default {
const data = await this.prepareGetOrder(shipId, context);
const result = await instance.getExpressOrder(data);
const base64Html = result.print_html;
const html = Buffer.from(base64Html, 'base64').toString('utf8');
const html = decodeURIComponent(escape(window.atob(base64Html)));
return {
type: 'html',
data: html,

View File

@ -157,15 +157,15 @@ export async function uploadShippingInfo(
const shipState = await getOrderShipState(context, shipId);
const now = dayjs().format();
const application = context.getApplication();
const { type, config } = application!;
assert(type === 'wechatMp');
const { appId, appSecret } = config as WechatMpConfig;
const wechatInstance = WechatSDK.getInstance(
appId,
'wechatMp',
appSecret,
) as WechatMpInstance;
// const application = context.getApplication();
// const { type, config } = application!;
// assert(type === 'wechatMp');
// const { appId, appSecret } = config as WechatMpConfig;
// const wechatInstance = WechatSDK.getInstance(
// appId,
// 'wechatMp',
// appSecret,
// ) as WechatMpInstance;
if (deposits && deposits.length > 0 && shipState === 'unshipped') {
//充值
@ -196,10 +196,19 @@ export async function uploadShippingInfo(
openid: meta?.payer?.openid,
},
}
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
const { type, config } = pay?.application!;
if (type === 'wechatMp') {
const { appId, appSecret } = config as WechatMpConfig;
const wechatInstance = WechatSDK.getInstance(
appId,
'wechatMp',
appSecret,
) as WechatMpInstance;
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
}
}
} else if (shipOrder$ship && shipOrder$ship.length > 0 && shipState && ['unshipped', 'shipping'].includes(shipState)) {
//订单 每笔微信支付调用一次接口
@ -248,10 +257,19 @@ export async function uploadShippingInfo(
},
}
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
const { type, config } = wechatPay?.application!;
if (type === 'wechatMp') {
const { appId, appSecret } = config as WechatMpConfig;
const wechatInstance = WechatSDK.getInstance(
appId,
'wechatMp',
appSecret,
) as WechatMpInstance;
const result = await wechatInstance.uploadShippingInfo(shipInfo);
if (result?.errcode !== 0) {
console.error(JSON.stringify(result));
throw new UploadShipException(result?.message);
}
}
}
}
@ -260,8 +278,7 @@ export async function uploadShippingInfo(
/**
*
* @param context
* @param deposit
* @param order
* @param shipId
* @returns
*/
export async function getOrderShipState(
@ -281,23 +298,20 @@ export async function getOrderShipState(
assert(ship);
const { deposit$ship: deposits, shipOrder$ship } = ship;
const application = context.getApplication();
const { type, config } = application!;
assert(type === 'wechatMp');
const { appId, appSecret } = config as WechatMpConfig;
const wechatInstance = WechatSDK.getInstance(
appId,
'wechatMp',
appSecret,
) as WechatMpInstance;
let info = undefined;
let info = undefined, wechatInstance = undefined;
if (deposits && deposits.length > 0) {
//充值
const [deposit] = deposits;
const { pay$deposit: pays, } = deposit;
const pay = pays?.[0];
if (shipId && pay) {
const { config } = pay.application;
const { appId, appSecret } = config as WechatMpConfig;
wechatInstance = WechatSDK.getInstance(
appId,
'wechatMp',
appSecret,
) as WechatMpInstance;
info = {
transaction_id: (pay?.meta as { transaction_id: string })?.transaction_id
};
@ -307,12 +321,19 @@ export async function getOrderShipState(
const { pay$order: pays, } = order;
const pay = pays?.find((ele) => ele.entity === 'wpProduct');
if (shipId && pay) {
const { config } = pay.application;
const { appId, appSecret } = config as WechatMpConfig;
wechatInstance = WechatSDK.getInstance(
appId,
'wechatMp',
appSecret,
) as WechatMpInstance;
info = {
transaction_id: (pay?.meta as { transaction_id: string })?.transaction_id
};
}
}
if (info) {
if (info && wechatInstance) {
const result = await wechatInstance.getOrderState(info);
const { orderState, inComplaint } = result;
if (!inComplaint) {

View File

@ -367,8 +367,8 @@ export default class WechatMpShipDebug<ED extends EntityDict & BaseEntityDict, C
const data = await this.prepareGetOrder(shipId, context);
console.log('mock getPrintInfo to wechatMpShip, data:', data);
const mockData = 'PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PjxoMT5IZWxsbywgV29ybGQhPC9oMT48L2JvZHk+PC9odG1sPg==';
const mockHtml = atob(mockData);
const mockData = 'PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PjxoMT5tb2NrIOmdouWNlTwvaDE+PC9ib2R5PjwvaHRtbD4=';
const mockHtml = decodeURIComponent(escape(window.atob(mockData)));
return {
type: 'html',
data: mockHtml

View File

@ -82,7 +82,7 @@ export default class WechatMpShip<ED extends EntityDict & BaseEntityDict, Contex
const data = await this.prepareGetOrder(shipId, context);
const result = await instance.getExpressOrder(data);
const base64Html = result.print_html;
const html = Buffer.from(base64Html, 'base64').toString('utf8');
const html = decodeURIComponent(escape(window.atob(base64Html)));
return {
type: 'html',
data: html,

View File

@ -5,7 +5,7 @@ import { OperationResult } from 'oak-domain/lib/types';
import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
import { shipProjection, refreshtShipState } from '../utils/ship';
const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 600 * 1000 : 60 * 1000;
const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
const watchers: Watcher<EntityDict, 'ship', BRC>[] = [
{