using System; using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Shentun.Peis.EntityFrameworkCore; using Shentun.Peis.MultiTenancy; using Volo.Abp.AspNetCore.Mvc.UI.Theme.LeptonXLite; using Volo.Abp.AspNetCore.Mvc.UI.Theme.LeptonXLite.Bundling; using Microsoft.OpenApi.Models; using OpenIddict.Validation.AspNetCore; using Volo.Abp; using Volo.Abp.Account; using Volo.Abp.Account.Web; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.Autofac; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.Swashbuckle; using Volo.Abp.UI.Navigation.Urls; using Volo.Abp.VirtualFileSystem; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using System.Reflection; using Swashbuckle.AspNetCore.SwaggerGen; using Microsoft.AspNetCore.Mvc.Filters; using Volo.Abp.AspNetCore.Mvc.Validation; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Identity; using Microsoft.Extensions.FileProviders; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Volo.Abp.Settings; using Volo.Abp.Identity.Settings; using Microsoft.AspNetCore.Identity; using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Microsoft.AspNetCore.Authentication; using Nito.Disposables.Internals; using Volo.Abp.AspNetCore.Mvc.ExceptionHandling; using static IdentityModel.ClaimComparer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Shentun.Peis.Filter; using Shentun.Peis.VirtualPath; using System.Text.Encodings.Web; using System.Text.Unicode; using Volo.Abp.Json; using Shentun.Utilities; using Volo.Abp.SecurityLog; using Volo.Abp.Auditing; using Volo.Abp.BackgroundWorkers.Hangfire; using Hangfire; using Volo.Abp.BackgroundWorkers; using Shentun.Peis.Schedulers; using System.Threading.Tasks; using Shentun.Peis.ThirdInterfaces; using OpenIddict.Server; using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using System.Security.Cryptography; using Microsoft.AspNetCore.DataProtection; using Volo.Abp.OpenIddict; using System.Security.Cryptography.X509Certificates; using Shentun.Peis.Controllers; using Hangfire.Redis; namespace Shentun.Peis; [DependsOn( typeof(PeisHttpApiModule), typeof(AbpAutofacModule), typeof(AbpAspNetCoreMultiTenancyModule), typeof(PeisApplicationModule), typeof(PeisEntityFrameworkCoreModule), typeof(AbpAspNetCoreMvcUiLeptonXLiteThemeModule), typeof(AbpAccountWebOpenIddictModule), typeof(AbpAspNetCoreSerilogModule), typeof(AbpSwashbuckleModule), typeof(AbpBackgroundWorkersHangfireModule) )] public class PeisHttpApiHostModule : AbpModule { public override void PreConfigureServices(ServiceConfigurationContext context) { //自定义DataProtection路径 context.Services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(context.Services.GetHostingEnvironment().WebRootPath)); //关闭开发证书 PreConfigure(options => { options.AddDevelopmentEncryptionAndSigningCertificate = false; }); PreConfigure(builder => { builder.AddValidation(options => { options.AddAudiences("Peis"); options.UseLocalServer(); options.UseAspNetCore(); }); }); //重写定义token失效时间 接口返回的是秒 PreConfigure(builder => { //builder.SetAccessTokenLifetime(TimeSpan.FromHours(8)).SetRefreshTokenLifetime(TimeSpan.FromDays(15)); builder.SetAccessTokenLifetime(TimeSpan.FromDays(30)).SetRefreshTokenLifetime(TimeSpan.FromDays(60)); //导入自定义证书,低版本windows要用openssl1.1.1生成 builder.AddEncryptionCertificate(new X509Certificate2(File.ReadAllBytes(context.Services.GetHostingEnvironment().WebRootPath + "\\encryption-certificate.pfx"), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet)); builder.AddSigningCertificate(new X509Certificate2(File.ReadAllBytes(context.Services.GetHostingEnvironment().WebRootPath + "\\signing-certificate.pfx"), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet)); }); } public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); var hostingEnvironment = context.Services.GetHostingEnvironment(); ConfigureAuthentication(context); ConfigureBundles(); ConfigureUrls(configuration); ConfigureConventionalControllers(); ConfigureLocalization(); ConfigureVirtualFileSystem(context); ConfigureCors(context, configuration); ConfigureSwaggerServices(context, configuration); ConfigureJsonOptions(); //全局配置api返回值中的日期默认格式 //context.Services.AddControllers().AddJsonOptions(configure => //{ // configure.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter()); // configure.JsonSerializerOptions.PropertyNameCaseInsensitive = true; //}); //密码策略配置 context.Services.Configure(opt => { opt.Password.RequireDigit = false; opt.Password.RequireLowercase = false; opt.Password.RequireUppercase = false; opt.Password.RequireNonAlphanumeric = false; opt.Password.RequiredLength = 1; }); //context.Services.Configure(opt => //{ // opt.MaxRequestBodySize = 52428800; //}); //context.Services.Configure(opt => //{ // opt.Limits.MaxRequestBodySize = 52428800; //}); #region 临时去掉日志 //关闭审计日志 Configure(options => { options.IsEnabled = configuration.GetValue("IsAuditingLog", true); }); //关闭安全日志 Configure(options => { options.IsEnabled = configuration.GetValue("IsSecurityLog", true); }); #endregion //防伪令牌 //context.Services.Configure(opt => //{ // opt.AutoValidate = false; //}); context.Services.AddMvc(options => { var filterMetadata = options.Filters.FirstOrDefault(x => x is ServiceFilterAttribute attribute && attribute.ServiceType.Equals(typeof(AbpValidationActionFilter))); options.Filters.Remove(filterMetadata); options.Filters.Add(typeof(ValidateFilter)); options.Filters.Add(typeof(CustomerExceptionFilterAttribute)); options.Filters.Add(typeof(CustomerActionFilterAttribute)); // options.Filters.ReplaceOne(x => (x as ServiceFilterAttribute)?.ServiceType?.Name == nameof(ExceptionFilterAttribute), new ServiceFilterAttribute(typeof(ABCExceptionFilterAttribute))); // options.Filters.ReplaceOne(x => (x as ServiceFilterAttribute)?.ServiceType?.Name == nameof(AbpExceptionFilter), new ServiceFilterAttribute(typeof(MyExceptionFilter))); }); //context.Services.TryAddTransient(); ///解除https限制 context.Services.AddOpenIddict() .AddServer(option => { string issuerBase = configuration["AuthServer:IssuerBase"]; if (!string.IsNullOrWhiteSpace(issuerBase)) option.SetIssuer(new Uri(issuerBase)); option.UseAspNetCore().DisableTransportSecurityRequirement(); option.AllowCustomFlow("mini_program"); option.AllowCustomFlow("phone_verify"); }); context.Services.AddTransient(); ////虚拟目录 //context.Services.AddSingleton(new MyFileProvider(configuration["VirtualPath:RealPath"], configuration["VirtualPath:Alias"])); ////Pacs虚拟目录 //context.Services.AddSingleton(new MyFileProvider(configuration["PacsVirtualPath:RealPath"], configuration["PacsVirtualPath:Alias"])); /* Configure(options => { options.ConventionalControllers .Create(typeof(PeisApplicationModule).Assembly, opts => { opts.RootPath = "api/app"; opts.UrlControllerNameNormalizer = (controller) => { return controller.ControllerName; }; opts.UrlActionNameNormalizer = (action) => { return action.Action.ActionName; }; }); }); */ //后台计划任务 ConfigureHangfire(context, configuration); } /// /// 全局转换日期格式 /// private void ConfigureJsonOptions() { //context.Services.AddControllers().AddJsonOptions(options => //{ // options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); // options.JsonSerializerOptions.PropertyNamingPolicy = null; //}); Configure(x => { //x.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); x.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter()); x.JsonSerializerOptions.PropertyNameCaseInsensitive = true; }); Configure(x => { x.DefaultDateTimeFormat = "yyyy-MM-dd HH:mm:ss"; }); } private void ConfigureAuthentication(ServiceConfigurationContext context) { context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme); } private void ConfigureBundles() { Configure(options => { options.StyleBundles.Configure( LeptonXLiteThemeBundles.Styles.Global, bundle => { bundle.AddFiles("/global-styles.css"); } ); }); } private void ConfigureUrls(IConfiguration configuration) { Configure(options => { options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"].Split(',')); options.Applications["Angular"].RootUrl = configuration["App:ClientUrl"]; options.Applications["Angular"].Urls[AccountUrlNames.PasswordReset] = "account/reset-password"; }); } private void ConfigureVirtualFileSystem(ServiceConfigurationContext context) { var hostingEnvironment = context.Services.GetHostingEnvironment(); if (hostingEnvironment.IsDevelopment()) { Configure(options => { options.FileSets.ReplaceEmbeddedByPhysical( Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Shentun.Peis.Domain.Shared")); options.FileSets.ReplaceEmbeddedByPhysical( Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Shentun.Peis.Domain")); options.FileSets.ReplaceEmbeddedByPhysical( Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Shentun.Peis.Application.Contracts")); options.FileSets.ReplaceEmbeddedByPhysical( Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Shentun.Peis.Application")); }); } } private void ConfigureConventionalControllers() { #region 配置隐藏api Configure(options => { options.Conventions.Add(new ApplicationDescription()); }); #endregion Configure(options => { options.ConventionalControllers.Create(typeof(PeisApplicationModule).Assembly); }); } private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddAbpSwaggerGenWithOAuth( configuration["AuthServer:Authority"], new Dictionary { {"Peis", "Peis API"} }, options => { //options.SwaggerDoc("v1", new OpenApiInfo { Title = "Peis API", Version = "v1" }); //options.DocInclusionPredicate((docName, description) => true); //options.CustomSchemaIds(type => type.FullName); options.SwaggerDoc("Work", new OpenApiInfo { Title = "业务API", Version = "V1", Description = "业务API" }); options.SwaggerDoc("Sys", new OpenApiInfo { Title = "底层API", Version = "V1", Description = "底层API" }); options.DocInclusionPredicate((docName, description) => { if (!description.TryGetMethodInfo(out MethodInfo method)) { return false; } /*使用ApiExplorerSettingsAttribute里面的GroupName进行特性标识 * DeclaringType只能获取controller上的特性 * 我们这里是想以action的特性为主 * */ var version = method.DeclaringType.GetCustomAttributes(true).OfType().Select(m => m.GroupName); if (version.Any()) { if (version.Any(v => v == docName)) //选择了sys,并且分组不为User return true; } else { if (docName == "Sys") { return true; } } //这里获取action的特性 var actionVersion = method.GetCustomAttributes(true).OfType().Select(m => m.GroupName); if (actionVersion.Any()) { return actionVersion.Any(v => v == docName); } return false; }); }); } private void ConfigureLocalization() { Configure(options => { options.Languages.Add(new LanguageInfo("ar", "ar", "العربية")); options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština")); options.Languages.Add(new LanguageInfo("en", "en", "English")); options.Languages.Add(new LanguageInfo("en-GB", "en-GB", "English (UK)")); options.Languages.Add(new LanguageInfo("fi", "fi", "Finnish")); options.Languages.Add(new LanguageInfo("fr", "fr", "Français")); options.Languages.Add(new LanguageInfo("hi", "hi", "Hindi", "in")); options.Languages.Add(new LanguageInfo("is", "is", "Icelandic", "is")); options.Languages.Add(new LanguageInfo("it", "it", "Italiano", "it")); options.Languages.Add(new LanguageInfo("hu", "hu", "Magyar")); options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português")); options.Languages.Add(new LanguageInfo("ro-RO", "ro-RO", "Română")); options.Languages.Add(new LanguageInfo("ru", "ru", "Русский")); options.Languages.Add(new LanguageInfo("sk", "sk", "Slovak")); options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe")); options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文")); options.Languages.Add(new LanguageInfo("de-DE", "de-DE", "Deutsch", "de")); options.Languages.Add(new LanguageInfo("es", "es", "Español", "es")); options.Languages.Add(new LanguageInfo("el", "el", "Ελληνικά")); }); } private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddCors(options => { options.AddDefaultPolicy(builder => { builder .WithOrigins( configuration["App:CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToArray() ) .WithAbpExposedHeaders() .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); //添加swagger中文注释 context.Services.AddSwaggerGen(options => { //var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var baseDirectory = AppContext.BaseDirectory; //Serilog.Log.Information("swagger地址1:" + baseDirectory); //var baseDirectory2 = Path.GetDirectoryName(typeof(Program).Assembly.Location); //Serilog.Log.Information("swagger地址2:" + baseDirectory2); //var baseDirectory3 = AppDomain.CurrentDomain.BaseDirectory; //Serilog.Log.Information("swagger地址3:" + baseDirectory3); var commentsFile = Path.Combine(baseDirectory, "Shentun.Peis.Application.xml"); options.IncludeXmlComments(commentsFile, true); var commentsFileDro = Path.Combine(baseDirectory, "Shentun.Peis.Application.Contracts.xml"); options.IncludeXmlComments(commentsFileDro, true); var commentsFileapi = Path.Combine(baseDirectory, "Shentun.Peis.HttpApi.xml"); options.IncludeXmlComments(commentsFileapi, true); }); } private void ConfigureHangfire(ServiceConfigurationContext context, IConfiguration configuration) { //context.Services.AddHangfire(config => //{ // config.UsePostgreSqlStorage(configuration.GetConnectionString("Default")); //}); context.Services.AddHangfire(config => { config.UseRedisStorage(configuration["Hangfire:ConnectionStrings"], new RedisStorageOptions { Db = string.IsNullOrWhiteSpace(configuration["Hangfire:Db"]) ? 0 : Convert.ToInt32(configuration["Hangfire:Db"]) }); }); } public override async void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); var configuration = context.GetConfiguration(); //请求错误提示配置 // app.UseErrorHandling(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseAbpRequestLocalization(); if (!env.IsDevelopment()) { app.UseErrorPage(); } //配置是否启用任务面板 var IsEnabledDashboard = Convert.ToBoolean(configuration["Hangfire:IsEnabledDashboard"]); var IsEnabledHangfire = Convert.ToBoolean(configuration["Hangfire:IsEnabled"]); if (IsEnabledHangfire && IsEnabledDashboard) { app.UseHangfireDashboard("/hangfire", new DashboardOptions { Authorization = new[] { new CustomAuthorizeFilter() } }); } app.UseCorrelationId(); //var staticFile = new StaticFileOptions(); //var filePath = env.ContentRootPath+"UpLoad\\"; ////var filePath = env.ContentRootPath ; //staticFile.FileProvider = new PhysicalFileProvider(filePath); //app.UseStaticFiles(new StaticFileOptions //{ // FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "UpLoad")), // RequestPath="/UpLoad" //}); app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "ReportFile")), RequestPath = "/ReportFile" }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "photo")), RequestPath = "/photo" }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "CloudFilm")), RequestPath = "/CloudFilm" }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "UserPhoto")), RequestPath = "/UserPhoto" }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "UserSign")), RequestPath = "/UserSign" }); //虚拟目录 app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(configuration["VirtualPath:RealPath"]), RequestPath = configuration["VirtualPath:RequestPath"] }); //pacs虚拟目录 app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(configuration["PacsVirtualPath:RealPath"]), RequestPath = configuration["PacsVirtualPath:RequestPath"] }); app.UseRouting(); app.UseCors(); app.UseAuthentication(); app.UseAbpOpenIddictValidation(); if (MultiTenancyConsts.IsEnabled) { app.UseMultiTenancy(); } app.UseUnitOfWork(); app.UseAuthorization(); //app.UseMiddleware(typeof(AuthorizationMiddlewareResultHandler)); //配置是否启用swagger var IsSwagger = Convert.ToBoolean(configuration["Swagger:IsEnabled"]); if (IsSwagger) { app.UseSwagger(); app.UseAbpSwaggerUI(c => { //c.SwaggerEndpoint("/swagger/v1/swagger.json", "Peis API"); c.SwaggerEndpoint($"/swagger/Work/swagger.json", "业务API"); //分组显示 c.SwaggerEndpoint($"/swagger/Sys/swagger.json", "底层API"); //分组显示 //c.RoutePrefix = string.Empty; // url 中不显示swagger var configuration = context.ServiceProvider.GetRequiredService(); c.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); c.OAuthScopes("Peis"); c.DefaultModelExpandDepth(-1); }); } app.UseAuditing(); app.UseAbpSerilogEnrichers(); app.UseConfiguredEndpoints(); if (IsEnabledHangfire) { //任务计划 await StartScheduler(context); } } private async Task StartScheduler(ApplicationInitializationContext context) { //await context.AddBackgroundWorkerAsync(); //RecurringJob.AddOrUpdate("收费接口", o => o.DoWorkAsync(new Guid("")), CheckedBills, TimeZoneInfo.Local); //BackgroundJob.Enqueue(x => x.DoWorkWithArgAsync(new Guid(""))); var appServiceHelper = new AppServiceHelper(); await appServiceHelper.LoginAsync(); var ThirdInterfaceDtos = await appServiceHelper.CallAppServiceAsync>("api/app/ThirdInterface/GetList", null); foreach (var thirdInterfaceDto in ThirdInterfaceDtos) { if (thirdInterfaceDto.IsActive != 'Y') { continue; } var parmValue = thirdInterfaceDto.ParmValue; if (!string.IsNullOrWhiteSpace(parmValue)) { var configurationBuilder = new ConfigurationBuilder() .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); IConfigurationRoot interfaceConfig; try { interfaceConfig = configurationBuilder.Build(); } catch (Exception ex) { continue; } var isActive = interfaceConfig.GetSection("Interface").GetSection("Scheduler") .GetSection("IsActive").Value; var corn = interfaceConfig.GetSection("Interface").GetSection("Scheduler") .GetSection("Corn").Value; if (isActive == "Y") { if (thirdInterfaceDto.ThirdInterfaceType == "02") { RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } else if (thirdInterfaceDto.ThirdInterfaceType == "03") { RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } else if (thirdInterfaceDto.ThirdInterfaceType == "04") { RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } else if (thirdInterfaceDto.ThirdInterfaceType == "05") { RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } else if (thirdInterfaceDto.ThirdInterfaceType == "06") { RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } else if (thirdInterfaceDto.ThirdInterfaceType == "08") { //心电图 RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } else if (thirdInterfaceDto.ThirdInterfaceType == "09") { //同步组合项目价格 RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } else if (thirdInterfaceDto.ThirdInterfaceType == "14") { //导入外检检验结果 RecurringJob.AddOrUpdate(thirdInterfaceDto.DisplayName, o => o.DoWork(thirdInterfaceDto.Id), corn, TimeZoneInfo.Local); } } } } } }