同步阿里云短信模板
This commit is contained in:
parent
52d53a0b77
commit
33650f93a5
|
|
@ -11,44 +11,94 @@ import { AliSmsConfig } from '../../types/Config';
|
|||
export default class Ali implements Sms<ED, BackendRuntimeContext<ED>> {
|
||||
name = 'ali';
|
||||
|
||||
getConfig(context: BackendRuntimeContext<ED>) {
|
||||
const { system } = context.getApplication()!;
|
||||
const { config: systemConfig } = system as EntityDict['system']['Schema'];
|
||||
async getConfig(context: BackendRuntimeContext<ED>, systemId?: string) {
|
||||
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 as EntityDict['system']['Schema'];
|
||||
const aliConfig = get(systemConfig, 'Sms.ali.0', {}) as AliSmsConfig;
|
||||
const { accessKeyId,
|
||||
accessKeySecret,
|
||||
defaultSignName,
|
||||
endpoint } = aliConfig;
|
||||
const { accessKeyId, accessKeySecret, defaultSignName, endpoint } =
|
||||
aliConfig;
|
||||
assert(accessKeyId, 'accessKeyId未配置');
|
||||
assert(accessKeySecret, 'accessKeySecret未配置');
|
||||
assert(defaultSignName, 'defaultSignName未配置');
|
||||
assert(endpoint, 'endpoint未配置');
|
||||
return aliConfig;
|
||||
}
|
||||
async syncTemplate(systemId: string) {
|
||||
// todo
|
||||
return [];
|
||||
}
|
||||
async sendSms(params: {
|
||||
mobile: string,
|
||||
templateParam?: Record<string, any>,
|
||||
smsTemplate: Partial<EntityDict['smsTemplate']['Schema']>
|
||||
}, context: BackendRuntimeContext<ED>): Promise<{ success: boolean, res: any }> {
|
||||
const { mobile, templateParam, smsTemplate } = params;
|
||||
const { templateCode } = smsTemplate;
|
||||
const { accessKeyId, accessKeySecret, defaultSignName, endpoint } = this.getConfig(context);
|
||||
async syncTemplate(systemId: string, context: BackendRuntimeContext<ED>) {
|
||||
const { accessKeyId, accessKeySecret, endpoint, apiVersion } = await this.getConfig(
|
||||
context,
|
||||
systemId
|
||||
);
|
||||
const aliInstance = SDK.getInstance(
|
||||
'ali',
|
||||
accessKeyId,
|
||||
accessKeySecret,
|
||||
endpoint,
|
||||
undefined,
|
||||
apiVersion
|
||||
) as AliSmsInstance;
|
||||
const result = await aliInstance.syncTemplate({
|
||||
PageIndex: 1,
|
||||
PageSize: 100,
|
||||
});
|
||||
// // todo templateName: string,
|
||||
// templateCode: string,
|
||||
// templateContent: string
|
||||
const { smsTemplateList } = result;
|
||||
if (smsTemplateList) {
|
||||
return smsTemplateList.map((ele) => {
|
||||
return {
|
||||
templateCode: ele.templateCode!,
|
||||
templateName: ele.templateName!,
|
||||
templateContent: ele.templateContent!,
|
||||
};
|
||||
});
|
||||
}
|
||||
return [];
|
||||
}
|
||||
async sendSms(
|
||||
params: {
|
||||
mobile: string;
|
||||
templateParam?: Record<string, any>;
|
||||
smsTemplate: Partial<EntityDict['smsTemplate']['Schema']>;
|
||||
},
|
||||
context: BackendRuntimeContext<ED>
|
||||
): Promise<{ success: boolean; res: any }> {
|
||||
const { mobile, templateParam, smsTemplate } = params;
|
||||
const { templateCode } = smsTemplate;
|
||||
const { accessKeyId, accessKeySecret, defaultSignName, endpoint } =
|
||||
await this.getConfig(context);
|
||||
const aliInstance = SDK.getInstance(
|
||||
'ali',
|
||||
accessKeyId,
|
||||
accessKeySecret,
|
||||
endpoint
|
||||
) as AliSmsInstance;
|
||||
const result = await aliInstance.sendSms({
|
||||
phoneNumbers: [mobile],
|
||||
templateCode: templateCode!,
|
||||
templateParam: templateParam,
|
||||
signName: defaultSignName,
|
||||
})
|
||||
});
|
||||
const { code, message, requestId } = result;
|
||||
if (code === 'OK') {
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -48,13 +48,13 @@ export default class CTYun implements Sms<ED, BackendRuntimeContext<ED>> {
|
|||
async syncTemplate(systemId: string, context: BackendRuntimeContext<ED>) {
|
||||
const { accessKey, securityKey, endpoint } =
|
||||
await this.getConfig(context, systemId);
|
||||
const tencentInstance = SDK.getInstance(
|
||||
const ctyunInstance = SDK.getInstance(
|
||||
'ctyun',
|
||||
accessKey,
|
||||
securityKey,
|
||||
endpoint
|
||||
) as CTYunSmsInstance;
|
||||
const result = await tencentInstance.syncTemplate({
|
||||
const result = await ctyunInstance.syncTemplate({
|
||||
pageIndex: 1,
|
||||
pageSize: 100,
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue