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

93 lines
3.2 KiB
JavaScript
Raw Permalink 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 });
const tslib_1 = require("tslib");
const assert_1 = require("oak-domain/lib/utils/assert");
const lodash_1 = require("oak-domain/lib/utils/lodash");
const SmsSdk_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/SmsSdk"));
class CTYun {
name = 'ctyun';
async getConfig(context, systemId) {
let system;
if (systemId) {
[system] = await context.select('system', {
data: {
id: 1,
config: 1,
},
filter: {
id: systemId,
},
}, {
dontCollect: true,
});
}
else {
system = context.getApplication().system;
}
const { config: systemConfig } = system;
const mockSend = systemConfig?.Sms?.mockSend;
const ctyunConfig = (0, lodash_1.get)(systemConfig, 'Sms.ctyun.0', {});
const { accessKey, securityKey, endpoint } = ctyunConfig;
(0, assert_1.assert)(accessKey, 'accessKey未配置');
(0, assert_1.assert)(securityKey, 'securityKey未配置');
return {
config: ctyunConfig,
mockSend
};
}
async syncTemplate(options, context) {
const { systemId, pageIndex, pageSize } = options;
const { config, mockSend } = await this.getConfig(context, systemId);
const { accessKey, securityKey, endpoint } = config;
const ctyunInstance = SmsSdk_1.default.getInstance('ctyun', accessKey, securityKey, endpoint);
const result = await ctyunInstance.syncTemplate({
pageIndex: pageIndex || 1,
pageSize: pageSize || 50, // pageSize必须小于或等于50
});
const { data } = result;
if (data) {
return data.map((ele) => {
return {
templateCode: ele.templateCode,
templateName: ele.templateName,
templateContent: ele.templateContent,
};
});
}
return [];
}
async sendSms(params, context) {
const { mobile, templateParam, smsTemplate } = params;
const { templateCode } = smsTemplate;
const { config, mockSend, } = await this.getConfig(context);
const { accessKey, securityKey, endpoint, defaultSignName } = config;
const ctyunInstance = SmsSdk_1.default.getInstance('ctyun', accessKey, securityKey, endpoint);
if (mockSend) {
console.log(`当前模拟发送短信不会实际调用API`);
return {
success: true,
res: '模拟短信发送成功',
};
}
const result = await ctyunInstance.sendSms({
phoneNumber: mobile,
templateParam,
templateCode: templateCode,
signName: defaultSignName,
});
const code = result?.code || '';
if (code === 'OK') {
return {
success: true,
res: result,
};
}
return {
success: false,
res: result,
};
}
}
exports.default = CTYun;
;