mirror of
https://github.com/certd/certd.git
synced 2026-06-30 16:39:43 +08:00
perf: 支持短信验证码登录
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { AliyunAccess, AliyunClient } from '@certd/plugin-plus';
|
||||
import { logger } from '@certd/basic';
|
||||
import { ISmsService, PluginInputs, SmsPluginCtx } from './api.js';
|
||||
export type AliyunSmsConfig = {
|
||||
accessId: string;
|
||||
regionId: string;
|
||||
signName: string;
|
||||
codeTemplateId: string;
|
||||
};
|
||||
|
||||
export class AliyunSmsService implements ISmsService {
|
||||
static getDefine() {
|
||||
return {
|
||||
name: 'aliyun-sms',
|
||||
desc: '阿里云短信服务',
|
||||
input: {
|
||||
accessId: {
|
||||
title: '阿里云授权',
|
||||
component: {
|
||||
name: 'access-selector',
|
||||
from: 'aliyun',
|
||||
},
|
||||
required: true,
|
||||
},
|
||||
regionId: {
|
||||
title: '接入点',
|
||||
required: true,
|
||||
},
|
||||
signName: {
|
||||
title: '签名',
|
||||
required: true,
|
||||
},
|
||||
codeTemplateId: {
|
||||
title: '验证码模板Id',
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user