oak-general-business/lib/utils/sms.js

50 lines
2.3 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";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendSms = void 0;
const oak_external_sdk_1 = require("oak-external-sdk");
const assert_1 = require("oak-domain/lib/utils/assert");
const types_1 = require("oak-domain/lib/types");
async function sendSms(options, context) {
const { origin, templateName, mobile, templateParamSet, } = options;
const application = context.getApplication();
const { system } = application;
const { platform, config: systemConfig } = system;
const { config: platformConfig } = platform || {};
const accountConfigs = systemConfig?.Account?.[origin] || platformConfig?.Account?.[origin];
const smsConfigs = systemConfig?.Sms?.[origin] || platformConfig?.Sms?.[origin];
if (!accountConfigs ||
accountConfigs.length === 0 ||
!smsConfigs ||
smsConfigs.length === 0) {
(0, assert_1.assert)(false, `${origin}短信未配置`);
}
if (origin === 'tencent') {
for (const smsConfig of smsConfigs) {
const smsConfig = smsConfigs[0];
const accountConfig = accountConfigs[0];
// const accountConfig = (accountConfigs as TencentCloudConfig[]).find(
// ele => ele.secretId === smsConfig.secretId
// )!;
const template = smsConfig.templates?.[templateName];
const SmsSdkInstance = oak_external_sdk_1.SmsSdk.getInstance(origin, accountConfig.secretId, accountConfig.secretKey, accountConfig.region, accountConfig.endpoint);
const data = await SmsSdkInstance.sendSms({
PhoneNumberSet: [mobile],
SmsSdkAppId: smsConfig.smsSdkAppId,
SignName: template.signName || smsConfig.defaultSignName,
TemplateId: template.code,
TemplateParamSet: templateParamSet,
});
const sendStatus = data.SendStatusSet[0];
if (sendStatus.Code === 'Ok') {
return true;
}
console.warn(`通过微信云发送sms失败电话是${mobile}模板Id是${template.code}`, sendStatus);
}
}
else {
throw new Error('未实现');
}
throw new types_1.OakExternalException('尝试发送sms短信失败');
}
exports.sendSms = sendSms;