refactor: 重构项目基础架构与实体体系

- 替换原有自定义生命周期接口为通用基础服务体系
- 将所有实体基类替换为带雪花ID的SqlSugarBaseEntity
- 迁移DTO到Models项目并统一管理
- 移除冗余的Repository层实现,改用通用基础服务
- 添加Yitter.IdGenerator雪花ID生成支持
- 重构SqlSugar数据库上下文与依赖注入配置
- 新增微信用户、用户勋章、背包等实体与配套服务
- 整理分页查询与结果封装类
- 优化WebApi控制器结构
This commit is contained in:
glz
2026-06-02 14:10:43 +08:00
parent 831f8ba7f5
commit 8ed012bba8
51 changed files with 1762 additions and 1141 deletions

View File

@ -23,6 +23,11 @@ public class AdminController : BaseController
_logger = logger;
}
/// <summary>
/// 登录
/// </summary>
/// <param name="input">登录输入</param>
/// <returns>登录结果</returns>
[AllowAnonymous]
[HttpPost("login")]
public async Task<BaseResponse<AdminLoginOutput>> LoginAsync([FromBody] AdminLoginInput input)
@ -31,6 +36,10 @@ public class AdminController : BaseController
return Success(result);
}
/// <summary>
/// 登出
/// </summary>
/// <returns>登出结果</returns>
[HttpPost("logout")]
public async Task<BaseResponse<object>> LogoutAsync()
{
@ -44,6 +53,10 @@ public class AdminController : BaseController
return Success(new object(), "登出成功");
}
/// <summary>
/// 获取管理员信息
/// </summary>
/// <returns>管理员信息</returns>
[HttpGet("info")]
public async Task<BaseResponse<AdminUserInfoOutput>> GetAdminInfoAsync()
{
@ -57,6 +70,11 @@ public class AdminController : BaseController
return Success(result);
}
/// <summary>
/// 修改密码
/// </summary>
/// <param name="input">修改密码输入</param>
/// <returns>修改密码结果</returns>
[HttpPost("changePassword")]
public async Task<BaseResponse<object>> ChangePasswordAsync([FromBody] ChangePasswordInput input)
{

View File

@ -0,0 +1,121 @@
using Microsoft.AspNetCore.Mvc;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.IService.Dto;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
[Route("api/[controller]")]
[ApiController]
public class WxUserController : BaseController
{
private readonly IWxUserService _wxUserService;
private readonly ILogger<WxUserController> _logger;
public WxUserController(IWxUserService wxUserService, ILogger<WxUserController> logger)
{
_wxUserService = wxUserService;
_logger = logger;
}
[HttpPost("users")]
public async Task<BaseResponse<WxUserOutput>> CreateUserAsync([FromBody] WxUserInput input)
{
try
{
var result = await _wxUserService.CreateAsync(input);
return Success(result, "创建微信用户成功");
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "创建微信用户业务异常: {Message}", ex.Message);
return BaseResponse<WxUserOutput>.Fail(ex.Message, ex.Code);
}
catch (Exception ex)
{
_logger.LogError(ex, "创建微信用户系统异常,参数:{Input}", input);
return BaseResponse<WxUserOutput>.Fail("创建微信用户失败,请稍后重试", 500);
}
}
[HttpPut("users/{id}")]
public async Task<BaseResponse<WxUserOutput>> UpdateUserAsync(long id, [FromBody] WxUserInput input)
{
try
{
var result = await _wxUserService.UpdateAsync(id, input);
return Success(result, "更新微信用户成功");
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "更新微信用户业务异常: {Message}", ex.Message);
return BaseResponse<WxUserOutput>.Fail(ex.Message, ex.Code);
}
catch (Exception ex)
{
_logger.LogError(ex, "更新微信用户系统异常ID{Id},参数:{Input}", id, input);
return BaseResponse<WxUserOutput>.Fail("更新微信用户失败,请稍后重试", 500);
}
}
[HttpDelete("users/{id}")]
public async Task<BaseResponse<object>> DeleteUserAsync(long id)
{
try
{
await _wxUserService.DeleteAsync(id);
return Success(new object(), "删除微信用户成功");
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "删除微信用户业务异常: {Message}", ex.Message);
return BaseResponse<object>.Fail(ex.Message, ex.Code);
}
catch (Exception ex)
{
_logger.LogError(ex, "删除微信用户系统异常ID{Id}", id);
return BaseResponse<object>.Fail("删除微信用户失败,请稍后重试", 500);
}
}
[HttpGet("users/{id}")]
public async Task<BaseResponse<WxUserOutput>> GetUserByIdAsync(long id)
{
try
{
var result = await _wxUserService.GetByIdAsync(id);
return Success(result);
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "获取微信用户业务异常: {Message}", ex.Message);
return BaseResponse<WxUserOutput>.Fail(ex.Message, ex.Code);
}
catch (Exception ex)
{
_logger.LogError(ex, "获取微信用户系统异常ID{Id}", id);
return BaseResponse<WxUserOutput>.Fail("获取微信用户信息失败,请稍后重试", 500);
}
}
[HttpPost("users/list")]
public async Task<BaseResponse<PageListModel<WxUserOutput>>> GetUsersListAsync([FromBody] WxUserQueryInput input)
{
try
{
var result = await _wxUserService.GetListAsync(input);
return Success(result);
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "查询微信用户列表业务异常: {Message}", ex.Message);
return BaseResponse<PageListModel<WxUserOutput>>.Fail(ex.Message, ex.Code);
}
catch (Exception ex)
{
_logger.LogError(ex, "查询微信用户列表系统异常,参数:{Input}", input);
return BaseResponse<PageListModel<WxUserOutput>>.Fail("查询微信用户列表失败,请稍后重试", 500);
}
}
}

