From 0692aaadcc94a10e5e2c951cca3e590e6d726670 Mon Sep 17 00:00:00 2001 From: moyin <244344649@qq.com> Date: Wed, 17 Dec 2025 15:15:53 +0800 Subject: [PATCH] =?UTF-8?q?dto=EF=BC=9A=E5=88=9B=E5=BB=BAAPI=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E6=95=B0=E6=8D=AE=E4=BC=A0=E8=BE=93=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建LoginResponseDto、RegisterResponseDto等响应DTO - 定义标准化的API响应格式 - 添加完整的Swagger类型定义 --- src/business/login/login-response.dto.ts | 267 +++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 src/business/login/login-response.dto.ts diff --git a/src/business/login/login-response.dto.ts b/src/business/login/login-response.dto.ts new file mode 100644 index 0000000..d9f2a18 --- /dev/null +++ b/src/business/login/login-response.dto.ts @@ -0,0 +1,267 @@ +/** + * 登录业务响应数据传输对象 + * + * 功能描述: + * - 定义登录相关API的响应数据结构 + * - 提供Swagger文档生成支持 + * - 确保API响应的数据格式一致性 + * + * @author moyin + * @version 1.0.0 + * @since 2025-12-17 + */ + +import { ApiProperty } from '@nestjs/swagger'; + +/** + * 用户信息响应DTO + */ +export class UserInfoDto { + @ApiProperty({ + description: '用户ID', + example: '1' + }) + id: string; + + @ApiProperty({ + description: '用户名', + example: 'testuser' + }) + username: string; + + @ApiProperty({ + description: '用户昵称', + example: '测试用户' + }) + nickname: string; + + @ApiProperty({ + description: '邮箱地址', + example: 'test@example.com', + required: false + }) + email?: string; + + @ApiProperty({ + description: '手机号码', + example: '+8613800138000', + required: false + }) + phone?: string; + + @ApiProperty({ + description: '头像URL', + example: 'https://example.com/avatar.jpg', + required: false + }) + avatar_url?: string; + + @ApiProperty({ + description: '用户角色', + example: 1 + }) + role: number; + + @ApiProperty({ + description: '创建时间', + example: '2025-12-17T10:00:00.000Z' + }) + created_at: Date; +} + +/** + * 登录响应数据DTO + */ +export class LoginResponseDataDto { + @ApiProperty({ + description: '用户信息', + type: UserInfoDto + }) + user: UserInfoDto; + + @ApiProperty({ + description: '访问令牌', + example: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' + }) + access_token: string; + + @ApiProperty({ + description: '刷新令牌', + example: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...', + required: false + }) + refresh_token?: string; + + @ApiProperty({ + description: '是否为新用户', + example: false, + required: false + }) + is_new_user?: boolean; + + @ApiProperty({ + description: '响应消息', + example: '登录成功' + }) + message: string; +} + +/** + * 登录响应DTO + */ +export class LoginResponseDto { + @ApiProperty({ + description: '请求是否成功', + example: true + }) + success: boolean; + + @ApiProperty({ + description: '响应数据', + type: LoginResponseDataDto, + required: false + }) + data?: LoginResponseDataDto; + + @ApiProperty({ + description: '响应消息', + example: '登录成功' + }) + message: string; + + @ApiProperty({ + description: '错误代码', + example: 'LOGIN_FAILED', + required: false + }) + error_code?: string; +} + +/** + * 注册响应DTO + */ +export class RegisterResponseDto { + @ApiProperty({ + description: '请求是否成功', + example: true + }) + success: boolean; + + @ApiProperty({ + description: '响应数据', + type: LoginResponseDataDto, + required: false + }) + data?: LoginResponseDataDto; + + @ApiProperty({ + description: '响应消息', + example: '注册成功' + }) + message: string; + + @ApiProperty({ + description: '错误代码', + example: 'REGISTER_FAILED', + required: false + }) + error_code?: string; +} + +/** + * GitHub OAuth响应DTO + */ +export class GitHubOAuthResponseDto { + @ApiProperty({ + description: '请求是否成功', + example: true + }) + success: boolean; + + @ApiProperty({ + description: '响应数据', + type: LoginResponseDataDto, + required: false + }) + data?: LoginResponseDataDto; + + @ApiProperty({ + description: '响应消息', + example: 'GitHub登录成功' + }) + message: string; + + @ApiProperty({ + description: '错误代码', + example: 'GITHUB_OAUTH_FAILED', + required: false + }) + error_code?: string; +} + +/** + * 忘记密码响应数据DTO + */ +export class ForgotPasswordResponseDataDto { + @ApiProperty({ + description: '验证码(仅用于演示,实际应用中不应返回)', + example: '123456', + required: false + }) + verification_code?: string; +} + +/** + * 忘记密码响应DTO + */ +export class ForgotPasswordResponseDto { + @ApiProperty({ + description: '请求是否成功', + example: true + }) + success: boolean; + + @ApiProperty({ + description: '响应数据', + type: ForgotPasswordResponseDataDto, + required: false + }) + data?: ForgotPasswordResponseDataDto; + + @ApiProperty({ + description: '响应消息', + example: '验证码已发送,请查收' + }) + message: string; + + @ApiProperty({ + description: '错误代码', + example: 'SEND_CODE_FAILED', + required: false + }) + error_code?: string; +} + +/** + * 通用响应DTO(用于重置密码、修改密码等) + */ +export class CommonResponseDto { + @ApiProperty({ + description: '请求是否成功', + example: true + }) + success: boolean; + + @ApiProperty({ + description: '响应消息', + example: '操作成功' + }) + message: string; + + @ApiProperty({ + description: '错误代码', + example: 'OPERATION_FAILED', + required: false + }) + error_code?: string; +} \ No newline at end of file