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

89 lines
3.0 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 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 = get(systemConfig, 'Sms.ctyun.0', {});
const { accessKey, securityKey, endpoint } = ctyunConfig;
assert(accessKey, 'accessKey未配置');
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 = SDK.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 = SDK.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,
};
}
}
;