Skip to content

Commit 3c91935

Browse files
committed
feat: 图形验证码
1 parent 90530dc commit 3c91935

File tree

7 files changed

+292
-8
lines changed

7 files changed

+292
-8
lines changed

package-lock.json

Lines changed: 188 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
"class-transformer": "^0.5.1",
3535
"class-validator": "^0.14.0",
3636
"cross-env": "^7.0.3",
37+
"express-session": "^1.17.3",
3738
"ioredis": "^5.3.2",
3839
"lodash": "^4.17.21",
3940
"moment": "^2.29.4",
4041
"mysql2": "^3.6.1",
4142
"reflect-metadata": "^0.1.13",
4243
"rxjs": "^7.8.1",
44+
"svg-captcha": "^1.4.0",
4345
"typeorm": "^0.3.17",
4446
"uuid": "^9.0.1",
4547
"winston": "^3.10.0",
@@ -53,6 +55,7 @@
5355
"@nestjs/schematics": "^10.0.0",
5456
"@nestjs/testing": "^10.0.0",
5557
"@types/express": "^4.17.17",
58+
"@types/express-session": "^1.17.8",
5659
"@types/jest": "^29.5.2",
5760
"@types/lodash": "^4.14.199",
5861
"@types/node": "^20.3.1",

src/api/login/dto/login.dto.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ export class LoginDto {
99
@Length(6, 12, { message: '密码长度必须是6-12位的' })
1010
@IsNotEmpty({ message: '密码不能为空' })
1111
readonly password!: string;
12+
13+
@IsNotEmpty({ message: '验证码不能空' })
14+
readonly captcha!: string;
1215
}

src/api/login/login.controller.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
1-
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
1+
import { Body, Controller, Get, Post, Query, Res, Session } from '@nestjs/common';
22
import { LoginDto } from './dto/login.dto';
33
import { LoginService } from './login.service';
44
import { LoginVo } from './vo/login.vo';
5-
5+
import * as svgCaptcha from 'svg-captcha';
6+
import { HttpException } from '@nestjs/common/exceptions';
7+
import { HttpStatus } from '@nestjs/common/enums';
68
@Controller()
79
export class LoginController {
810
constructor(private readonly loginService: LoginService) {}
911

1012
@Post('login')
11-
async loginApi(@Body() req: LoginDto): Promise<LoginVo> {
12-
return await this.loginService.loginApi(req);
13+
async loginApi(@Body() req: LoginDto, @Session() session: any): Promise<LoginVo> {
14+
const storedCaptcha = session.code;
15+
if (req.captcha && storedCaptcha && req.captcha.toLowerCase() === storedCaptcha.toLowerCase()) {
16+
return await this.loginService.loginApi(req);
17+
} else {
18+
throw new HttpException('验证码错误', HttpStatus.OK);
19+
}
1320
}
1421

1522
@Get('refresh')
1623
async refreshTokenApi(@Query('token') token: string): Promise<LoginVo> {
1724
return await this.loginService.refreshTokenApi(token);
1825
}
26+
27+
@Get('captcha')
28+
getCaptchaApi(@Res() res: any, @Session() session: any) {
29+
const captcha = svgCaptcha.create({
30+
size: 4, //生成几个验证码
31+
fontSize: 50, //文字大小
32+
width: 100, //宽度
33+
height: 34, //高度
34+
background: '#cc9966', //背景颜色
35+
});
36+
console.log(captcha, '??');
37+
session['code'] = captcha['text']; //存储验证码记录到session
38+
res.set('Content-Type', 'image/svg+xml');
39+
res.send(captcha['data']);
40+
}
1941
}

0 commit comments

Comments
 (0)