Files
certd/packages/ui/certd-server/src/modules/basic/sms/aliyun-sms.ts
T

73 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-11-28 17:36:45 +08:00
import { logger } from '@certd/basic';
import { ISmsService, PluginInputs, SmsPluginCtx } from './api.js';
import { AliyunAccess, AliyunClient } from '../../../plugins/plugin-lib/aliyun/index.js';
2024-12-02 15:11:29 +08:00
2024-11-28 17:36:45 +08:00
export type AliyunSmsConfig = {
accessId: string;
signName: string;
codeTemplateId: string;
};
export class AliyunSmsService implements ISmsService {
static getDefine() {
return {
2024-12-02 15:11:29 +08:00
name: 'aliyun',
2024-11-28 17:36:45 +08:00
desc: '阿里云短信服务',
input: {
accessId: {
title: '阿里云授权',
component: {
name: 'access-selector',
2024-12-02 15:11:29 +08:00
type: 'aliyun',
2024-11-28 17:36:45 +08:00
},
required: true,
},
signName: {
title: '签名',
2024-12-02 15:11:29 +08:00
component: {
name: 'a-input',
vModel: 'value',
},
2024-11-28 17:36:45 +08:00
required: true,
},
codeTemplateId: {
title: '验证码模板Id',
2024-12-02 15:11:29 +08:00
component: {
name: 'a-input',
vModel: 'value',
},
2024-11-28 17:36:45 +08:00
required: true,
},
} as PluginInputs<AliyunSmsConfig>,
};
}
ctx: SmsPluginCtx<AliyunSmsConfig>;
setCtx(ctx: any) {
this.ctx = ctx;
}
async sendSmsCode(opts: { mobile: string; code: string; phoneCode: string }) {
const { mobile, code, phoneCode } = opts;
const access = await this.ctx.accessService.getById<AliyunAccess>(this.ctx.config.accessId);
const aliyunClinet = new AliyunClient({ logger });
await aliyunClinet.init({
accessKeyId: access.accessKeyId,
accessKeySecret: access.accessKeySecret,
endpoint: 'https://dysmsapi.aliyuncs.com',
apiVersion: '2017-05-25',
});
const smsConfig = this.ctx.config;
const phoneNumber = phoneCode + mobile;
const params = {
PhoneNumbers: phoneNumber,
SignName: smsConfig.signName,
TemplateCode: smsConfig.codeTemplateId,
TemplateParam: `{"code":"${code}"}`,
};
await aliyunClinet.request('SendSms', params);
}
}