Commit c9fa7831 authored by mingyard's avatar mingyard

feat:swagger

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