mirror of
https://github.com/certd/certd.git
synced 2026-05-16 21:27:34 +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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { FormItemProps, IAccessService } from '@certd/pipeline';
|
||||
|
||||
export interface ISmsService {
|
||||
sendSmsCode(opts: { mobile: string; code: string; phoneCode: string }): Promise<void>;
|
||||
setCtx(ctx: { accessService: IAccessService; config: { [key: string]: any } }): void;
|
||||
}
|
||||
|
||||
export type PluginInputs<T = any> = {
|
||||
[key in keyof T]: FormItemProps;
|
||||
};
|
||||
|
||||
export type SmsPluginCtx<T = any> = {
|
||||
accessService: IAccessService;
|
||||
config: T;
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
import { AliyunSmsService } from './aliyun-sms.js';
|
||||
|
||||
export class SmsServiceFactory {
|
||||
static createSmsService(type: string) {
|
||||
switch (type) {
|
||||
case 'aliyun':
|
||||
return new AliyunSmsService();
|
||||
default:
|
||||
throw new Error('不支持的短信服务类型');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user