Browse Source

跨域

master
boxu.zheng 1 year ago
parent
commit
383373f52b
  1. 8
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs
  2. 1
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/Faster.Zheng.Winin.Application.Contracts.csproj
  3. 76
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs
  4. 24
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/WininWebModule.cs
  5. 11
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/appsettings.json
  6. BIN
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/sdf

8
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
namespace Faster.Zheng.Winin.AppBase; namespace Faster.Zheng.Winin.AppBase;
@ -11,4 +12,11 @@ public interface IZbxBase<TEntity, TEntityDto, TPagedAndSortedResultRequestDto,
{ {
Task<PagedResultDto<TEntityDto>> GetPageListByFilterAsync(SfsRequestInputBase sfsRequestInputBase, Task<PagedResultDto<TEntityDto>> GetPageListByFilterAsync(SfsRequestInputBase sfsRequestInputBase,
bool includeDetails = false, CancellationToken cancellationToken = default); bool includeDetails = false, CancellationToken cancellationToken = default);
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sfsRequestInputBase"></param>
/// <param name="filePath"></param>
Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase);
} }

1
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/Faster.Zheng.Winin.Application.Contracts.csproj

@ -13,6 +13,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Volo.Abp.ObjectExtending" Version="7.2.1" /> <PackageReference Include="Volo.Abp.ObjectExtending" Version="7.2.1" />
<PackageReference Include="Volo.Abp.Account.Application.Contracts" Version="7.2.1" /> <PackageReference Include="Volo.Abp.Account.Application.Contracts" Version="7.2.1" />
<PackageReference Include="Volo.Abp.Identity.Application.Contracts" Version="7.2.1" /> <PackageReference Include="Volo.Abp.Identity.Application.Contracts" Version="7.2.1" />

76
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs

@ -264,44 +264,92 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
/// <summary> /// <summary>
/// 导出Excel /// 导出Excel
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <param name="sfsRequestInputBase"></param>
/// <param name="data"></param>
/// <param name="filePath"></param> /// <param name="filePath"></param>
[HttpPost("api/[controller]/base/export-to-excel")] [HttpPost("api/[controller]/base/export-to-excel")]
public void ExportToExcel<T>(IEnumerable<T> data, string filePath) public virtual async Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase)
{ {
var data= (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items;
var fileStream = new MemoryStream();
IWorkbook workbook = new XSSFWorkbook(); IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1"); ISheet sheet = workbook.CreateSheet("Sheet1");
// 获取泛型参数的类型 // 获取主表和从表的属性
var type = typeof(T); var mainProperties = typeof(TEntityDto).GetProperties();
var properties = type.GetProperties(); var detailProperties = typeof(TEntityDto).GetProperty("Details").PropertyType.GetGenericArguments()[0].GetProperties();
// 创建表头 // 创建表头
IRow headerRow = sheet.CreateRow(0); IRow headerRow = sheet.CreateRow(0);
for (int i = 0; i < properties.Length; i++) for (int i = 0; i < mainProperties.Length; i++)
{
headerRow.CreateCell(i).SetCellValue(mainProperties[i].Name);
}
for (int i = 0; i < detailProperties.Length; i++)
{ {
headerRow.CreateCell(i).SetCellValue(properties[i].Name); headerRow.CreateCell(mainProperties.Length + i).SetCellValue(detailProperties[i].Name);
} }
// 填充数据行 // 填充数据行
int rowIndex = 1; int rowIndex = 1;
foreach (var item in data) foreach (var mainDto in data)
{ {
IRow dataRow = sheet.CreateRow(rowIndex); IRow dataRow = sheet.CreateRow(rowIndex);
for (int i = 0; i < properties.Length; i++)
// 填充主表数据
for (int i = 0; i < mainProperties.Length; i++)
{ {
var value = properties[i].GetValue(item); var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString()); dataRow.CreateCell(i).SetCellValue(value?.ToString());
} }
// 填充从表数据
var detailList = (IEnumerable<object>)mainProperties[0].GetValue(mainDto);
int detailCellIndex = mainProperties.Length;
foreach (var detailItem in detailList)
{
IRow detailRow;
if (rowIndex == 1)
{
// 创建新的行用于显示从表数据
detailRow = sheet.CreateRow(detailCellIndex);
}
else
{
// 获取已创建的行用于显示从表数据
detailRow = sheet.GetRow(detailCellIndex);
}
for (int i = 0; i < detailProperties.Length; i++)
{
var value = detailProperties[i].GetValue(detailItem);
detailRow.CreateCell(mainProperties.Length + i).SetCellValue(value?.ToString());
}
detailCellIndex++;
}
rowIndex++; rowIndex++;
} }
// 保存Excel文件 // 自动调整列宽
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++)
{ {
workbook.Write(fileStream,true); sheet.AutoSizeColumn(i);
} }
// 保存Excel文件到MemoryStream
workbook.Write(fileStream,true);
fileStream.Position = 0;
// 创建FileContentResult返回Excel文件
var fileContentResult = new FileContentResult(fileStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "export.xlsx"
};
await Task.CompletedTask;
return fileContentResult;
} }
#endregion #endregion

