From 0a32754740d38ba1c53aa2240aa652bce46af1cd Mon Sep 17 00:00:00 2001
From: glz <694770232@qq.com>
Date: Tue, 2 Jun 2026 17:58:10 +0800
Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AE=8C=E6=88=90=E7=B3=BB?=
=?UTF-8?q?=E7=BB=9F=E5=9F=BA=E7=A1=80=E6=9E=B6=E6=9E=84=E9=87=8D=E6=9E=84?=
=?UTF-8?q?=E4=B8=8E=E4=B8=9A=E5=8A=A1=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
本次提交包含多项核心变更:
1. **用户体系重构**:将管理员用户状态从字符串改为整型枚举,移除微信用户冗余字段Points和GrowthPoints,新增通用基础实体主键配置
2. **代码清理**:删除HealthController、WxUserMedal、WxUserBag等废弃文件,移除WeChatDto中冗余查询字段
3. **响应格式统一**:重构BaseResponse与ResultCode枚举,标准化全局响应格式
4. **权限与验证优化**:添加JWT认证与开发环境免认证逻辑,新增模型验证过滤器,统一控制器认证配置
5. **工具与配置更新**:新增dotnet-tools.json配置EF工具,调整Swagger与压缩中间件配置,优化SqlSugar默认值处理逻辑
6. **业务逻辑简化**:移除AutoMapper映射,改为手动映射DTO以提升性能,修复异常处理与响应返回逻辑
---
.../DependencyInjectionExtensions.cs | 50 +++-
.../Middleware/GlobalExceptionMiddleware.cs | 2 +-
.../Middleware/ModelValidActionFilter.cs | 45 ++++
.../Dto/Admin/AdminUserDto.cs | 4 +-
.../Dto/Admin/AuthDto.cs | 2 +-
.../Dto/BaseResponse.cs | 224 ++++++++++++++----
.../Dto/WeChat/WeChatDto.cs | 19 --
.../Entity/AdminUser.cs | 6 -
.../Entity/SqlSugarBaseEntity.cs | 7 +-
.../Entity/User .cs | 58 +++++
.../Entity/{WxUserBag.cs => UserBag.cs} | 6 +-
.../Entity/{WxUserMedal.cs => UserMedal.cs} | 6 +-
.../Entity/WxUser.cs | 12 -
.../Core/SqlSugarExtension.cs | 4 +-
.../AdminAuthService.cs | 2 +-
.../AdminUserService.cs | 51 ++--
.../WxUserService.cs | 94 +++++---
.../Controllers/AdminController.cs | 23 +-
.../Controllers/BaseController.cs | 9 +-
.../Controllers/HealthController.cs | 22 --
.../Controllers/WxUserController.cs | 52 +++-
QYZH.InteractiveMagazine.WebApi/Program.cs | 46 +++-
.../QYZH.InteractiveMagazine.WebApi.csproj | 3 +-
.../dotnet-tools.json | 13 +
24 files changed, 551 insertions(+), 209 deletions(-)
create mode 100644 QYZH.InteractiveMagazine.Infrastructure/Middleware/ModelValidActionFilter.cs
create mode 100644 QYZH.InteractiveMagazine.Models/Entity/User .cs
rename QYZH.InteractiveMagazine.Models/Entity/{WxUserBag.cs => UserBag.cs} (92%)
rename QYZH.InteractiveMagazine.Models/Entity/{WxUserMedal.cs => UserMedal.cs} (90%)
delete mode 100644 QYZH.InteractiveMagazine.WebApi/Controllers/HealthController.cs
create mode 100644 QYZH.InteractiveMagazine.WebApi/dotnet-tools.json
diff --git a/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs b/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs
index 9a97769..19f6a2e 100644
--- a/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs
+++ b/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs
@@ -1,10 +1,16 @@
+using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
using QYZH.InteractiveMagazine.Models.Settings;
using System.Text;
+using System.Text.Encodings.Web;
namespace QYZH.InteractiveMagazine.Infrastructure.Extensions;
@@ -16,11 +22,12 @@ public static class DependencyInjectionExtensions
///
/// 注册基础设施服务
///
- /// 服务集合
+ /// 服务集合
/// 配置
- public static void AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
+ /// 运行环境
+ public static void AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
{
- AddJwtAuthentication(services, configuration);
+ AddJwtAuthentication(services, configuration, environment);
services.AddTransient();
services.AddTransient();
@@ -32,8 +39,17 @@ public static class DependencyInjectionExtensions
///
/// 服务集合
/// 配置
- private static void AddJwtAuthentication(IServiceCollection services, IConfiguration configuration)
+ /// 运行环境
+ private static void AddJwtAuthentication(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
{
+ // 开发环境下跳过 JWT 验证
+ if (environment?.IsDevelopment() == true)
+ {
+ services.AddAuthentication("NoAuth")
+ .AddScheme("NoAuth", options => { });
+ return;
+ }
+
var jwtSettings = configuration.GetSection("JwtSettings").Get()!;
services.AddSingleton(jwtSettings);
@@ -55,3 +71,29 @@ public static class DependencyInjectionExtensions
});
}
}
+
+///
+/// 开发环境免认证处理器
+///
+public class NoAuthHandler : AuthenticationHandler
+{
+ public NoAuthHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
+ : base(options, logger, encoder, clock)
+ {
+ }
+
+ protected override Task HandleAuthenticateAsync()
+ {
+ // 开发环境下始终认证成功
+ var claims = new[]
+ {
+ new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Name, "DevUser"),
+ new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, "0")
+ };
+ var identity = new System.Security.Claims.ClaimsIdentity(claims, Scheme.Name);
+ var principal = new System.Security.Claims.ClaimsPrincipal(identity);
+ var ticket = new AuthenticationTicket(principal, Scheme.Name);
+
+ return Task.FromResult(AuthenticateResult.Success(ticket));
+ }
+}
diff --git a/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs b/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs
index 4f0bef2..39b8601 100644
--- a/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs
+++ b/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs
@@ -55,7 +55,7 @@ public class GlobalExceptionMiddleware : IMiddleware
context.Response.ContentType = "application/json";
context.Response.StatusCode = StatusCodes.Status400BadRequest;
- var response = BaseResponse