From 24309f47d2c6deba69b2c5dba1fbe1a06b786ce6 Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Fri, 1 Mar 2024 16:00:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=A3=E9=99=A4=E9=94=81=E5=B1=8Fapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/auth/auth.controller.ts | 11 +++++++++++ src/modules/auth/auth.service.ts | 13 +++++++++++++ src/modules/auth/dto/auth.dto.ts | 9 ++++----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index b3270a4..2922f8e 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -12,6 +12,8 @@ import { LoginDto, RegisterDto } from './dto/auth.dto'; import { LocalGuard } from './guards/local.guard'; import { LoginToken } from './models/auth.model'; import { CaptchaService } from './services/captcha.service'; +import { AuthUser } from './decorators/auth-user.decorator'; +import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'; @ApiTags('Auth - 认证模块') @UseGuards(LocalGuard) @@ -37,6 +39,15 @@ export class AuthController { return { token }; } + @Post('unlock') + @ApiSecurityAuth() + @ApiOperation({ summary: '屏幕解锁,使用密码和token' }) + @ApiResult({ type: LoginToken }) + async unlock(@Body() dto: LoginDto, @AuthUser() user: IAuthUser): Promise { + await this.authService.unlock(user.uid, dto.password); + return true; + } + @Post('register') @ApiOperation({ summary: '注册' }) async register(@Body() dto: RegisterDto): Promise { diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 8f97c97..9526203 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -80,6 +80,19 @@ export class AuthService { return token.accessToken; } + /** + * 解锁屏幕 + * 返回null则账号密码有误,不存在该用户 + */ + async unlock(uid: number, password: string): Promise { + const user = await this.userService.findUserById(uid); + if (isEmpty(user)) throw new BusinessException(ErrorEnum.INVALID_USERNAME_PASSWORD); + + const comparePassword = md5(`${password}${user.psalt}`); + if (user.password !== comparePassword) + throw new BusinessException(ErrorEnum.INVALID_USERNAME_PASSWORD); + } + /** * 效验账号密码 */ diff --git a/src/modules/auth/dto/auth.dto.ts b/src/modules/auth/dto/auth.dto.ts index 8cce9d0..436ed04 100644 --- a/src/modules/auth/dto/auth.dto.ts +++ b/src/modules/auth/dto/auth.dto.ts @@ -1,11 +1,10 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsString, Matches, MaxLength, MinLength } from 'class-validator'; +import { IsOptional, IsString, Matches, MaxLength, MinLength } from 'class-validator'; export class LoginDto { @ApiProperty({ description: '手机号/邮箱' }) - @IsString() - @MinLength(4) + @IsOptional() username: string; @ApiProperty({ description: '密码', example: 'a123456' }) @@ -15,11 +14,11 @@ export class LoginDto { password: string; @ApiProperty({ description: '验证码标识' }) - @IsString() + @IsOptional() captchaId: string; @ApiProperty({ description: '用户输入的验证码' }) - @IsString() + @IsOptional() @MinLength(4) @MaxLength(4) verifyCode: string;