feat: 优化项目目录结构
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { IsEnum, IsString, Length, ValidateIf } from 'class-validator';
|
||||
|
||||
export class SendVerificationCodeDto {
|
||||
@IsEnum(['phone', 'email'], { message: '请求类型错误' })
|
||||
targetType: 'phone' | 'email';
|
||||
|
||||
@IsEnum(['login'], { message: '请求类型错误' })
|
||||
type: 'login';
|
||||
|
||||
@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;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { VerificationController } from './verification.controller';
|
||||
|
||||
describe('VerificationController', () => {
|
||||
let controller: VerificationController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [VerificationController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<VerificationController>(VerificationController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
34
apps/backend/src/verification/verification.controller.ts
Normal file
34
apps/backend/src/verification/verification.controller.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
Controller,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { SendVerificationCodeDto } from './dto/send-verification-code.dto';
|
||||
import { VerificationService } from './verification.service';
|
||||
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
|
||||
|
||||
@Controller('verification')
|
||||
export class VerificationController {
|
||||
constructor(private readonly verificationService: VerificationService) {}
|
||||
|
||||
@Post('send')
|
||||
@UseGuards(ThrottlerGuard)
|
||||
@Throttle({ default: { limit: 20, ttl: 60000 } })
|
||||
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('不支持的验证码类型');
|
||||
}
|
||||
}
|
||||
}
|
||||
12
apps/backend/src/verification/verification.module.ts
Normal file
12
apps/backend/src/verification/verification.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { VerificationController } from './verification.controller';
|
||||
import { VerificationService } from './verification.service';
|
||||
import { NotificationModule } from 'src/notification/notification.module';
|
||||
|
||||
@Module({
|
||||
controllers: [VerificationController],
|
||||
providers: [VerificationService],
|
||||
exports: [VerificationService],
|
||||
imports: [NotificationModule],
|
||||
})
|
||||
export class VerificationModule {}
|
||||
18
apps/backend/src/verification/verification.service.spec.ts
Normal file
18
apps/backend/src/verification/verification.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { VerificationService } from './verification.service';
|
||||
|
||||
describe('VerificationService', () => {
|
||||
let service: VerificationService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [VerificationService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<VerificationService>(VerificationService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
127
apps/backend/src/verification/verification.service.ts
Normal file
127
apps/backend/src/verification/verification.service.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { NotificationService } from 'src/notification/notification.service';
|
||||
|
||||
@Injectable()
|
||||
export class VerificationService {
|
||||
private readonly logger = new Logger(VerificationService.name);
|
||||
|
||||
constructor(private readonly notificationService: NotificationService) {}
|
||||
|
||||
private pool: Map<
|
||||
string,
|
||||
{
|
||||
code: string;
|
||||
createdAt: number;
|
||||
expiredAt: number;
|
||||
tryCount: number;
|
||||
maxTryCount: number;
|
||||
}
|
||||
> = new Map();
|
||||
|
||||
/**
|
||||
* @deprecated 该方法暂时弃用,因为没有申请到签名
|
||||
*/
|
||||
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);
|
||||
throw new Error('不允许的登陆方式');
|
||||
return true;
|
||||
}
|
||||
|
||||
async sendEmailCode(email: string, type: 'login') {
|
||||
const key = `email:${email}:${type}`;
|
||||
// 检测是否在冷却时间内
|
||||
if (this.isInCooldownPeriod(key)) {
|
||||
throw new BadRequestException('发送过于频繁,请稍后再试');
|
||||
}
|
||||
|
||||
// 生成验证码
|
||||
const code = this.generateCode();
|
||||
// 存储验证码
|
||||
this.saveCode(key, code);
|
||||
this.logger.log(`Email[${email}] code: ${code}`);
|
||||
// 发送验证码
|
||||
await this.notificationService
|
||||
.sendMail({ type: 'login-verify', targetMail: email, code })
|
||||
.catch(() => {
|
||||
this.clearCode(key);
|
||||
throw new BadRequestException('发送失败,请稍后再试');
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private isInCooldownPeriod(key: string) {
|
||||
const item = this.pool.get(key);
|
||||
if (!item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 冷却60秒
|
||||
if (Date.now() - item.createdAt > 60 * 1000) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
private clearCode(key: string) {
|
||||
this.pool.delete(key);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user