小程序确认收货提醒

This commit is contained in:
lxy 2025-03-14 17:16:07 +08:00
parent b752c97da1
commit b1cab48ca4
11 changed files with 380 additions and 6 deletions

View File

@ -2,7 +2,7 @@ import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import { DATA_SUBSCRIBER_KEYS } from '../config/constants';
import assert from 'assert';
import { getShipClazz, getShipEntity } from '../utils/shipClazz';
import { uploadShippingInfo } from '../utils/ship';
import { notifyConfirmReceive, uploadShippingInfo } from '../utils/ship';
const triggers = [
{
name: '当虚拟或自提类型的ship创建后自动发货',
@ -322,5 +322,55 @@ const triggers = [
}
}
},
{
name: '当ship签收后调用小程序确认收货提醒',
entity: 'ship',
action: 'receive',
when: 'commit',
asRoot: true,
filter: {
receiveAt: {
$exists: true,
}
},
fn: async ({ ids }, context, option) => {
assert(ids.length === 1);
const id = ids[0];
const [ship] = await context.select('ship', {
data: {
id: 1,
entity: 1,
entityId: 1,
shipServiceId: 1,
extraShipId: 1,
receiveAt: 1,
shipOrder$ship: {
$entity: 'shipOrder',
data: {
id: 1,
orderId: 1,
}
},
wechatMpShip: {
id: 1,
applicationId: 1,
}
},
filter: {
id
}
}, { dontCollect: true, forUpdate: true });
const { entity, entityId, extraShipId, shipOrder$ship, wechatMpShip } = ship;
if (entity && entityId && extraShipId && shipOrder$ship) {
const shipClazz = await getShipClazz(entity, entityId, context);
const { openId } = await shipClazz.getReceiverInfo(shipOrder$ship.map((ele) => ele.orderId), wechatMpShip?.applicationId, context);
if (openId) {
//当存在openId时调用小程序发货信息录入
await notifyConfirmReceive(id, context);
return 1;
}
}
}
},
];
export default triggers;

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

@ -27,3 +27,7 @@ export declare function getOrderShipState(context: BRC, shipId: string): Promise
* @returns
*/
export declare function refreshtShipState(ship: EntityDict['ship']['Schema'], context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
/**
*
*/
export declare function notifyConfirmReceive(shipId: string, context: BRC): Promise<void>;

View File

@ -366,3 +366,69 @@ export async function refreshtShipState(ship, context) {
}, {});
}
}
/**
* 小程序确认收货提醒
*/
export async function notifyConfirmReceive(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
receiveAt: 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,
},
filter: {
iState: 'paid'
}
}
}
}
},
},
filter: {
id: shipId,
}
}, {
blockTrigger: true,
forUpdate: true
});
assert(ship);
const { shipOrder$ship, receiveAt } = ship;
assert(!!receiveAt && shipOrder$ship && shipOrder$ship.length > 0);
const orders = shipOrder$ship.map((ele) => ele.order);
for (const order of orders) {
const wechatPay = order.pay$order.find((ele) => ele.entity === 'wpProduct');
const meta = wechatPay?.meta;
const notifyInfo = {
transaction_id: meta.transaction_id,
received_time: receiveAt.valueOf(),
};
const { type, config } = wechatPay?.application;
if (type === 'wechatMp') {
const { appId, appSecret } = config;
const wechatInstance = WechatSDK.getInstance(appId, 'wechatMp', appSecret);
await wechatInstance.notifyConfirmReceive(notifyInfo);
}
}
}

View File

@ -6,7 +6,7 @@ export function getWpProductTypeFromEnv() {
return ['jsapi', 'native'];
}
else if (isPc) {
return ['native'];
return ['native', 'h5'];
}
// 手机端非微信环境
return ['h5', 'native'];

View File

@ -325,5 +325,55 @@ const triggers = [
}
}
},
{
name: '当ship签收后调用小程序确认收货提醒',
entity: 'ship',
action: 'receive',
when: 'commit',
asRoot: true,
filter: {
receiveAt: {
$exists: true,
}
},
fn: async ({ ids }, context, option) => {
(0, assert_1.default)(ids.length === 1);
const id = ids[0];
const [ship] = await context.select('ship', {
data: {
id: 1,
entity: 1,
entityId: 1,
shipServiceId: 1,
extraShipId: 1,
receiveAt: 1,
shipOrder$ship: {
$entity: 'shipOrder',
data: {
id: 1,
orderId: 1,
}
},
wechatMpShip: {
id: 1,
applicationId: 1,
}
},
filter: {
id
}
}, { dontCollect: true, forUpdate: true });
const { entity, entityId, extraShipId, shipOrder$ship, wechatMpShip } = ship;
if (entity && entityId && extraShipId && shipOrder$ship) {
const shipClazz = await (0, shipClazz_1.getShipClazz)(entity, entityId, context);
const { openId } = await shipClazz.getReceiverInfo(shipOrder$ship.map((ele) => ele.orderId), wechatMpShip?.applicationId, context);
if (openId) {
//当存在openId时调用小程序发货信息录入
await (0, ship_1.notifyConfirmReceive)(id, context);
return 1;
}
}
}
},
];
exports.default = triggers;

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

