|
@@ -1,15 +1,10 @@
|
|
|
using Hotline.Application.ExportExcel;
|
|
|
using Hotline.Share.Dtos.Order;
|
|
|
-using Hotline.Share.Dtos.Snapshot;
|
|
|
using Hotline.Share.Tools;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
-using Microsoft.AspNetCore.Routing;
|
|
|
-using MongoDB.Bson.IO;
|
|
|
using SqlSugar;
|
|
|
using System.ComponentModel;
|
|
|
using System.Reflection;
|
|
|
-using System.Text.Json;
|
|
|
-using XF.Domain.Dependency;
|
|
|
using XF.Domain.Exceptions;
|
|
|
|
|
|
namespace Hotline.Api.Controllers.ExportData;
|
|
@@ -40,28 +35,25 @@ public class ExportDataController : BaseController
|
|
|
var fullPath = HttpContext.Request.Path.Value;
|
|
|
var originalPath = fullPath?.Substring(0, fullPath.LastIndexOf("/export_excel")).TrimStart('/');
|
|
|
if (string.IsNullOrEmpty(originalPath))
|
|
|
- throw UserFriendlyException.SameMessage("Invalid export path.");
|
|
|
+ throw UserFriendlyException.SameMessage("无效的URL地址" + fullPath);
|
|
|
|
|
|
var matchingEndpoint = _endpointDataSource.Endpoints
|
|
|
.OfType<RouteEndpoint>()
|
|
|
- .FirstOrDefault(endpoint => endpoint.RoutePattern.RawText == originalPath);
|
|
|
-
|
|
|
- if (matchingEndpoint == null)
|
|
|
- throw UserFriendlyException.SameMessage($"No route matches '{originalPath}'.");
|
|
|
+ .FirstOrDefault(endpoint => endpoint.RoutePattern.RawText == originalPath)
|
|
|
+ ?? throw UserFriendlyException.SameMessage($"根据URL查询路由失败: {originalPath}");
|
|
|
|
|
|
var controllerName = matchingEndpoint.RoutePattern.RequiredValues["controller"]?.ToString();
|
|
|
var actionName = matchingEndpoint.RoutePattern.RequiredValues["action"]?.ToString();
|
|
|
|
|
|
- var applicationServiceType = GetApplicationServiceType(controllerName);
|
|
|
- if (applicationServiceType == null)
|
|
|
- throw UserFriendlyException.SameMessage($"Application service for '{controllerName}' not found.");
|
|
|
+ var applicationServiceType = GetApplicationServiceType(controllerName)
|
|
|
+ ?? throw UserFriendlyException.SameMessage($"根据名称查找方法失败. '{controllerName}'");
|
|
|
|
|
|
var method = applicationServiceType.GetMethod(actionName);
|
|
|
if (method == null)
|
|
|
{
|
|
|
method = applicationServiceType.GetMethod(actionName + "Async");
|
|
|
if (method == null)
|
|
|
- throw UserFriendlyException.SameMessage($"Action '{actionName}' not found in Application service.");
|
|
|
+ throw UserFriendlyException.SameMessage($"根据方法名查找方法失败. '{actionName}'");
|
|
|
}
|
|
|
|
|
|
var serviceInstance = _serviceProvider.GetService(applicationServiceType);
|
|
@@ -69,7 +61,7 @@ public class ExportDataController : BaseController
|
|
|
{
|
|
|
serviceInstance = _serviceProvider.GetService(applicationServiceType.GetInterfaces()[0]);
|
|
|
if (serviceInstance == null)
|
|
|
- throw UserFriendlyException.SameMessage($"Unable to create instance of Application service '{applicationServiceType.Name}'.");
|
|
|
+ throw UserFriendlyException.SameMessage($"获取注入失败. '{applicationServiceType.Name}'.");
|
|
|
}
|
|
|
|
|
|
try
|
|
@@ -137,35 +129,11 @@ public class ExportDataController : BaseController
|
|
|
|
|
|
private Type GetApplicationServiceType(string controllerName)
|
|
|
{
|
|
|
- // 根据约定规则,将 Controller 映射到 Application 服务类
|
|
|
- // 假设 Application 服务类命名规则为 "{ControllerName}Application"
|
|
|
- var applicationServiceName = $"Hotline.Application.Snapshot.{controllerName}Application";
|
|
|
var name = controllerName + "Application";
|
|
|
-
|
|
|
var type = AppDomain.CurrentDomain.GetAssemblies().ToList()
|
|
|
.SelectMany(d => d.GetTypes())
|
|
|
.Where(d => d.Name == name)
|
|
|
.First();
|
|
|
return type;
|
|
|
}
|
|
|
-
|
|
|
- private async Task<object[]> BindMethodParameters(System.Reflection.MethodInfo method)
|
|
|
- {
|
|
|
- // 动态解析方法的参数绑定
|
|
|
- var parameters = method.GetParameters();
|
|
|
- var result = new List<object>();
|
|
|
-
|
|
|
- foreach (var param in parameters)
|
|
|
- {
|
|
|
- using var reader = new StreamReader(HttpContext.Request.Body);
|
|
|
- var body = await reader.ReadToEndAsync();
|
|
|
- var genericType = typeof(ExportExcelDto<>).MakeGenericType(param.ParameterType);
|
|
|
- var deserializedObject = body.FromJson(genericType);
|
|
|
- var queryDto = genericType.GetProperty("QueryDto")?.GetValue(deserializedObject);
|
|
|
- result.Add(queryDto);
|
|
|
- }
|
|
|
-
|
|
|
- return result.ToArray();
|
|
|
- }
|
|
|
-
|
|
|
}
|