oak-general-business/es/utils/sms/tencent.js

106 lines
3.8 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.

import { assert } from 'oak-domain/lib/utils/assert';
import { get } from 'oak-domain/lib/utils/lodash';
import SDK from 'oak-external-sdk/lib/SmsSdk';
export default class Tencent {
name = 'tencent';
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 tencentConfig = get(systemConfig, 'Sms.tencent.0', {});
const { secretId, secretKey, region, endpoint, smsSdkAppId } = tencentConfig;
assert(secretId, 'secretId未配置');
assert(secretKey, 'secretKey未配置');
assert(region, 'region未配置');
assert(endpoint, 'endpoint未配置');
assert(smsSdkAppId, 'smsSdkAppId未配置');
return {
config: tencentConfig,
mockSend
};
}
async syncTemplate(options, context) {
const { systemId, pageIndex, pageSize, international } = options;
const { config, mockSend } = await this.getConfig(context, systemId);
const { secretId, secretKey, region, endpoint } = config;
const tencentInstance = SDK.getInstance('tencent', secretId, secretKey, endpoint, region);
const result = await tencentInstance.syncTemplate({
International: international || 0,
Offset: pageIndex && pageIndex >= 1 ? pageIndex - 1 : 0,
Limit: pageSize || 100,
});
const { DescribeTemplateStatusSet } = result;
if (DescribeTemplateStatusSet) {
return DescribeTemplateStatusSet.map((ele) => {
return {
templateCode: ele.TemplateId.toString(),
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 { secretId, secretKey, region, endpoint, defaultSignName, smsSdkAppId } = config;
const tencentInstance = SDK.getInstance('tencent', secretId, secretKey, endpoint, region);
// const params: SendSmsRequest = {
// PhoneNumberSet: [],
// TemplateParamSet: [],
// SmsSdkAppId: '',
// TemplateId: '',
// };
const TemplateParamSet = [];
if (templateParam) {
Object.keys(templateParam).forEach((ele) => {
TemplateParamSet.push(templateParam[ele]);
});
}
if (mockSend) {
console.log(`当前模拟发送短信不会实际调用API`);
return {
success: true,
res: '模拟短信发送成功',
};
}
const result = await tencentInstance.sendSms({
PhoneNumberSet: [mobile],
SmsSdkAppId: smsSdkAppId,
TemplateParamSet,
TemplateId: templateCode,
SignName: defaultSignName,
});
const code = result?.SendStatusSet?.[0]?.Code || '';
if (code === 'Ok') {
return {
success: true,
res: result,
};
}
return {
success: false,
res: result,
};
}
}
;