|
|
|
|
using System.Reflection;
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
using DS.Module.Core.Filters;
|
|
|
|
|
using DS.Module.Core.Helpers;
|
|
|
|
|
using DS.Module.Core.Modules;
|
|
|
|
|
using DS.Module.Core.Reflection;
|
|
|
|
|
using DS.Module.Jwt;
|
|
|
|
|
using DS.Module.Log;
|
|
|
|
|
using DS.Module.SqlSugar;
|
|
|
|
|
using DS.Module.Swagger;
|
|
|
|
|
using DS.Module.User;
|
|
|
|
|
using DS.WMS.Core.System.Dtos;
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
using FluentValidation.AspNetCore;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.WebApi;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 基础模块
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DSDependsOn(
|
|
|
|
|
typeof(DependencyAppModule),
|
|
|
|
|
typeof(NLogModule),
|
|
|
|
|
typeof(SqlSugarModule),
|
|
|
|
|
typeof(SwaggerModule),
|
|
|
|
|
typeof(JwtModule),
|
|
|
|
|
typeof(UserModule))]
|
|
|
|
|
public class DSAppWebModule : DSAppModule
|
|
|
|
|
{
|
|
|
|
|
private string policyName = string.Empty;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
public override void ConfigureServices(ConfigureServicesContext context)
|
|
|
|
|
{
|
|
|
|
|
var service = context.Services;
|
|
|
|
|
service.AddControllersWithViews(options =>
|
|
|
|
|
{
|
|
|
|
|
// 全局添加自定义模型验证过滤器
|
|
|
|
|
// options.Filters.Add<ModelActionFiter>();
|
|
|
|
|
//全局异常过滤
|
|
|
|
|
options.Filters.Add<GlobalExceptionsFilter>();
|
|
|
|
|
options.SuppressAsyncSuffixInActionNames = false;
|
|
|
|
|
//x.Filters.Add<PermissionAuthorizationFilter>();
|
|
|
|
|
})
|
|
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
//使用自定义模型验证
|
|
|
|
|
options.InvalidModelStateResponseFactory = (context) =>
|
|
|
|
|
{
|
|
|
|
|
var errors = context.ModelState
|
|
|
|
|
.Where(e => e.Value.Errors.Count > 0)
|
|
|
|
|
.Select(e => e.Value.Errors.First().ErrorMessage)
|
|
|
|
|
.ToList();
|
|
|
|
|
var result = DataResult<Object>.Failed(string.Join("|", errors));
|
|
|
|
|
|
|
|
|
|
return new JsonResult(result);
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
//全局配置Json序列化处理
|
|
|
|
|
.AddNewtonsoftJson(options =>
|
|
|
|
|
{
|
|
|
|
|
// 首字母小写(驼峰样式)
|
|
|
|
|
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
|
|
|
// 时间格式化
|
|
|
|
|
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
// 忽略循环引用
|
|
|
|
|
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
|
|
|
// 忽略空值
|
|
|
|
|
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
|
|
|
|
});
|
|
|
|
|
//添加FluentValidation验证
|
|
|
|
|
service.AddFluentValidationAutoValidation();
|
|
|
|
|
//跨域请求
|
|
|
|
|
policyName = AppSetting.Configuration["Cors:PolicyName"];
|
|
|
|
|
var corsUrl = AppSetting.Configuration["Cors:Url"];
|
|
|
|
|
service.AddCors(c =>
|
|
|
|
|
{
|
|
|
|
|
c.AddPolicy(policyName, policy =>
|
|
|
|
|
{
|
|
|
|
|
policy.WithOrigins(corsUrl
|
|
|
|
|
.Split(",", StringSplitOptions.RemoveEmptyEntries).ToArray())
|
|
|
|
|
//policy.WithOrigins("http://localhost:5001")//支持多个域名端口,注意端口号后不要带/斜杆:比如localhost:8000/,是错的
|
|
|
|
|
.AllowAnyHeader().AllowAnyMethod().AllowCredentials(); //允许cookie;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
public override void ApplicationInitialization(ApplicationContext context)
|
|
|
|
|
{
|
|
|
|
|
var applicationBuilder = context.GetApplicationBuilder();
|
|
|
|
|
|
|
|
|
|
if (!policyName.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
applicationBuilder.UseCors(policyName); //添加跨域中间件
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applicationBuilder.UseRouting();
|
|
|
|
|
|
|
|
|
|
applicationBuilder.UseStaticFiles();
|
|
|
|
|
}
|
|
|
|
|
}
|