24
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/WininWebModule.cs

@ -107,6 +107,8 @@ public class WininWebModule : AbpModule
//向前端返回完整错误日志 //向前端返回完整错误日志
ConfigureExceptionHandling(); ConfigureExceptionHandling();
//配置跨域节点
ConfigureAddCors();
ConfigureSwaggerServices(context.Services); ConfigureSwaggerServices(context.Services);
} }
@ -220,6 +222,10 @@ public class WininWebModule : AbpModule
app.UseCorrelationId(); app.UseCorrelationId();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
//
app.UseCors("CorsPolicy");
app.UseAuthentication(); app.UseAuthentication();
app.UseAbpOpenIddictValidation(); app.UseAbpOpenIddictValidation();
@ -236,6 +242,8 @@ public class WininWebModule : AbpModule
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Winin API"); options.SwaggerEndpoint("/swagger/v1/swagger.json", "Winin API");
}); });
//app.UseAbpSwaggerUI(options => //app.UseAbpSwaggerUI(options =>
//{ //{
// var apiDescriptionGroups = context.ServiceProvider.GetRequiredService<IApiDescriptionGroupCollectionProvider>().ApiDescriptionGroups.Items; // var apiDescriptionGroups = context.ServiceProvider.GetRequiredService<IApiDescriptionGroupCollectionProvider>().ApiDescriptionGroups.Items;
@ -292,5 +300,21 @@ public class WininWebModule : AbpModule
} }
} }
/// <summary>
/// 添加跨域配置
/// </summary>
protected virtual void ConfigureAddCors()
{
var origins = ServiceConfigurationContext.Services.GetConfiguration().GetSection("App:CorsOrigins").Get<string[]>() ?? Array.Empty<string>();
Console.WriteLine($"CORS Origins:{string.Concat(origins)}");
ServiceConfigurationContext.Services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
});
});
}
#endregion #endregion
} }

11
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/appsettings.json

@ -1,12 +1,19 @@
{ {
"App": { "App": {
"SelfUrl": "https://localhost:44392" "SelfUrl": "https://localhost:44392",
//
"CorsOrigins": [
"http://localhost:9527",
"http://localhost:9528"
]
}, },
"ConnectionStrings": { "ConnectionStrings": {
"Default": "Server=.;Database=Faster.Zheng.Winin;uid=sa;pwd=sasa;timeout=6000;Encrypt=False" "Default": "Server=.;Database=Faster.Zheng.Winin;uid=sa;pwd=sasa;timeout=6000;Encrypt=False"
//"Default": "Server=dev.ccwin-in.com,13319;Database=Faster.Zheng.Winin;uid=ccwin-in;pwd=Microsoft@2022;timeout=6000;Encrypt=False;"
}, },
"StringEncryption": { "StringEncryption": {
"DefaultPassPhrase": "Aj66rJI3krHbVhS6" "DefaultPassPhrase": "Aj66rJI3krHbVhS6"
}, },
"AlwaysAllowAuthorization": "True" "AlwaysAllowAuthorization": "True",
"urls": "https://localhost:60069"
} }

BIN
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/sdf

Binary file not shown.
Loading…
Cancel
Save