93 lines
3.0 KiB
JavaScript
93 lines
3.0 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.wechatMpHandler = void 0;
|
|
const tslib_1 = require("tslib");
|
|
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
|
const WechatSDK_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/WechatSDK"));
|
|
const domain_1 = require("oak-domain/lib/utils/domain");
|
|
const wechatMpHandler = async (notification, context) => {
|
|
const { data, templateId, messageSystemId, data1, id } = notification;
|
|
const [messageSystem] = await context.select('messageSystem', {
|
|
data: {
|
|
id: 1,
|
|
messageId: 1,
|
|
message: {
|
|
id: 1,
|
|
userId: 1,
|
|
router: 1,
|
|
type: 1,
|
|
restriction: 1,
|
|
},
|
|
system: {
|
|
id: 1,
|
|
application$system: {
|
|
$entity: 'application',
|
|
data: {
|
|
id: 1,
|
|
type: 1,
|
|
config: 1,
|
|
},
|
|
},
|
|
}
|
|
},
|
|
filter: {
|
|
id: messageSystemId,
|
|
}
|
|
}, { dontCollect: true });
|
|
const { system, message } = messageSystem;
|
|
const { router } = message;
|
|
const { application$system: applications } = system;
|
|
const app = applications.find(ele => ele.type === 'wechatMp');
|
|
const { config } = app;
|
|
const { appId, appSecret } = config;
|
|
const instance = WechatSDK_1.default.getInstance(appId, 'wechatMp', appSecret);
|
|
let page;
|
|
const disableRouter = message?.restriction?.disableRouter?.['wechatMp'] || false;
|
|
if (router && !disableRouter) {
|
|
const pathname = router.pathname;
|
|
const url = pathname.startsWith('/')
|
|
? `pages${pathname}/index`
|
|
: `pages/${pathname}/index`;
|
|
page = (0, domain_1.composeUrl)(url, Object.assign({}, router.props, router.state));
|
|
}
|
|
// 根据当前环境决定消息推哪个版本
|
|
const StateDict = {
|
|
'development': 'developer',
|
|
'staging': 'trial',
|
|
'production': 'former',
|
|
};
|
|
try {
|
|
await instance.sendSubscribedMessage({
|
|
templateId: templateId,
|
|
data: data,
|
|
openId: data1.openId,
|
|
page,
|
|
state: StateDict[process.env.NODE_ENV],
|
|
});
|
|
await context.operate('notification', {
|
|
id: await (0, uuid_1.generateNewIdAsync)(),
|
|
action: 'succeed',
|
|
data: {},
|
|
filter: {
|
|
id,
|
|
}
|
|
}, { dontCollect: true });
|
|
}
|
|
catch (err) {
|
|
console.warn('发微信小程序消息失败', err);
|
|
await context.operate('notification', {
|
|
id: await (0, uuid_1.generateNewIdAsync)(),
|
|
action: 'fail',
|
|
data: {
|
|
data2: {
|
|
res: err?.message
|
|
}
|
|
},
|
|
filter: {
|
|
id,
|
|
}
|
|
}, { dontCollect: true });
|
|
}
|
|
};
|
|
exports.wechatMpHandler = wechatMpHandler;
|