66 lines
2.4 KiB
JavaScript
66 lines
2.4 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.wechatMpHandler = void 0;
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
const index_1 = require("./index");
|
||
const wechatMpHandler = async ({ message, applications, system, context }) => {
|
||
const { userId, type } = message;
|
||
const notificationDatas = [];
|
||
const apps = applications.filter(ele => ele.type === 'wechatMp');
|
||
const wechatUsers = await context.select('wechatUser', {
|
||
data: {
|
||
id: 1,
|
||
applicationId: 1,
|
||
openId: 1,
|
||
},
|
||
filter: {
|
||
applicationId: {
|
||
$in: apps.map(ele => ele.id),
|
||
},
|
||
userId,
|
||
}
|
||
}, { dontCollect: true });
|
||
for (const app of apps) {
|
||
// 如果是wechatMp或者wechat,还要保证用户已经有openId
|
||
const wechatUser = wechatUsers.find(ele => ele.applicationId === app.id);
|
||
const messageTypeTemplates = await context.select('messageTypeTemplate', {
|
||
data: {
|
||
id: 1,
|
||
templateId: 1,
|
||
template: {
|
||
id: 1,
|
||
wechatId: 1,
|
||
applicationId: 1,
|
||
},
|
||
type: 1,
|
||
},
|
||
filter: {
|
||
type,
|
||
template: {
|
||
applicationId: app.id,
|
||
},
|
||
},
|
||
}, { dontCollect: true });
|
||
const messageTypeTemplate = messageTypeTemplates.find(ele => ele.template.applicationId === app.id && ele.type === type);
|
||
if (messageTypeTemplate && wechatUser) {
|
||
const converter = index_1.ConverterDict[type] && index_1.ConverterDict[type].wechatMp;
|
||
const dispersedData = converter &&
|
||
(await converter(message, apps, app, context));
|
||
if (dispersedData) {
|
||
notificationDatas.push({
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
data: dispersedData,
|
||
channel: 'wechatMp',
|
||
applicationId: app.id,
|
||
templateId: messageTypeTemplate.template.wechatId,
|
||
data1: {
|
||
openId: wechatUser.openId,
|
||
}
|
||
});
|
||
}
|
||
}
|
||
}
|
||
return notificationDatas;
|
||
};
|
||
exports.wechatMpHandler = wechatMpHandler;
|