oak-general-business/lib/aspects/wechatMenu.js

174 lines
5.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentMenu = getCurrentMenu;
exports.getMenu = getMenu;
exports.createMenu = createMenu;
exports.createConditionalMenu = createConditionalMenu;
exports.deleteConditionalMenu = deleteConditionalMenu;
exports.deleteMenu = deleteMenu;
const tslib_1 = require("tslib");
const WechatSDK_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/WechatSDK"));
const assert_1 = require("oak-domain/lib/utils/assert");
const uuid_1 = require("oak-domain/lib/utils/uuid");
const Exception_1 = require("oak-domain/lib/types/Exception");
async function getWechatPublicConfig(applicationId, context) {
const [application] = await context.select('application', {
data: {
id: 1,
config: 1,
type: 1,
},
filter: {
id: applicationId,
},
}, {
dontCollect: true,
});
return application;
}
async function getCurrentMenu(params, context) {
const application = await getWechatPublicConfig(params.applicationId, context);
(0, assert_1.assert)(application);
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK_1.default.getInstance(appId, type, appSecret);
try {
const result = await wechatInstance.getCurrentMenu();
return result;
}
catch (e) {
throw e;
}
}
async function getMenu(params, context) {
const application = await getWechatPublicConfig(params.applicationId, context);
(0, assert_1.assert)(application);
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK_1.default.getInstance(appId, type, appSecret);
try {
const result = await wechatInstance.getMenu();
return result;
}
catch (e) {
if (e instanceof Exception_1.OakExternalException &&
e?.message?.includes('menu no exist')) {
return {
menu: {
button: []
}
};
}
throw e;
}
}
async function createMenu(params, context) {
const application = await getWechatPublicConfig(params.applicationId, context);
(0, assert_1.assert)(application);
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK_1.default.getInstance(appId, type, appSecret);
try {
const result = await wechatInstance.createMenu(params.menuConfig);
await context.operate('wechatMenu', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
iState: 'success',
},
filter: {
id: params.id,
},
}, {});
}
catch (e) {
await context.operate('wechatMenu', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
iState: 'fail',
},
filter: {
id: params.id,
},
}, {});
throw e;
}
}
async function createConditionalMenu(params, context) {
const application = await getWechatPublicConfig(params.applicationId, context);
(0, assert_1.assert)(application);
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK_1.default.getInstance(appId, type, appSecret);
try {
const result = await wechatInstance.createConditionalMenu(params.menuConfig);
await context.operate('wechatMenu', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
iState: 'success',
menuId: result.menuid,
},
filter: {
id: params.id,
},
}, {});
}
catch (e) {
await context.operate('wechatMenu', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
iState: 'fail',
},
filter: {
id: params.id,
},
}, {});
throw e;
}
}
async function deleteConditionalMenu(params, context) {
const application = await getWechatPublicConfig(params.applicationId, context);
(0, assert_1.assert)(application);
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK_1.default.getInstance(appId, type, appSecret);
const result = await wechatInstance.deleteConditionalMenu(params.menuId);
return result;
}
async function deleteMenu(params, context) {
const application = await getWechatPublicConfig(params.applicationId, context);
(0, assert_1.assert)(application);
const { type, config } = application;
(0, assert_1.assert)(type === 'wechatPublic');
let appId, appSecret;
const config2 = config;
appId = config2.appId;
appSecret = config2.appSecret;
const wechatInstance = WechatSDK_1.default.getInstance(appId, type, appSecret);
const result = await wechatInstance.deleteMenu();
return result;
}