diff --git a/.gitignore b/.gitignore index 9491a2f..7809aef 100644 --- a/.gitignore +++ b/.gitignore @@ -360,4 +360,7 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd +/.trae/documents/企业级框架搭建计划.md +/.gitignore +/.gitignore diff --git a/.trae/documents/企业级框架搭建计划.md b/.trae/documents/企业级框架搭建计划.md deleted file mode 100644 index 95daead..0000000 --- a/.trae/documents/企业级框架搭建计划.md +++ /dev/null @@ -1,329 +0,0 @@ -# QYZH.InteractiveMagazine 企业级框架搭建计划 - -## 一、技术栈 - -| 技术 | 用途 | -|------|------| -| .NET 8 Web API | 后端框架 | -| MySQL | 主数据库 | -| SugarSql (SqlSugar) | ORM 框架 | -| JWT | 身份认证 | -| RabbitMQ | 消息队列 | -| Redis | 缓存 | -| Serilog | 日志记录 | -| 微信小程序 | 前端客户端 | - ---- - -## 二、项目分层结构 - -``` -QYZH.InteractiveMagazine/ -├── QYZH.InteractiveMagazine.WebApi/ # API 控制器、启动配置 -├── QYZH.InteractiveMagazine.IService/ # 服务接口定义 -├── QYZH.InteractiveMagazine.Service/ # 业务逻辑实现 -├── QYZH.InteractiveMagazine.Repository/ # 数据访问层 -├── QYZH.InteractiveMagazine.Models/ # 实体、DTO、枚举、基础模型 -├── QYZH.InteractiveMagazine.Infrastructure/ # 中间件、JWT、缓存、消息队列等 -├── QYZH.InteractiveMagazine.Common/ # 公共工具类、扩展方法 -└── QYZH.InteractiveMagazine.Test/ # 单元测试(可选) -``` - -### 项目引用关系 - -``` -WebApi → IService, Service, Infrastructure, Common, Models -Service → IService, Repository, Common, Models -Repository → Common, Models -Infrastructure → Common, Models -IService → Common, Models -Common → Models -Models → 不依赖任何其他项目 -``` - ---- - -## 三、实施步骤 - -### 阶段一:创建解决方案与项目结构 - -1. 清理现有项目文件,创建新的解决方案文件 `.sln` -2. 创建以下类库项目: - - `QYZH.InteractiveMagazine.Models` - .NET 8 类库 - - `QYZH.InteractiveMagazine.Common` - .NET 8 类库 - - `QYZH.InteractiveMagazine.Repository` - .NET 8 类库 - - `QYZH.InteractiveMagazine.IService` - .NET 8 类库 - - `QYZH.InteractiveMagazine.Service` - .NET 8 类库 - - `QYZH.InteractiveMagazine.Infrastructure` - .NET 8 类库 -3. 将现有的 `QYZH.InteractiveMagazine` 项目重命名为 `QYZH.InteractiveMagazine.WebApi` -4. 设置项目引用关系 - ---- - -### 阶段二:安装 NuGet 依赖包 - -| 项目 | 依赖包 | -|------|--------| -| **Models** | 无外部依赖 | -| **Common** | Newtonsoft.Json, AutoMapper | -| **Repository** | SqlSugar, MySqlConnector, Microsoft.Extensions.Configuration.Abstractions | -| **IService** | 无外部依赖 | -| **Service** | AutoMapper, Microsoft.Extensions.Logging.Abstractions | -| **Infrastructure** | Microsoft.AspNetCore.Authentication.JwtBearer, System.IdentityModel.Tokens.Jwt, StackExchange.Redis, RabbitMQ.Client, Serilog.AspNetCore, SqlSugar | -| **WebApi** | Microsoft.AspNetCore.OpenApi, Swashbuckle.AspNetCore, Serilog.AspNetCore | - ---- - -### 阶段三:构建基础模型层 (Models) - -#### 3.1 创建基础实体类 `BaseEntity.cs`(仅包含字段定义,不包含具体业务实体) - -#### 3.2 创建响应模型 `BaseResponse.cs` -```csharp -public class BaseResponse -{ - public int Code { get; set; } - public string Message { get; set; } - public T Data { get; set; } - - public static BaseResponse Success(T data, string message = "操作成功") - public static BaseResponse Fail(string message, int code = 500) -} -``` - -#### 3.3 创建分页模型 `PageListModel.cs` 和 `PageQueryModel.cs` - -#### 3.4 创建业务异常类 `BusinessException.cs` - -#### 3.5 创建 JWT 配置模型 `JwtSettings.cs` - -#### 3.6 创建 RabbitMQ 配置模型 `RabbitMQSettings.cs` - -#### 3.7 创建 Redis 配置模型 `RedisSettings.cs` - ---- - -### 阶段四:构建公共工具层 (Common) - -#### 4.1 创建雪花 ID 生成器 `SnowflakeIdHelper.cs` - -#### 4.2 创建扩展方法类: -- `ObjectExtension.cs` - 对象拷贝、深拷贝等 -- `DateTimeExtension.cs` - 日期时间扩展(格式转换、时间戳转换、年龄计算等) -- `StringExtension.cs` - 字符串扩展(脱敏、MD5、SHA256、Base64、正则校验等) -- `EnumExtension.cs` - 枚举扩展(描述获取、值转换、名称列表等) - -#### 4.3 创建公共工具类: -- `JsonHelper.cs` - JSON 序列化/反序列化 -- `HttpHelper.cs` - HTTP 请求工具 -- `ValidateHelper.cs` - 参数校验工具 -- `EnumHelper.cs` - 枚举工具类 - ---- - -### 阶段五:构建数据访问层 (Repository) - -#### 5.1 创建 SqlSugar 数据库上下文封装 `SqlSugarDbContext.cs` -- 连接字符串读取 -- 数据库初始化 -- 软删除全局过滤 - -#### 5.2 创建基础仓储接口 `IBaseRepository` -- 增删改查基础方法 -- 分页查询方法 - -#### 5.3 创建基础仓储实现 `BaseRepository` - ---- - -### 阶段六:构建基础设施层 (Infrastructure) - -#### 6.1 JWT 认证模块 -- `JwtHelper.cs` - JWT Token 生成/验证 -- `JwtServiceExtensions.cs` - JWT 服务注册扩展 - -#### 6.2 Redis 缓存模块 -- `RedisHelper.cs` - Redis 操作封装 -- `CacheServiceExtensions.cs` - 缓存服务注册扩展 - -#### 6.3 RabbitMQ 消息队列模块 -- `RabbitMQPublisher.cs` - 消息发布器 -- `RabbitMQConsumer.cs` - 消息消费者 -- `RabbitMQServiceExtensions.cs` - RabbitMQ 服务注册扩展 - -#### 6.4 全局异常处理中间件 -- `GlobalExceptionMiddleware.cs` - 全局异常捕获与统一响应 - -#### 6.5 JWT 认证过滤器 -- `JwtAuthorizationFilter.cs` - 自定义 JWT 授权过滤 - -#### 6.6 操作日志中间件 -- `OperationLogMiddleware.cs` - 请求日志记录 - -#### 6.7 服务注册扩展 -- `DependencyInjectionExtensions.cs` - 统一服务注册入口 - ---- - -### 阶段七:构建服务层 (IService & Service) - -#### 7.1 创建基础服务接口 `IBaseService` -- 通用业务操作方法 - -#### 7.2 创建基础服务实现 `BaseService` -- 事务管理 -- 业务逻辑封装 - -#### 7.3 创建用户服务示例(演示分层调用) -- `IAuthService.cs` - 认证服务接口 -- `AuthService.cs` - 认证服务实现(登录、注册、Token 刷新) - ---- - -### 阶段八:构建 API 层 (WebApi) - -#### 8.1 创建基础控制器 `BaseController.cs` -- 获取当前用户信息 -- 统一响应返回 - -#### 8.2 创建认证控制器 `AuthController.cs` -- `POST api/auth/login` - 用户登录 -- `POST api/auth/register` - 用户注册 -- `POST api/auth/refreshToken` - Token 刷新 - -#### 8.3 创建健康检查控制器 `HealthController.cs` -- `GET api/health/ping` - 健康检查 - -#### 8.4 配置 Program.cs -- Serilog 日志配置 -- 依赖注入配置 -- 中间件管道配置(日志 → 异常处理 → 认证 → 授权 → 路由) -- JWT 认证配置 -- Swagger 配置 -- CORS 配置 -- RabbitMQ 消费者启动 - -#### 8.5 配置 appsettings.json -- 数据库连接字符串 -- JWT 配置 -- Redis 配置 -- RabbitMQ 配置 -- Serilog 日志配置 - ---- - -### 阶段九:微信小程序支持 - -#### 9.1 创建微信小程序专用服务 -- `IWeChatMiniProgramService.cs` - 微信服务接口 -- `WeChatMiniProgramService.cs` - 微信服务实现 -- 微信登录(code 换取 openid/session_key) -- 用户信息解密 - -#### 9.2 创建微信小程序控制器 -- `WeChatController.cs` - 微信相关 API -- `POST api/wechat/login` - 微信登录 - -#### 9.3 微信小程序配置项 -- AppId、AppSecret 配置 - ---- - -### 阶段十:配置与脚本 - -#### 10.1 创建数据库初始化脚本 -- `init_database.sql` - 创建数据库和基础表 - -#### 10.2 创建 Docker 配置(可选) -- `Dockerfile` -- `docker-compose.yml` - -#### 10.3 创建 .gitignore - -#### 10.4 创建 launchSettings.json 配置 - ---- - -## 四、关键设计决策 - -| 决策点 | 选择 | 原因 | -|--------|------|------| -| ORM 框架 | SqlSugar | 功能全面,API 简洁,支持代码优先 | -| 主键策略 | long 雪花 ID | 分布式友好,高性能 | -| 软删除 | IsDeleted 字段 | 数据安全,可恢复 | -| 认证方式 | JWT | 无状态,适合小程序 | -| 日志框架 | Serilog | 结构化日志,易扩展 | -| 响应格式 | BaseResponse | 统一格式,前端易处理 | - ---- - -## 五、最终项目文件树(核心文件) - -``` -QYZH.InteractiveMagazine/ -├── QYZH.InteractiveMagazine.sln -├── QYZH.InteractiveMagazine.WebApi/ -│ ├── Controllers/ -│ │ ├── BaseController.cs -│ │ ├── AuthController.cs -│ │ ├── HealthController.cs -│ │ └── WeChatController.cs -│ ├── Program.cs -│ ├── appsettings.json -│ └── QYZH.InteractiveMagazine.WebApi.csproj -├── QYZH.InteractiveMagazine.IService/ -│ ├── IBaseService.cs -│ ├── IAuthService.cs -│ └── QYZH.InteractiveMagazine.IService.csproj -├── QYZH.InteractiveMagazine.Service/ -│ ├── BaseService.cs -│ ├── AuthService.cs -│ └── QYZH.InteractiveMagazine.Service.csproj -├── QYZH.InteractiveMagazine.Repository/ -│ ├── IBaseRepository.cs -│ ├── BaseRepository.cs -│ ├── SqlSugarDbContext.cs -│ └── QYZH.InteractiveMagazine.Repository.csproj -├── QYZH.InteractiveMagazine.Models/ -│ ├── Entity/ -│ │ └── BaseEntity.cs -│ ├── Dto/ -│ │ ├── BaseResponse.cs -│ │ ├── PageListModel.cs -│ │ └── PageQueryModel.cs -│ ├── Settings/ -│ │ ├── JwtSettings.cs -│ │ ├── RedisSettings.cs -│ │ └── RabbitMQSettings.cs -│ ├── Common/ -│ │ └── BusinessException.cs -│ └── QYZH.InteractiveMagazine.Models.csproj -├── QYZH.InteractiveMagazine.Infrastructure/ -│ ├── Extensions/ -│ │ └── DependencyInjectionExtensions.cs -│ ├── Middleware/ -│ │ ├── GlobalExceptionMiddleware.cs -│ │ └── OperationLogMiddleware.cs -│ ├── Auth/ -│ │ └── JwtHelper.cs -│ ├── Cache/ -│ │ └── RedisHelper.cs -│ ├── MessageQueue/ -│ │ └── RabbitMQPublisher.cs -│ └── QYZH.InteractiveMagazine.Infrastructure.csproj -├── QYZH.InteractiveMagazine.Common/ -│ ├── Extensions/ -│ │ ├── ObjectExtension.cs -│ │ ├── DateTimeExtension.cs -│ │ ├── StringExtension.cs -│ │ └── EnumExtension.cs -│ ├── Helpers/ -│ │ ├── JsonHelper.cs -│ │ ├── HttpHelper.cs -│ │ ├── ValidateHelper.cs -│ │ ├── EnumHelper.cs -│ │ └── SnowflakeIdHelper.cs -│ └── QYZH.InteractiveMagazine.Common.csproj -└── scripts/ - └── init_database.sql -```