oak-general-business/lib/triggers/wechatQrCode.js

49 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const oak_external_sdk_1 = require("oak-external-sdk");
const uuid_1 = require("oak-domain/lib/utils/uuid");
const lodash_1 = require("lodash");
const triggers = [
{
name: '选择wechatQrCode时动态生成需要的数据',
entity: 'wechatQrCode',
action: 'select',
when: 'after',
fn: async ({ result }, context, params) => {
let count = 0;
const application = await context.getApplication();
const { type, config } = application;
(0, assert_1.default)(type === 'wechatMp' || config.type === 'wechatMp');
const config2 = config;
const { appId, appSecret } = config2;
for (const code of result) {
const { type, expired, id } = code;
if (type === 'wechatMpWxaCode' && code.hasOwnProperty('buffer')) {
// 小程序码去实时获取(暂时不考虑缓存)
const wechatInstance = oak_external_sdk_1.WechatSDK.getInstance(appId, appSecret, 'wechatMp');
const buffer = await wechatInstance.getMpUnlimitWxaCode({
scene: (0, uuid_1.shrinkUuidTo32Bytes)(id),
page: 'pages/index/index', // todo这里用其它的页面微信服务器拒绝因为没发布。应该是 pages/wechatQrCode/scan/index
});
// 把arrayBuffer转成字符串返回
const str = String.fromCharCode(...new Uint8Array(buffer));
(0, lodash_1.assign)(code, {
buffer: str,
});
count++;
}
else if (expired && code.hasOwnProperty('url')) {
// 如果过期了,在这里生成新的临时码并修改值(公众号)
throw new Error('not implemented yet');
}
}
return count;
}
},
];
exports.default = triggers;