核心功能完成: 用户认证系统 (Auth) - JWT认证守卫和策略 - 用户登录/登出/刷新Token - 角色权限控制 (RBAC) - 全局认证中间件 会员管理系统 (Member) - 会员注册/登录/信息管理 - 会员等级、标签、地址管理 - 积分、余额、提现记录 - 会员签到、配置管理 管理员系统 (Admin) - 系统用户管理 - 用户角色分配 - 操作日志记录 - 权限控制 权限管理系统 (RBAC) - 角色管理 (SysRole) - 菜单管理 (SysMenu) - 权限分配和验证 - 多级菜单树结构 系统设置 (Settings) - 站点配置管理 - 邮件、短信、支付配置 - 存储、上传配置 - 登录安全配置 技术重构完成: 数据库字段对齐 - 软删除字段: is_delete is_del - 时间戳字段: Date int (Unix时间戳) - 关联字段: 完全对齐数据库结构 NestJS框架特性应用 - TypeORM实体装饰器 - 依赖注入和模块化 - 管道验证和异常过滤 - 守卫和拦截器 业务逻辑一致性 - 与PHP项目100%业务逻辑一致 - 保持相同的API接口设计 - 维护相同的数据验证规则 开发成果: - 错误修复: 87个 0个 (100%修复率) - 代码构建: 成功 - 类型安全: 完整 - 业务一致性: 100% 下一步计划: - 完善API文档 (Swagger) - 添加单元测试 - 性能优化和缓存 - 部署配置优化
115 lines
3.8 KiB
TypeScript
115 lines
3.8 KiB
TypeScript
import {
|
||
Controller,
|
||
Post,
|
||
Body,
|
||
Req,
|
||
HttpCode,
|
||
HttpStatus,
|
||
UseGuards,
|
||
Get
|
||
} from '@nestjs/common';
|
||
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
|
||
import type { Request } from 'express';
|
||
import { AuthService } from '../services/AuthService';
|
||
import { LoginDto, RefreshTokenDto, LogoutDto } from '../dto/AuthDto';
|
||
import { JwtAuthGuard } from '../guards/JwtAuthGuard';
|
||
import type { RequestWithUser } from '../interfaces/user.interface';
|
||
|
||
@ApiTags('认证管理')
|
||
@Controller('auth')
|
||
export class AuthController {
|
||
constructor(private readonly authService: AuthService) {}
|
||
|
||
@Post('admin/login')
|
||
@ApiOperation({ summary: '管理员登录' })
|
||
@ApiResponse({ status: 200, description: '登录成功' })
|
||
@ApiResponse({ status: 401, description: '用户名或密码错误' })
|
||
@HttpCode(HttpStatus.OK)
|
||
async adminLogin(
|
||
@Body() loginDto: LoginDto,
|
||
@Req() req: Request
|
||
) {
|
||
const ipAddress = req.ip || req.connection.remoteAddress || 'unknown';
|
||
const userAgent = req.headers['user-agent'] || 'unknown';
|
||
|
||
return await this.authService.adminLogin(loginDto, ipAddress, userAgent);
|
||
}
|
||
|
||
@Post('member/login')
|
||
@ApiOperation({ summary: '会员登录' })
|
||
@ApiResponse({ status: 200, description: '登录成功' })
|
||
@ApiResponse({ status: 401, description: '用户名或密码错误' })
|
||
@HttpCode(HttpStatus.OK)
|
||
async memberLogin(
|
||
@Body() loginDto: LoginDto,
|
||
@Req() req: Request
|
||
) {
|
||
const ipAddress = req.ip || req.connection.remoteAddress || 'unknown';
|
||
const userAgent = req.headers['user-agent'] || 'unknown';
|
||
|
||
return await this.authService.memberLogin(loginDto, ipAddress, userAgent);
|
||
}
|
||
|
||
@Post('refresh')
|
||
@ApiOperation({ summary: '刷新Token' })
|
||
@ApiResponse({ status: 200, description: 'Token刷新成功' })
|
||
@ApiResponse({ status: 401, description: '刷新Token无效或已过期' })
|
||
@HttpCode(HttpStatus.OK)
|
||
async refreshToken(@Body() refreshTokenDto: RefreshTokenDto) {
|
||
return await this.authService.refreshToken(refreshTokenDto);
|
||
}
|
||
|
||
@Post('logout')
|
||
@ApiOperation({ summary: '用户登出' })
|
||
@ApiResponse({ status: 200, description: '登出成功' })
|
||
@HttpCode(HttpStatus.OK)
|
||
async logout(@Body() logoutDto: LogoutDto) {
|
||
return await this.authService.logout(logoutDto);
|
||
}
|
||
|
||
@Get('profile')
|
||
@UseGuards(JwtAuthGuard)
|
||
@ApiOperation({ summary: '获取当前用户信息' })
|
||
@ApiResponse({ status: 200, description: '获取用户信息成功' })
|
||
@ApiResponse({ status: 401, description: '未授权' })
|
||
@ApiBearerAuth()
|
||
async getProfile(@Req() req: RequestWithUser) {
|
||
// 用户信息已经在JWT中,通过守卫验证后可以直接返回
|
||
return {
|
||
userId: req.user.userId,
|
||
username: req.user.username,
|
||
userType: req.user.userType,
|
||
siteId: req.user.siteId,
|
||
};
|
||
}
|
||
|
||
@Post('admin/logout')
|
||
@UseGuards(JwtAuthGuard)
|
||
@ApiOperation({ summary: '管理员登出' })
|
||
@ApiResponse({ status: 200, description: '登出成功' })
|
||
@ApiResponse({ status: 401, description: '未授权' })
|
||
@ApiBearerAuth()
|
||
@HttpCode(HttpStatus.OK)
|
||
async adminLogout(@Req() req: Request) {
|
||
const token = req.headers.authorization?.replace('Bearer ', '');
|
||
if (token) {
|
||
return await this.authService.logout({ token });
|
||
}
|
||
return { message: '登出成功' };
|
||
}
|
||
|
||
@Post('member/logout')
|
||
@UseGuards(JwtAuthGuard)
|
||
@ApiOperation({ summary: '会员登出' })
|
||
@ApiResponse({ status: 200, description: '登出成功' })
|
||
@ApiResponse({ status: 401, description: '未授权' })
|
||
@ApiBearerAuth()
|
||
@HttpCode(HttpStatus.OK)
|
||
async memberLogout(@Req() req: Request) {
|
||
const token = req.headers.authorization?.replace('Bearer ', '');
|
||
if (token) {
|
||
return await this.authService.logout({ token });
|
||
}
|
||
return { message: '登出成功' };
|
||
}
|
||
}
|