@ -27,3 +27,7 @@ export declare function getOrderShipState(context: BRC, shipId: string): Promise
* @returns
*/
export declare function refreshtShipState(ship: EntityDict['ship']['Schema'], context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
/**
*
*/
export declare function notifyConfirmReceive(shipId: string, context: BRC): Promise<void>;

View File

@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.refreshtShipState = exports.getOrderShipState = exports.uploadShippingInfo = exports.maskPhone = exports.shipProjection = void 0;
exports.notifyConfirmReceive = exports.refreshtShipState = exports.getOrderShipState = exports.uploadShippingInfo = exports.maskPhone = exports.shipProjection = 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");
@ -374,3 +374,70 @@ async function refreshtShipState(ship, context) {
}
}
exports.refreshtShipState = refreshtShipState;
/**
* 小程序确认收货提醒
*/
async function notifyConfirmReceive(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
receiveAt: 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,
},
filter: {
iState: 'paid'
}
}
}
}
},
},
filter: {
id: shipId,
}
}, {
blockTrigger: true,
forUpdate: true
});
(0, assert_1.assert)(ship);
const { shipOrder$ship, receiveAt } = ship;
(0, assert_1.assert)(!!receiveAt && shipOrder$ship && shipOrder$ship.length > 0);
const orders = shipOrder$ship.map((ele) => ele.order);
for (const order of orders) {
const wechatPay = order.pay$order.find((ele) => ele.entity === 'wpProduct');
const meta = wechatPay?.meta;
const notifyInfo = {
transaction_id: meta.transaction_id,
received_time: receiveAt.valueOf(),
};
const { type, config } = wechatPay?.application;
if (type === 'wechatMp') {
const { appId, appSecret } = config;
const wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, 'wechatMp', appSecret);
await wechatInstance.notifyConfirmReceive(notifyInfo);
}
}
}
exports.notifyConfirmReceive = notifyConfirmReceive;

View File

@ -9,7 +9,7 @@ function getWpProductTypeFromEnv() {
return ['jsapi', 'native'];
}
else if (utils_1.isPc) {
return ['native'];
return ['native', 'h5'];
}
// 手机端非微信环境
return ['h5', 'native'];

View File

@ -5,7 +5,7 @@ import { BRC } from '../types/RuntimeCxt';
import { DATA_SUBSCRIBER_KEYS } from '../config/constants';
import assert from 'assert';
import { getShipClazz, getShipEntity } from '../utils/shipClazz';
import { uploadShippingInfo } from '../utils/ship';
import { notifyConfirmReceive, uploadShippingInfo } from '../utils/ship';
const triggers: Trigger<EntityDict, 'ship', BRC>[] = [
{
@ -336,6 +336,58 @@ const triggers: Trigger<EntityDict, 'ship', BRC>[] = [
}
}
} as UpdateTriggerCrossTxn<EntityDict, 'ship', BRC>,
{
name: '当ship签收后调用小程序确认收货提醒',
entity: 'ship',
action: 'receive',
when: 'commit',
asRoot: true,
filter: {
receiveAt: {
$exists: true,
}
},
fn: async ({ ids }, context, option) => {
assert(ids.length === 1);
const id = ids[0];
const [ship] = await context.select('ship', {
data: {
id: 1,
entity: 1,
entityId: 1,
shipServiceId: 1,
extraShipId: 1,
receiveAt: 1,
shipOrder$ship: {
$entity: 'shipOrder',
data: {
id: 1,
orderId: 1,
}
},
wechatMpShip: {
id: 1,
applicationId: 1,
}
},
filter: {
id
}
}, { dontCollect: true, forUpdate: true });
const { entity, entityId, extraShipId, shipOrder$ship, wechatMpShip } = ship;
if (entity && entityId && extraShipId && shipOrder$ship) {
const shipClazz = await getShipClazz(entity, entityId, context);
const { openId } = await shipClazz.getReceiverInfo(shipOrder$ship.map((ele) => ele.orderId!), wechatMpShip?.applicationId!, context);
if (openId) {
//当存在openId时调用小程序发货信息录入
await notifyConfirmReceive(id!, context);
return 1;
}
}
}
} as UpdateTriggerCrossTxn<EntityDict, 'ship', BRC>,
];
export default triggers;

View File

@ -413,4 +413,85 @@ export async function refreshtShipState(
}
}, {});
}
}
/**
*
*/
export async function notifyConfirmReceive(
shipId: string,
context: BRC,
) {
const [ship] = await context.select('ship', {
data: {
id: 1,
receiveAt: 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,
},
filter: {
iState: 'paid'
}
}
}
}
},
},
filter: {
id: shipId,
}
}, {
blockTrigger: true,
forUpdate: true
});
assert(ship);
const { shipOrder$ship, receiveAt } = ship;
assert(!!receiveAt && shipOrder$ship && shipOrder$ship.length > 0);
const orders = shipOrder$ship.map((ele) => ele.order);
for (const order of orders) {
const wechatPay = order.pay$order!.find((ele) => ele.entity === 'wpProduct');
const meta = wechatPay?.meta as {
transaction_id: string;
payer: {
openid: string;
};
};
const notifyInfo = {
transaction_id: meta.transaction_id,
received_time: receiveAt.valueOf(),
}
const { type, config } = wechatPay?.application!;
if (type === 'wechatMp') {
const { appId, appSecret } = config as WechatMpConfig;
const wechatInstance = WechatSDK.getInstance(
appId,
'wechatMp',
appSecret,
) as WechatMpInstance;
await wechatInstance.notifyConfirmReceive(notifyInfo);
}
}
}

View File

@ -10,7 +10,7 @@ export function getWpProductTypeFromEnv(): EntityDict['wpProduct']['OpSchema']['
return ['jsapi', 'native'];
}
else if (isPc) {
return ['native'];
return ['native', 'h5'];
}
// 手机端非微信环境