View File

@ -4,21 +4,38 @@ using BCrypt.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi;
using QYZH.InteractiveMagazine.Common.Extensions;
using QYZH.InteractiveMagazine.Common.Helpers;
using QYZH.InteractiveMagazine.Infrastructure.Autofacs;
using QYZH.InteractiveMagazine.Infrastructure.Context;
using QYZH.InteractiveMagazine.Infrastructure.Extensions;
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
using QYZH.InteractiveMagazine.Models.Entity;
using QYZH.InteractiveMagazine.Models.Enum;
using QYZH.InteractiveMagazine.Repository;
using QYZH.InteractiveMagazine.Repository.Core;
using Serilog;
using SqlSugar.IOC;
using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.SwaggerUI;
using QYZH.InteractiveMagazine.Infrastructure.Autofacs;
using System.Text.Json.Serialization;
using Yitter.IdGenerator;
var builder = WebApplication.CreateBuilder(args);
// 初始化雪花ID生成器
YitIdHelper.SetIdGenerator(new IdGeneratorOptions() { WorkerId = 1 });
// autofac注入 允许使用autofac作为DI容器
builder.UseAutofac();
builder.InitSqlSugarDb(new IocConfig()
{
ConfigId = 0,
DbType = IocDbType.MySql,
ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"),
IsAutoCloseConnection = true,
});
// 配置Serilog
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
@ -26,13 +43,22 @@ Log.Logger = new LoggerConfiguration()
.CreateLogger();
builder.Host.UseSerilog();
builder.Services.AddControllers();
builder.Services.AddControllers(options =>
{
options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;//Required 不作为必填
})
.AddJsonOptions(options =>
{
// 配置返回时间格式转换
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
}).ConfigureApiBehaviorOptions(opt => opt.SuppressModelStateInvalidFilter = true);//关闭默认模型验证
builder.Services.AddEndpointsApiExplorer();
// 跨域配置
builder.AddCorsRegister();
//builder.Services.AddSession();
builder.Services.AddHttpClient();
// 注册 Swagger 文档
builder.Services.AddSwaggerGen(option =>
{
@ -88,10 +114,9 @@ builder.Services.AddSwaggerGen(option =>
builder.Services.AddInfrastructureServices(builder.Configuration);
// 初始化SqlSugar
SqlSugarDbContext.Init(builder.Configuration);
//注册 HttpContextAccessor
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped(typeof(BaseRepository<>));
// 添加CORS
builder.Services.AddCors(options =>
@ -118,7 +143,7 @@ var app = builder.Build();
c.DocExpansion(DocExpansion.None); // ->修改界面打开时自动折叠
});
}
app.UseServiceContext();
app.UseHttpsRedirection();
app.UseCors("AllowAll");
app.UseMiddleware<GlobalExceptionMiddleware>();