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

91 lines
3.2 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 Ali {
name = 'ali';
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 aliConfig = get(systemConfig, 'Sms.ali.0', {});
const { accessKeyId, accessKeySecret, defaultSignName, endpoint } = aliConfig;
assert(accessKeyId, 'accessKeyId未配置');
assert(accessKeySecret, 'accessKeySecret未配置');
assert(defaultSignName, 'defaultSignName未配置');
assert(endpoint, 'endpoint未配置');
return {
config: aliConfig,
mockSend
};
}
async syncTemplate(options, context) {
const { systemId, pageIndex, pageSize } = options;
const { config, mockSend } = await this.getConfig(context, systemId);
const { accessKeyId, accessKeySecret, endpoint, apiVersion } = config;
const aliInstance = SDK.getInstance('ali', accessKeyId, accessKeySecret, endpoint, undefined, apiVersion);
const result = await aliInstance.syncTemplate({
pageIndex: pageIndex || 1,
pageSize: pageSize || 50
});
const { smsTemplateList } = result;
if (smsTemplateList) {
return smsTemplateList.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 { accessKeyId, accessKeySecret, defaultSignName, endpoint } = config;
const aliInstance = SDK.getInstance('ali', accessKeyId, accessKeySecret, endpoint);
if (mockSend) {
console.log(`当前模拟发送短信不会实际调用API`);
return {
success: true,
res: '模拟短信发送成功',
};
}
const result = await aliInstance.sendSms({
phoneNumbers: [mobile],
templateCode: templateCode,
templateParam: templateParam,
signName: defaultSignName,
});
const { code, message, requestId } = result;
if (code === 'OK') {
return {
success: true,
res: result,
};
}
return {
success: false,
res: result,
};
}
}
;