实现天翼云短信 暂未测试

This commit is contained in:
wkj 2024-01-05 15:59:37 +08:00
parent 7b601ff11f
commit 3dffa3e3bc
2 changed files with 120 additions and 0 deletions

117
src/utils/sms/ctyun.ts Normal file
View File

@ -0,0 +1,117 @@
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain'
import { BackendRuntimeContext } from '../../context/BackendRuntimeContext';
import { assert } from 'oak-domain/lib/utils/assert';
import Sms from "../../types/Sms";
import { ED } from '../../types/RuntimeCxt';
import { EntityDict } from '../../oak-app-domain';
import { get } from 'oak-domain/lib/utils/lodash';
import SDK, { CTYunSmsInstance } from 'oak-external-sdk/lib/SmsSdk';
import { CTYunSmsConfig } from '../../types/Config';
export default class CTYun implements Sms<ED, BackendRuntimeContext<ED>> {
name = 'ctyun';
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 ctyunConfig = get(
systemConfig,
'Sms.ctyun.0',
{}
) as CTYunSmsConfig;
const { accessKey, securityKey, endpoint } =
ctyunConfig;
assert(accessKey, 'accessKey未配置');
assert(securityKey, 'securityKey未配置');
return ctyunConfig;
}
async syncTemplate(systemId: string, context: BackendRuntimeContext<ED>) {
const { accessKey, securityKey, endpoint } =
await this.getConfig(context, systemId);
const tencentInstance = SDK.getInstance(
'ctyun',
accessKey,
securityKey,
endpoint
) as CTYunSmsInstance;
const result = await tencentInstance.syncTemplate({
pageIndex: 1,
pageSize: 100,
});
// // todo templateName: string,
// templateCode: string,
// templateContent: string
const { data } = result;
if (data) {
return data.map((ele) => {
return {
templateCode: ele.templateCode!,
templateName: ele.templateName!,
templateContent: ele.templateContent!,
};
});
}
return [];
}
async sendSms(
params: {
mobile: string;
templateParam?: Record<string, string>;
smsTemplate: Partial<EntityDict['smsTemplate']['Schema']>;
},
context: BackendRuntimeContext<ED>
): Promise<{ success: boolean; res: any }> {
const { mobile, templateParam, smsTemplate } = params;
const { templateCode } = smsTemplate;
const {
accessKey,
securityKey,
endpoint,
defaultSignName,
} = await this.getConfig(context);
const ctyunInstance = SDK.getInstance(
'ctyun',
accessKey,
securityKey,
endpoint
) as CTYunSmsInstance;
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,
};
}
};

View File

@ -10,13 +10,16 @@ import Sms from '../../types/Sms';
import Ali from './ali';
import Tencent from './tencent';
import CTYun from './ctyun';
const ali = new Ali();
const tencent = new Tencent();
const ctyun = new CTYun();
const SmsDict: Record<string, any> = {
[ali.name]: ali,
[tencent.name]: tencent,
[ctyun.name]: ctyun,
};