format + lint
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import { IsEnum, IsString, Length, ValidateIf } from "class-validator";
|
||||
import { IsEnum, IsString, Length, ValidateIf } from 'class-validator';
|
||||
|
||||
export class SendVerificationCodeDto {
|
||||
@IsEnum(['phone', 'email'], { message: '请求类型错误' })
|
||||
targetType: 'phone' | 'email';
|
||||
@IsEnum(['phone', 'email'], { message: '请求类型错误' })
|
||||
targetType: 'phone' | 'email';
|
||||
|
||||
@IsEnum(['login'], { message: '请求类型错误' })
|
||||
type: 'login'
|
||||
@IsEnum(['login'], { message: '请求类型错误' })
|
||||
type: 'login';
|
||||
|
||||
@ValidateIf(o => o.targetType === 'phone')
|
||||
@IsString({ message: '手机号必须输入' })
|
||||
@Length(11, 11, { message: '手机号异常' })// 中国大陆,11位数字
|
||||
phone?: string;
|
||||
@ValidateIf((o) => o.targetType === 'phone')
|
||||
@IsString({ message: '手机号必须输入' })
|
||||
@Length(11, 11, { message: '手机号异常' }) // 中国大陆,11位数字
|
||||
phone?: string;
|
||||
|
||||
@ValidateIf(o => o.targetType === 'email')
|
||||
@IsString({ message: '邮箱必须输入' })
|
||||
@Length(6, 254, { message: '邮箱异常' })// RFC 5321
|
||||
email?: string;
|
||||
}
|
||||
@ValidateIf((o) => o.targetType === 'email')
|
||||
@IsString({ message: '邮箱必须输入' })
|
||||
@Length(6, 254, { message: '邮箱异常' }) // RFC 5321
|
||||
email?: string;
|
||||
}
|
||||
|
||||
@@ -4,25 +4,22 @@ import { VerificationService } from './verification.service';
|
||||
|
||||
@Controller('verification')
|
||||
export class VerificationController {
|
||||
constructor(private readonly verificationService: VerificationService) {}
|
||||
|
||||
constructor(
|
||||
private readonly verificationService: VerificationService,
|
||||
) { }
|
||||
|
||||
@Post('send')
|
||||
async sendVerificationCode(@Body() dto: SendVerificationCodeDto) {
|
||||
switch (dto.type) {
|
||||
case 'login':
|
||||
switch (dto.targetType) {
|
||||
case 'phone':
|
||||
return this.verificationService.sendPhoneCode(dto.phone, dto.type);
|
||||
case 'email':
|
||||
return this.verificationService.sendEmailCode(dto.email, dto.type);
|
||||
default:
|
||||
throw new BadRequestException('不支持的目标类型');
|
||||
}
|
||||
default:
|
||||
throw new BadRequestException('不支持的验证码类型');
|
||||
@Post('send')
|
||||
async sendVerificationCode(@Body() dto: SendVerificationCodeDto) {
|
||||
switch (dto.type) {
|
||||
case 'login':
|
||||
switch (dto.targetType) {
|
||||
case 'phone':
|
||||
return this.verificationService.sendPhoneCode(dto.phone, dto.type);
|
||||
case 'email':
|
||||
return this.verificationService.sendEmailCode(dto.email, dto.type);
|
||||
default:
|
||||
throw new BadRequestException('不支持的目标类型');
|
||||
}
|
||||
default:
|
||||
throw new BadRequestException('不支持的验证码类型');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,4 @@ import { NotificationModule } from 'src/notification/notification.module';
|
||||
exports: [VerificationService],
|
||||
imports: [NotificationModule],
|
||||
})
|
||||
export class VerificationModule { }
|
||||
export class VerificationModule {}
|
||||
|
||||
@@ -3,97 +3,96 @@ import { NotificationService } from 'src/notification/notification.service';
|
||||
|
||||
@Injectable()
|
||||
export class VerificationService {
|
||||
private readonly logger = new Logger(VerificationService.name);
|
||||
|
||||
private readonly logger = new Logger(VerificationService.name);
|
||||
constructor(private readonly notificationService: NotificationService) {}
|
||||
|
||||
constructor(
|
||||
private readonly notificationService: NotificationService,
|
||||
) { }
|
||||
|
||||
private pool: Map<string, {
|
||||
code: string;
|
||||
createdAt: number;
|
||||
expiredAt: number;
|
||||
tryCount: number;
|
||||
maxTryCount: number;
|
||||
}> = new Map();
|
||||
|
||||
async sendPhoneCode(phone: string, type: 'login') {
|
||||
const key = `phone:${phone}:${type}`;
|
||||
// 检测是否在冷却时间内
|
||||
// TODO
|
||||
|
||||
// 生成验证码
|
||||
const code = this.generateCode();
|
||||
this.logger.log(`Phone[${phone}] code: ${code}`);
|
||||
|
||||
// 发送验证码
|
||||
// await this.notificationService.sendSMS(phone, type, code);
|
||||
// 存储验证码
|
||||
this.saveCode(key, code);
|
||||
return true;
|
||||
private pool: Map<
|
||||
string,
|
||||
{
|
||||
code: string;
|
||||
createdAt: number;
|
||||
expiredAt: number;
|
||||
tryCount: number;
|
||||
maxTryCount: number;
|
||||
}
|
||||
> = new Map();
|
||||
|
||||
async sendEmailCode(email: string, type: 'login') {
|
||||
const key = `email:${email}:${type}`;
|
||||
// 检测是否在冷却时间内
|
||||
// TODO
|
||||
async sendPhoneCode(phone: string, type: 'login') {
|
||||
const key = `phone:${phone}:${type}`;
|
||||
// 检测是否在冷却时间内
|
||||
// TODO
|
||||
|
||||
// 生成验证码
|
||||
const code = this.generateCode();
|
||||
this.logger.log(`Email[${email}] code: ${code}`);
|
||||
// 发送验证码
|
||||
// TODO
|
||||
// 生成验证码
|
||||
const code = this.generateCode();
|
||||
this.logger.log(`Phone[${phone}] code: ${code}`);
|
||||
|
||||
// 存储验证码
|
||||
this.saveCode(key, code);
|
||||
return true;
|
||||
// 发送验证码
|
||||
// await this.notificationService.sendSMS(phone, type, code);
|
||||
// 存储验证码
|
||||
this.saveCode(key, code);
|
||||
return true;
|
||||
}
|
||||
|
||||
async sendEmailCode(email: string, type: 'login') {
|
||||
const key = `email:${email}:${type}`;
|
||||
// 检测是否在冷却时间内
|
||||
// TODO
|
||||
|
||||
// 生成验证码
|
||||
const code = this.generateCode();
|
||||
this.logger.log(`Email[${email}] code: ${code}`);
|
||||
// 发送验证码
|
||||
// TODO
|
||||
|
||||
// 存储验证码
|
||||
this.saveCode(key, code);
|
||||
return true;
|
||||
}
|
||||
|
||||
private saveCode(key: string, code: string) {
|
||||
this.pool.set(key, {
|
||||
code: code,
|
||||
createdAt: Date.now(),
|
||||
expiredAt: Date.now() + 10 * 60 * 1000, // 10分钟过期
|
||||
tryCount: 0,
|
||||
maxTryCount: 5,
|
||||
});
|
||||
}
|
||||
|
||||
verifyPhoneCode(phone: string, code: string, type: 'login') {
|
||||
const key = `phone:${phone}:${type}`;
|
||||
return this.verifyCode(key, code);
|
||||
}
|
||||
|
||||
verifyEmailCode(email: string, code: string, type: 'login') {
|
||||
const key = `email:${email}:${type}`;
|
||||
return this.verifyCode(key, code);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns 0: 验证码正确, -1: 验证码不存在或已过期, -2: 验证码错误, -3: 超过最大尝试次数
|
||||
*/
|
||||
private verifyCode(key: string, code: string) {
|
||||
const data = this.pool.get(key);
|
||||
if (!data) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
private saveCode(key: string, code: string) {
|
||||
this.pool.set(key, {
|
||||
code: code,
|
||||
createdAt: Date.now(),
|
||||
expiredAt: Date.now() + 10 * 60 * 1000, // 10分钟过期
|
||||
tryCount: 0,
|
||||
maxTryCount: 5,
|
||||
});
|
||||
if (data.tryCount >= data.maxTryCount) {
|
||||
return -3;
|
||||
}
|
||||
|
||||
verifyPhoneCode(phone: string, code: string, type: 'login') {
|
||||
const key = `phone:${phone}:${type}`;
|
||||
return this.verifyCode(key, code);
|
||||
if (data.expiredAt < Date.now()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
verifyEmailCode(email: string, code: string, type: 'login') {
|
||||
const key = `email:${email}:${type}`;
|
||||
return this.verifyCode(key, code);
|
||||
if (data.code !== code) {
|
||||
data.tryCount++;
|
||||
return -2;
|
||||
}
|
||||
this.pool.delete(key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns 0: 验证码正确, -1: 验证码不存在或已过期, -2: 验证码错误, -3: 超过最大尝试次数
|
||||
*/
|
||||
private verifyCode(key: string, code: string) {
|
||||
const data = this.pool.get(key);
|
||||
if (!data) {
|
||||
return -1;
|
||||
}
|
||||
if (data.tryCount >= data.maxTryCount) {
|
||||
return -3;
|
||||
}
|
||||
if (data.expiredAt < Date.now()) {
|
||||
return -1;
|
||||
}
|
||||
if (data.code !== code) {
|
||||
data.tryCount++;
|
||||
return -2;
|
||||
}
|
||||
this.pool.delete(key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
private generateCode() {
|
||||
return Math.floor(100000 + Math.random() * 900000).toString();
|
||||
}
|
||||
private generateCode() {
|
||||
return Math.floor(100000 + Math.random() * 900000).toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user