feat: 二维码访问功能,统一端点管道增强,端点迁移至 Services 层
- 新增二维码生成端点,自动检测局域网 IP,前端扫一扫即可打开网站 - 提取 IApiResponse 接口,ServiceRequestBinder 支持强类型请求 DTO 绑定 - FileStream 端点迁移至 AppEndpoints 统一注册,管道支持 FileStreamResponse 原始文件返回 - 文件库端点全面使用 MapGet<TService, TRequest> 泛型注册 - 移除 Avalonia-API/Extensions 中的业务端点文件,统一由 Services 层管理
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
using Avalonia_Common.Core;
|
||||
using Avalonia_EFCore.Database;
|
||||
using Avalonia_EFCore.Models;
|
||||
using Avalonia_Services.Core;
|
||||
using Avalonia_Services.Services;
|
||||
using Avalonia_Services.Services.FileLibrary;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Avalonia_Services.Services.QrCode;
|
||||
|
||||
namespace Avalonia_Services.Endpoints
|
||||
{
|
||||
@@ -35,24 +32,12 @@ namespace Avalonia_Services.Endpoints
|
||||
});
|
||||
|
||||
// ---- 业务端点注册 ----
|
||||
// 天气预报(从数据库读取)
|
||||
endpoints.MapGet("api/wData", GetWeatherForecastsAsync)
|
||||
.WithOpenApi("Weather", "获取天气预报信息。")
|
||||
.WithName("GetWeatherForecast");
|
||||
|
||||
// 获取用户(演示从数据库查询)
|
||||
endpoints.MapGet("api/getUser", GetUserFromDatabaseAsync)
|
||||
.WithName("GetUser");
|
||||
|
||||
// 处理数据(POST — 演示参数处理)
|
||||
endpoints.MapPost("api/processData", ProcessDataAsync)
|
||||
.WithName("ProcessData");
|
||||
|
||||
endpoints.MapGet<IFileLibraryEndpointService>("api/library/drives", (service, ctx) => service.GetDrivesAsync(ctx))
|
||||
.WithOpenApi("FileLibrary", "查询服务器磁盘。")
|
||||
.WithName("GetLibraryDrives");
|
||||
|
||||
endpoints.MapGet<IFileLibraryEndpointService>("api/library/directories", (service, ctx) => service.GetDirectoriesAsync(ctx))
|
||||
endpoints.MapGet<IFileLibraryEndpointService, DirectoryQueryRequest>("api/library/directories", (service, request, _) => service.GetDirectoriesAsync(request))
|
||||
.WithOpenApi("FileLibrary", "查询服务器目录。")
|
||||
.WithName("GetLibraryDirectories");
|
||||
|
||||
@@ -60,34 +45,42 @@ namespace Avalonia_Services.Endpoints
|
||||
.WithOpenApi("FileLibrary", "查询文件库目录。")
|
||||
.WithName("GetLibraryRoots");
|
||||
|
||||
endpoints.MapPost<IFileLibraryEndpointService>("api/library/roots", (service, ctx) => service.AddRootAsync(ctx))
|
||||
endpoints.MapPost<IFileLibraryEndpointService, AddLibraryRootRequest>("api/library/roots", (service, request, _) => service.AddRootAsync(request))
|
||||
.WithOpenApi("FileLibrary", "添加文件库目录。")
|
||||
.WithName("AddLibraryRoot");
|
||||
|
||||
endpoints.MapPost<IFileLibraryEndpointService>("api/library/roots/enabled", (service, ctx) => service.SetRootEnabledAsync(ctx))
|
||||
endpoints.MapPost<IFileLibraryEndpointService, UpdateLibraryRootRequest>("api/library/roots/enabled", (service, request, _) => service.SetRootEnabledAsync(request))
|
||||
.WithOpenApi("FileLibrary", "启用或禁用文件库目录。")
|
||||
.WithName("SetLibraryRootEnabled");
|
||||
|
||||
endpoints.MapPost<IFileLibraryEndpointService>("api/library/roots/delete", (service, ctx) => service.DeleteRootAsync(ctx))
|
||||
endpoints.MapPost<IFileLibraryEndpointService, DeleteLibraryRootRequest>("api/library/roots/delete", (service, request, _) => service.DeleteRootAsync(request))
|
||||
.WithOpenApi("FileLibrary", "删除文件库目录。")
|
||||
.WithName("DeleteLibraryRoot");
|
||||
|
||||
endpoints.MapPost<IFileLibraryEndpointService>("api/library/roots/scan", (service, ctx) => service.ScanRootAsync(ctx))
|
||||
endpoints.MapPost<IFileLibraryEndpointService, ScanLibraryRootRequest>("api/library/roots/scan", (service, request, _) => service.ScanRootAsync(request))
|
||||
.WithOpenApi("FileLibrary", "立即扫描文件库目录。")
|
||||
.WithName("ScanLibraryRoot");
|
||||
|
||||
endpoints.MapGet<IFileLibraryEndpointService>("api/files", (service, ctx) => service.SearchFilesAsync(ctx))
|
||||
endpoints.MapGet<IFileLibraryEndpointService, SearchFilesRequest>("api/files", (service, request, _) => service.SearchFilesAsync(request))
|
||||
.WithOpenApi("FileLibrary", "分页查询已扫描文件。")
|
||||
.WithName("SearchFiles");
|
||||
|
||||
endpoints.MapGet<IFileLibraryEndpointService>("api/files/detail", (service, ctx) => service.GetFileAsync(ctx))
|
||||
endpoints.MapGet<IFileLibraryEndpointService, FileQueryRequest>("api/files/detail", (service, request, _) => service.GetFileAsync(request))
|
||||
.WithOpenApi("FileLibrary", "查询文件详情。")
|
||||
.WithName("GetFileDetail");
|
||||
|
||||
endpoints.MapGet<IFileLibraryEndpointService>("api/files/text", (service, ctx) => service.GetTextPreviewAsync(ctx))
|
||||
endpoints.MapGet<IFileLibraryEndpointService, FileQueryRequest>("api/files/text", (service, request, _) => service.GetTextPreviewAsync(request))
|
||||
.WithOpenApi("FileLibrary", "预览文本文件。")
|
||||
.WithName("GetTextPreview");
|
||||
|
||||
endpoints.MapGet("api/files/stream", GetFileStreamAsync)
|
||||
.WithOpenApi("FileLibrary", "流式传输文件(支持 Range 请求)。")
|
||||
.WithName("StreamManagedFile");
|
||||
|
||||
endpoints.MapGet<IQrCodeService>("api/qrcode", (service, ctx) => service.GenerateQrCodeAsync(ctx))
|
||||
.WithOpenApi("Utility", "生成局域网访问二维码。")
|
||||
.WithName("GetQrCode");
|
||||
|
||||
// ---- 需要鉴权的端点示例 ----
|
||||
// endpoints.MapGet("api/admin/dashboard", AdminDashboardAsync)
|
||||
// .WithName("AdminDashboard")
|
||||
@@ -101,86 +94,16 @@ namespace Avalonia_Services.Endpoints
|
||||
|
||||
#region 业务处理方法
|
||||
|
||||
/// <summary>
|
||||
/// 从数据库查询天气预报(优先数据库,回退到内存生成)。
|
||||
/// </summary>
|
||||
private static async Task<object?> GetWeatherForecastsAsync(ServiceEndpointContext ctx)
|
||||
private static async Task<object?> GetFileStreamAsync(ServiceEndpointContext ctx)
|
||||
{
|
||||
var sp = ctx.Items["ServiceProvider"] as IServiceProvider;
|
||||
var service = sp?.GetService(typeof(IFileStreamService)) as IFileStreamService;
|
||||
if (service is null) return null;
|
||||
|
||||
// 尝试从数据库读取
|
||||
if (sp?.GetService(typeof(AppDataContext)) is AppDataContext db)
|
||||
{
|
||||
var dbForecasts = await db.WeatherForecasts
|
||||
.OrderByDescending(f => f.Date)
|
||||
.Take(5)
|
||||
.ToListAsync();
|
||||
if (!int.TryParse(ctx.Query.GetValueOrDefault("id"), out var id) || id <= 0)
|
||||
return null;
|
||||
|
||||
if (dbForecasts.Count > 0)
|
||||
{
|
||||
return ResponseHelper.Ok(dbForecasts, "获取天气预报成功(来自数据库)");
|
||||
}
|
||||
}
|
||||
|
||||
// 回退:内存生成(数据库为空时)
|
||||
var service = sp?.GetService(typeof(WeatherForecastService)) as WeatherForecastService
|
||||
?? new WeatherForecastService();
|
||||
|
||||
var forecasts = service.GetWeatherForecasts();
|
||||
return ResponseHelper.Ok(forecasts, "获取天气预报成功(内存生成)");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从数据库获取用户信息(演示数据库查询),若无数据则返回演示用户。
|
||||
/// </summary>
|
||||
/// <param name="ctx">服务端点上下文。</param>
|
||||
/// <returns>用户信息。</returns>
|
||||
private static async Task<object?> GetUserFromDatabaseAsync(ServiceEndpointContext ctx)
|
||||
{
|
||||
var sp = ctx.Items["ServiceProvider"] as IServiceProvider;
|
||||
|
||||
// 尝试从数据库读取用户
|
||||
if (sp?.GetService(typeof(AppDataContext)) is AppDataContext db)
|
||||
{
|
||||
var users = await db.Set<UserEntity>().Take(1).ToListAsync();
|
||||
if (users.Count > 0)
|
||||
{
|
||||
return ResponseHelper.Ok(users[0], "获取用户成功(来自数据库)");
|
||||
}
|
||||
}
|
||||
|
||||
// 回退:演示数据
|
||||
await Task.Delay(100);
|
||||
var user = new { id = 1, name = "张三", email = "zhangsan@example.com" };
|
||||
return ResponseHelper.Ok(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理前端发送的数据(POST 演示),将数据存入数据库或转为大写返回。
|
||||
/// </summary>
|
||||
/// <param name="ctx">服务端点上下文。</param>
|
||||
/// <returns>处理结果。</returns>
|
||||
private static async Task<object?> ProcessDataAsync(ServiceEndpointContext ctx)
|
||||
{
|
||||
var sp = ctx.Items["ServiceProvider"] as IServiceProvider;
|
||||
|
||||
// 演示:将收到的数据存入数据库
|
||||
var input = ctx.Body ?? string.Empty;
|
||||
if (sp?.GetService(typeof(AppDataContext)) is AppDataContext db && !string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
var forecast = new WeatherForecastEntity
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now),
|
||||
TemperatureC = 20,
|
||||
Summary = input,
|
||||
};
|
||||
db.WeatherForecasts.Add(forecast);
|
||||
await db.SaveChangesAsync();
|
||||
return ResponseHelper.Ok(forecast, "数据已存入数据库");
|
||||
}
|
||||
|
||||
await Task.Delay(200);
|
||||
return ResponseHelper.Ok(new { input, processed = input.ToUpperInvariant() });
|
||||
return await service.GetFileStreamAsync(id);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -16,17 +16,17 @@ namespace Avalonia_Services.Endpoints
|
||||
{
|
||||
builder.ConfigureEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapPost<IApiAuthEndpointService>("api/auth/login", (service, ctx) => service.LoginAsync(ctx))
|
||||
endpoints.MapPost<IApiAuthEndpointService, ApiLoginRequest>("api/auth/login", (service, request, ctx) => service.LoginAsync(request, ctx))
|
||||
.WithName("ApiLogin")
|
||||
.WithOpenApi("Auth", "API 登录,返回 access token 和 refresh token。", "", typeof(ApiLoginRequest), typeof(AuthTokenResponse))
|
||||
.ApiOnly();
|
||||
|
||||
endpoints.MapPost<IApiAuthEndpointService>("api/auth/refresh", (service, ctx) => service.RefreshAsync(ctx))
|
||||
endpoints.MapPost<IApiAuthEndpointService, ApiRefreshTokenRequest>("api/auth/refresh", (service, request, ctx) => service.RefreshAsync(request, ctx))
|
||||
.WithName("ApiRefresh")
|
||||
.WithOpenApi("Auth", "API refresh token 轮换。", "", typeof(ApiRefreshTokenRequest), typeof(AuthTokenResponse))
|
||||
.ApiOnly();
|
||||
|
||||
endpoints.MapPost<IApiAuthEndpointService>("api/auth/logout", (service, ctx) => service.LogoutAsync(ctx))
|
||||
endpoints.MapPost<IApiAuthEndpointService, ApiLogoutRequest>("api/auth/logout", (service, request, ctx) => service.LogoutAsync(request, ctx))
|
||||
.WithName("ApiLogout")
|
||||
.WithOpenApi("Auth", "API 退出登录并吊销 refresh token。", "", typeof(ApiLogoutRequest))
|
||||
.ApiOnly();
|
||||
@@ -41,17 +41,17 @@ namespace Avalonia_Services.Endpoints
|
||||
{
|
||||
builder.ConfigureEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapPost<IPcAuthEndpointService>("api/pc/auth/authorize", (service, ctx) => service.AuthorizeAsync(ctx))
|
||||
endpoints.MapPost<IPcAuthEndpointService, PcAuthorizeRequest>("api/pc/auth/authorize", (service, request, ctx) => service.AuthorizeAsync(request, ctx))
|
||||
.WithName("PcAuthorize")
|
||||
.WithOpenApi("Auth", "PC 授权码登录,生成本地全局 token。", "", typeof(PcAuthorizeRequest), typeof(PcTokenResponse))
|
||||
.PcOnly();
|
||||
|
||||
endpoints.MapPost<IPcAuthEndpointService>("api/pc/auth/refresh", (service, ctx) => service.RefreshAsync(ctx))
|
||||
endpoints.MapPost<IPcAuthEndpointService, PcRefreshRequest>("api/pc/auth/refresh", (service, request, ctx) => service.RefreshAsync(request, ctx))
|
||||
.WithName("PcRefresh")
|
||||
.WithOpenApi("Auth", "PC 全局 token 刷新。", "", typeof(PcRefreshRequest), typeof(PcTokenResponse))
|
||||
.PcOnly();
|
||||
|
||||
endpoints.MapPost<IPcAuthEndpointService>("api/pc/auth/logout", (service, ctx) => service.LogoutAsync(ctx))
|
||||
endpoints.MapPost<IPcAuthEndpointService, PcLogoutRequest>("api/pc/auth/logout", (service, request, ctx) => service.LogoutAsync(request, ctx))
|
||||
.WithName("PcLogout")
|
||||
.WithOpenApi("Auth", "PC 退出登录。", "", typeof(PcLogoutRequest))
|
||||
.PcOnly();
|
||||
|
||||
Reference in New Issue
Block a user