Commit c9fa7831 authored by mingyard's avatar mingyard

feat:swagger

parent b1dc0400
...@@ -14,7 +14,10 @@ import { ApiResponseInterceptor } from '@/common/interceptor/api.response.interc ...@@ -14,7 +14,10 @@ import { ApiResponseInterceptor } from '@/common/interceptor/api.response.interc
import { ApiExceptionsFilter } from '@/common/filter/api.exception.filter'; import { ApiExceptionsFilter } from '@/common/filter/api.exception.filter';
import { RegisterDto } from './dto/req/register.dto'; import { RegisterDto } from './dto/req/register.dto';
import { Auth } from '@/common/decorators/auth.decorator'; import { Auth } from '@/common/decorators/auth.decorator';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { LoginResDto } from './dto/res/loginRes.dto';
@ApiTags('auth')
@Controller('auth') @Controller('auth')
@UseInterceptors(ApiResponseInterceptor) @UseInterceptors(ApiResponseInterceptor)
@UseFilters(ApiExceptionsFilter) @UseFilters(ApiExceptionsFilter)
...@@ -22,8 +25,14 @@ export class AuthController { ...@@ -22,8 +25,14 @@ export class AuthController {
constructor(private readonly authService: AuthService) {} constructor(private readonly authService: AuthService) {}
// 登录接口 // 登录接口
@ApiOperation({ summary: '用户登录' })
@ApiResponse({ status: 200, description: '登录成功', type: LoginResDto })
@ApiResponse({ status: 401, description: '认证失败' })
@Post('login') @Post('login')
async login(@Body() loginDto: LoginDto, @Req() req: IRequest) { async login(
@Body() loginDto: LoginDto,
@Req() req: IRequest,
): Promise<LoginResDto> {
const record: LoginRecordDto = { const record: LoginRecordDto = {
userId: null, userId: null,
ip: req.ip, ip: req.ip,
...@@ -38,15 +47,30 @@ export class AuthController { ...@@ -38,15 +47,30 @@ export class AuthController {
} }
// 注册接口 // 注册接口
@ApiOperation({ summary: '用户注册' })
@ApiResponse({ status: 201, description: '注册成功', type: LoginResDto })
@ApiResponse({ status: 400, description: '请求参数错误' })
@Post('register') @Post('register')
async register(@Body() registerDto: RegisterDto) { async register(@Body() registerDto: RegisterDto): Promise<LoginResDto> {
return await this.authService.register(registerDto); return await this.authService.register(registerDto);
} }
// 重置密码接口
@ApiOperation({ summary: '用户重置密码' })
@ApiResponse({ status: 200, description: '重置密码成功', type: LoginResDto })
@ApiResponse({ status: 400, description: '请求参数错误' })
@Post('resetPwd')
async resetPwd(@Body() registerDto: RegisterDto): Promise<LoginResDto> {
return await this.authService.resetPwd(registerDto);
}
// 退出登录接口 // 退出登录接口
@ApiOperation({ summary: '用户退出登录' })
@ApiResponse({ status: 200, description: '退出登录成功' })
@ApiResponse({ status: 401, description: '认证失败' })
@Auth() @Auth()
@Post('logout') @Post('logout')
async logout(@Req() req: IRequest) { async logout(@Req() req: IRequest): Promise<void> {
return await this.authService.logout(req.token, req.decodedToken.exp); return await this.authService.logout(req.token, req.decodedToken.exp);
} }
} }
import { IsString, IsNotEmpty, IsPhoneNumber } from 'class-validator'; import { IsString, IsNotEmpty, IsPhoneNumber } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class LoginDto { export class LoginDto {
@ApiProperty({
description: '用户的手机号码',
type: 'string',
example: '13800138000',
})
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
@IsPhoneNumber('CN') @IsPhoneNumber('CN')
phone: string; phone: string;
@ApiProperty({
description: '用户的密码',
type: 'string',
example: '1234test',
})
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
password: string; password: string;
......
import { ApiProperty } from '@nestjs/swagger';
import { import {
IsString, IsString,
IsNotEmpty, IsNotEmpty,
...@@ -7,15 +8,30 @@ import { ...@@ -7,15 +8,30 @@ import {
} from 'class-validator'; } from 'class-validator';
export class RegisterDto { export class RegisterDto {
@ApiProperty({
description: '用户的手机号码',
type: 'string',
example: '13800138000',
})
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
@IsPhoneNumber('CN') @IsPhoneNumber('CN')
phone: string; phone: string;
@ApiProperty({
description: '短信验证码',
type: 'number',
example: 1234,
})
@IsNotEmpty() @IsNotEmpty()
@IsNumber() @IsNumber()
code: number; code: number;
@ApiProperty({
description: '用户的密码',
type: 'string',
example: '1234test',
})
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
@Matches(/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/, { @Matches(/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/, {
......
import { Expose } from 'class-transformer'; import { Expose } from 'class-transformer';
import { UserBaseDto } from '../userBase.dto'; import { UserBaseDto } from '../userBase.dto';
import { ApiProperty } from '@nestjs/swagger';
export class LoginResDto extends UserBaseDto { export class LoginResDto extends UserBaseDto {
@ApiProperty({
description: '用户的登录令牌',
type: 'string',
example: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
})
@Expose() @Expose()
token: string; token: string;
} }
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer'; import { Expose } from 'class-transformer';
export class UserBaseDto { export class UserBaseDto {
@ApiProperty({
description: '用户的id',
type: 'number',
example: 1111,
})
@Expose() @Expose()
id: number; id: number;
@ApiProperty({
description: '用户的账号',
type: 'string',
example: 'test',
})
@Expose() @Expose()
acc: string; acc: string;
@ApiProperty({
description: '用户的手机号码',
type: 'string',
example: '13800138000',
})
@Expose() @Expose()
phone: string; phone: string;
@ApiProperty({
description: '用户的邮箱地址',
type: 'string',
example: 'test@example.com',
})
@Expose() @Expose()
mailAddr: string; mailAddr: string;
@ApiProperty({
description: '用户的最后登录时间',
type: 'number',
example: 1627849200,
})
@Expose() @Expose()
lastLoginTime: number; lastLoginTime: number;
@ApiProperty({
description: '用户的状态',
type: 'number',
example: 1,
})
@Expose() @Expose()
state: number; state: number;
@ApiProperty({
description: '用户的昵称',
type: 'string',
example: 'test_nickname',
required: false,
})
@Expose() @Expose()
nickname?: string; nickname?: string;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment