"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wechatPublicHandler = 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 domain_2 = require("../domain"); const wechatPublicHandler = 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 === 'wechatPublic'); const { config, id: applicationId } = app; const { appId, appSecret } = config; const [domain] = await context.select('domain', { data: { id: 1, url: 1, apiPath: 1, protocol: 1, port: 1, }, filter: { system: { application$system: { id: applicationId, }, }, }, }, { dontCollect: true }); const instance = WechatSDK_1.default.getInstance(appId, 'wechatPublic', appSecret); const { openId, wechatMpAppId } = data1; let page; const disableRouter = message?.restriction?.disableRouter?.['wechatPublic'] || false; if (router && !disableRouter) { const pathname = router.pathname; if (wechatMpAppId) { const url = pathname.startsWith('/') ? `pages${pathname}/index` : `pages/${pathname}/index`; page = (0, domain_1.composeUrl)(url, Object.assign({}, router.props, router.state)); } else { const url = (0, domain_2.composeDomainUrl)(domain, pathname); page = (0, domain_1.composeUrl)(url, Object.assign({}, router.props, router.state)); } } try { await instance.sendTemplateMessage({ openId, templateId: templateId, url: !wechatMpAppId ? page : undefined, data: data, miniProgram: wechatMpAppId && page ? { appid: wechatMpAppId, pagepath: page, } : undefined, clientMsgId: id, }); 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.wechatPublicHandler = wechatPublicHandler;