|
@@ -35,7 +35,7 @@ Console.Title = $"FMGJ OASystem Server";
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
var basePath = AppContext.BaseDirectory;
|
|
var basePath = AppContext.BaseDirectory;
|
|
|
|
|
|
|
|
-//寮曞叆閰嶇疆鏂囦欢
|
|
|
|
|
|
|
+// 导入配置文件
|
|
|
var _config = new ConfigurationBuilder()
|
|
var _config = new ConfigurationBuilder()
|
|
|
.SetBasePath(basePath)
|
|
.SetBasePath(basePath)
|
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
@@ -44,10 +44,10 @@ var _config = new ConfigurationBuilder()
|
|
|
.Build();
|
|
.Build();
|
|
|
builder.Services.AddSingleton(new AppSettingsHelper(_config));
|
|
builder.Services.AddSingleton(new AppSettingsHelper(_config));
|
|
|
|
|
|
|
|
-//璁剧疆璇锋眰鍙傛暟鍙�涓嶅~
|
|
|
|
|
|
|
+// 设置请求参数发生异常
|
|
|
builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
|
|
builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
|
|
|
|
|
|
|
|
-//璁剧疆璇锋眰鍙傛暟閿欒��榛樿�よ繑鍥炴牸寮�
|
|
|
|
|
|
|
+// 设置请求参数错误 默认返回格式
|
|
|
builder.Services.AddControllers()
|
|
builder.Services.AddControllers()
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
{
|
|
{
|
|
@@ -76,28 +76,29 @@ builder.Services.AddControllersWithViews();
|
|
|
builder.Services.AddControllers()
|
|
builder.Services.AddControllers()
|
|
|
.AddJsonOptions(options =>
|
|
.AddJsonOptions(options =>
|
|
|
{
|
|
{
|
|
|
- //绌哄瓧娈典笉鍝嶅簲Response
|
|
|
|
|
|
|
+ // 空字段不响应 Response
|
|
|
//options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|
//options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new NullJsonConverter());
|
|
options.JsonSerializerOptions.Converters.Add(new NullJsonConverter());
|
|
|
|
|
|
|
|
- //鏃堕棿鏍煎紡鍖栧搷搴�
|
|
|
|
|
|
|
+ // 时间格式化响应
|
|
|
options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
|
|
options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
|
|
|
- //decimal 鍥涗綅灏忔暟
|
|
|
|
|
- //options.JsonSerializerOptions.Converters.Add(new DecimalConverter(_decimalPlaces)); // 灏嗕繚鐣欏皬鏁颁綅鏁板弬鏁颁紶閫掔粰鑷�瀹氫箟搴忓垪鍖栧櫒
|
|
|
|
|
|
|
+ // decimal 四位小数
|
|
|
|
|
+ // 保留小数位数参数传递给自定义序列化器
|
|
|
|
|
+ //options.JsonSerializerOptions.Converters.Add(new DecimalConverter(_decimalPlaces));
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
|
|
|
-#region 娣诲姞闄愭祦涓�闂翠欢鏈嶅姟娉ㄥ唽
|
|
|
|
|
|
|
+#region 添加限流中间件服务注册
|
|
|
|
|
|
|
|
-// 娣诲姞鍐呭瓨缂撳瓨锛堥檺娴侀渶瑕侊級
|
|
|
|
|
|
|
+// 添加内存缓存,限流中间件使用
|
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.AddMemoryCache();
|
|
|
|
|
|
|
|
-// 閰嶇疆闄愭祦璁剧疆
|
|
|
|
|
|
|
+// 配置限流设置
|
|
|
builder.Services.Configure<RateLimitConfig>(
|
|
builder.Services.Configure<RateLimitConfig>(
|
|
|
- builder.Configuration.GetSection("RateLimiting"));
|
|
|
|
|
|
|
+builder.Configuration.GetSection("RateLimiting"));
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
#region Gzip
|
|
#region Gzip
|
|
@@ -121,14 +122,14 @@ builder.Services.AddCors(options =>
|
|
|
//policy.AddPolicy("Cors", opt => opt
|
|
//policy.AddPolicy("Cors", opt => opt
|
|
|
// //.SetIsOriginAllowed(origin =>
|
|
// //.SetIsOriginAllowed(origin =>
|
|
|
// //{
|
|
// //{
|
|
|
- // // // 瀹氫箟鍏佽�哥殑鏉ユ簮鍒楄〃
|
|
|
|
|
|
|
+ // // // 定义允许的来源列表
|
|
|
// // var allowedOrigins = new List<string>
|
|
// // var allowedOrigins = new List<string>
|
|
|
// // {
|
|
// // {
|
|
|
// // "http://132.232.92.186:9002",
|
|
// // "http://132.232.92.186:9002",
|
|
|
// // "http://oa.pan-american-intl.com:4399"
|
|
// // "http://oa.pan-american-intl.com:4399"
|
|
|
// // };
|
|
// // };
|
|
|
|
|
|
|
|
- // // // 妫€鏌ヨ�锋眰鐨勬潵婧愭槸鍚﹀湪鍏佽�哥殑鍒楄〃涓�
|
|
|
|
|
|
|
+ // // // 检查请求的来源是否在允许的列表中
|
|
|
// // return allowedOrigins.Contains(origin);
|
|
// // return allowedOrigins.Contains(origin);
|
|
|
// //})
|
|
// //})
|
|
|
|
|
|
|
@@ -150,7 +151,7 @@ builder.Services.AddCors(options =>
|
|
|
});
|
|
});
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 涓婁紶鏂囦欢
|
|
|
|
|
|
|
+#region 上传文件
|
|
|
builder.Services.AddCors(policy =>
|
|
builder.Services.AddCors(policy =>
|
|
|
{
|
|
{
|
|
|
policy.AddPolicy("Cors", opt => opt
|
|
policy.AddPolicy("Cors", opt => opt
|
|
@@ -176,15 +177,17 @@ builder.Services.Configure<KestrelServerOptions>(options =>
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 鎺ュ彛鍒嗙粍
|
|
|
|
|
|
|
+#region 上传文件
|
|
|
|
|
+// 上传文件分组配置:Tuple<分组标识, 分组名称>
|
|
|
var groups = new List<Tuple<string, string>>
|
|
var groups = new List<Tuple<string, string>>
|
|
|
{
|
|
{
|
|
|
- //new Tuple<string, string>("Group1","鍒嗙粍涓€"),
|
|
|
|
|
- //new Tuple<string, string>("Group2","鍒嗙粍浜�")
|
|
|
|
|
|
|
+ // 示例分组(取消注释即可启用)
|
|
|
|
|
+ //new Tuple<string, string>("Group1","分组一"),
|
|
|
|
|
+ //new Tuple<string, string>("Group2","分组二")
|
|
|
};
|
|
};
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 娉ㄥ叆鏁版嵁搴�
|
|
|
|
|
|
|
+#region 接口分组
|
|
|
|
|
|
|
|
#region old
|
|
#region old
|
|
|
|
|
|
|
@@ -207,38 +210,36 @@ builder.Services.AddScoped(options =>
|
|
|
}
|
|
}
|
|
|
, db =>
|
|
, db =>
|
|
|
{
|
|
{
|
|
|
- // SQL鎵ц�屽畬
|
|
|
|
|
|
|
+ // SQL 执行完
|
|
|
db.Aop.OnLogExecuted = (sql, pars) =>
|
|
db.Aop.OnLogExecuted = (sql, pars) =>
|
|
|
{
|
|
{
|
|
|
- // 瓒呰繃1绉�
|
|
|
|
|
|
|
+ // 超过 1 秒
|
|
|
if (db.Ado.SqlExecutionTime.TotalSeconds > 1)
|
|
if (db.Ado.SqlExecutionTime.TotalSeconds > 1)
|
|
|
{
|
|
{
|
|
|
var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName;
|
|
var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName;
|
|
|
- //鎵ц�屽畬浜嗗彲浠ヨ緭鍑篠QL鎵ц�屾椂闂� (OnLogExecutedDelegate)
|
|
|
|
|
|
|
+ // 执行完成可以输出 SQL 执行时间 (OnLogExecutedDelegate)
|
|
|
Console.WriteLine("NowTime:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
Console.WriteLine("NowTime:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
Console.WriteLine("MethodName:" + FirstMethodName);
|
|
Console.WriteLine("MethodName:" + FirstMethodName);
|
|
|
Console.WriteLine("ElapsedTime:" + db.Ado.SqlExecutionTime.ToString());
|
|
Console.WriteLine("ElapsedTime:" + db.Ado.SqlExecutionTime.ToString());
|
|
|
Console.WriteLine("ExecuteSQL:" + sql);
|
|
Console.WriteLine("ExecuteSQL:" + sql);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
- //SQL鎵ц�屽墠
|
|
|
|
|
|
|
+ //
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
{
|
|
{
|
|
|
};
|
|
};
|
|
|
- //SQL鎶ラ敊
|
|
|
|
|
|
|
+ // SQL 执行前
|
|
|
db.Aop.OnError = (exp) =>
|
|
db.Aop.OnError = (exp) =>
|
|
|
{
|
|
{
|
|
|
- //鑾峰彇鍘熺敓SQL鎺ㄨ崘 5.1.4.63 鎬ц兘OK
|
|
|
|
|
|
|
+ // 获取原生 SQL 建议 5.1.4.63 性能 OK
|
|
|
//UtilMethods.GetNativeSql(exp.Sql, exp.Parametres);
|
|
//UtilMethods.GetNativeSql(exp.Sql, exp.Parametres);
|
|
|
- //鑾峰彇鏃犲弬鏁癝QL瀵规€ц兘鏈夊奖鍝嶏紝鐗瑰埆澶х殑SQL鍙傛暟澶氱殑锛岃皟璇曚娇鐢�
|
|
|
|
|
|
|
+ // 获取无参数 SQL 对性能有影响,特别是大的 SQL 参数多的,调试使用
|
|
|
//UtilMethods.GetSqlString(DbType.SqlServer, exp.sql, exp.parameters);
|
|
//UtilMethods.GetSqlString(DbType.SqlServer, exp.sql, exp.parameters);
|
|
|
|
|
|
|
|
};
|
|
};
|
|
|
- //淇�鏀筍QL鍜屽弬鏁扮殑鍊�
|
|
|
|
|
|
|
+ // 修改 SQL 和参数的值
|
|
|
db.Aop.OnExecutingChangeSql = (sql, pars) =>
|
|
db.Aop.OnExecutingChangeSql = (sql, pars) =>
|
|
|
{
|
|
{
|
|
|
- //sql=newsql
|
|
|
|
|
- //foreach(var p in pars) //淇�鏀�
|
|
|
|
|
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
|
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
@@ -249,27 +250,7 @@ builder.Services.AddScoped(options =>
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-//#region Identity 閰嶇疆
|
|
|
|
|
-//builder.Services.AddDataProtection();
|
|
|
|
|
-////涓嶈�佺敤 AddIdentity 锛� AddIdentity 鏄�浜嶮VC妗嗘灦涓�鐨�
|
|
|
|
|
-//builder.Services.AddIdentityCore<User>(opt =>
|
|
|
|
|
-//{
|
|
|
|
|
-// opt.Password.RequireDigit = false; //鏁板瓧
|
|
|
|
|
-// opt.Password.RequireLowercase = false;//灏忓啓瀛楁瘝
|
|
|
|
|
-// opt.Password.RequireNonAlphanumeric = false;//鐗规畩绗﹀彿 渚嬪�� 锟�#@锛�
|
|
|
|
|
-// opt.Password.RequireUppercase = false; //澶у啓瀛楁瘝
|
|
|
|
|
-// opt.Password.RequiredLength = 6;//瀵嗙爜闀垮害 6
|
|
|
|
|
-// opt.Password.RequiredUniqueChars = 1;//鐩稿悓瀛楃�﹀彲浠ュ嚭鐜板嚑娆�
|
|
|
|
|
-// opt.Lockout.MaxFailedAccessAttempts = 5; //鍏佽�告渶澶氳緭鍏ヤ簲娆$敤鎴峰悕/瀵嗙爜閿欒��
|
|
|
|
|
-// opt.Lockout.DefaultLockoutTimeSpan = new TimeSpan(0, 5, 0);//閿佸畾浜斿垎閽�
|
|
|
|
|
-// opt.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider; // 淇�鏀瑰瘑鐮佷娇鐢ㄩ偖浠躲€愰獙璇佺爜妯″紡銆�
|
|
|
|
|
-// opt.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider; ////
|
|
|
|
|
-//});
|
|
|
|
|
-//var idBuilder = new IdentityBuilder(typeof(User), typeof(UserRole), services);
|
|
|
|
|
-//idBuilder.AddEntityFrameworkStores<swapDbContext>().AddDefaultTokenProviders().AddRoleManager<RoleManager<UserRole>>().AddUserManager<UserManager<User>>();
|
|
|
|
|
-//#endregion
|
|
|
|
|
-
|
|
|
|
|
-#region 娉ㄥ叆Swagger娉ㄩ噴(鍚�鐢�)
|
|
|
|
|
|
|
+#region 注入 Swagger 注解 (禁用)
|
|
|
|
|
|
|
|
if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
|
{
|
|
{
|
|
@@ -279,7 +260,7 @@ if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
|
{
|
|
{
|
|
|
Version = "v1",
|
|
Version = "v1",
|
|
|
Title = "Api",
|
|
Title = "Api",
|
|
|
- Description = "Api鎺ュ彛鏂囨。"
|
|
|
|
|
|
|
+ Description = "Api 接口文档"
|
|
|
});
|
|
});
|
|
|
foreach (var item in groups)
|
|
foreach (var item in groups)
|
|
|
{
|
|
{
|
|
@@ -312,7 +293,7 @@ if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
|
}
|
|
}
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 娣诲姞鏍¢獙
|
|
|
|
|
|
|
+#region 添加校验
|
|
|
|
|
|
|
|
builder.Services.AddTransient<OASystemAuthentication>();
|
|
builder.Services.AddTransient<OASystemAuthentication>();
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
@@ -327,7 +308,7 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
ValidAudience = "OASystem.com",
|
|
ValidAudience = "OASystem.com",
|
|
|
ValidIssuer = "OASystem.com",
|
|
ValidIssuer = "OASystem.com",
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"])),
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"])),
|
|
|
- ClockSkew = TimeSpan.FromSeconds(30), //杩囨湡鏃堕棿瀹归敊鍊硷紝瑙e喅鏈嶅姟鍣ㄧ��鏃堕棿涓嶅悓姝ラ棶棰橈紙绉掞級
|
|
|
|
|
|
|
+ ClockSkew = TimeSpan.FromSeconds(30), // 过期时间默认值,解决服务器时间不同步问题(秒)
|
|
|
RequireExpirationTime = true,
|
|
RequireExpirationTime = true,
|
|
|
};
|
|
};
|
|
|
options.Events = new JwtBearerEvents
|
|
options.Events = new JwtBearerEvents
|
|
@@ -335,7 +316,7 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
OnMessageReceived = context =>
|
|
OnMessageReceived = context =>
|
|
|
{
|
|
{
|
|
|
var path = context.HttpContext.Request.Path;
|
|
var path = context.HttpContext.Request.Path;
|
|
|
- //濡傛灉鏄痵ignalr璇锋眰锛岄渶瑕佸皢token杞�瀛橈紝鍚﹀垯JWT鑾峰彇涓嶅埌token銆侽PTIONS璇锋眰闇€瑕佽繃婊ゅ埌锛屽洜涓篛PTIONS璇锋眰鑾峰彇涓嶅埌Token锛岀敤NGINX杩囨护鎺塐PTION璇锋眰.
|
|
|
|
|
|
|
+ // 如果是 signalr 请求,需要将 token 迁移,否则 JWT 获取不到 token。OPTIONS 请求需要过滤到,因为 OPTIONS 请求获取不到 Token,用 NGINX 过滤掉 OPTIONS 请求。
|
|
|
if (path.StartsWithSegments("/ChatHub"))
|
|
if (path.StartsWithSegments("/ChatHub"))
|
|
|
{
|
|
{
|
|
|
string accessToken = context.Request.Query["access_token"].ToString();
|
|
string accessToken = context.Request.Query["access_token"].ToString();
|
|
@@ -352,10 +333,10 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 鍒濆�嬪寲鏃ュ織
|
|
|
|
|
|
|
+#region 初始化日志
|
|
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
- //涓嶈�板綍瀹氭椂璁块棶API
|
|
|
|
|
|
|
+ // 不记录定时访问API
|
|
|
.Filter.ByIncludingOnly(logEvent =>
|
|
.Filter.ByIncludingOnly(logEvent =>
|
|
|
{
|
|
{
|
|
|
if (logEvent.Properties.TryGetValue("RequestPath", out var pathValue))
|
|
if (logEvent.Properties.TryGetValue("RequestPath", out var pathValue))
|
|
@@ -373,22 +354,20 @@ Log.Logger = new LoggerConfiguration()
|
|
|
.WriteTo.File(Path.Combine("Logs", @"Log.txt"), rollingInterval: RollingInterval.Day)
|
|
.WriteTo.File(Path.Combine("Logs", @"Log.txt"), rollingInterval: RollingInterval.Day)
|
|
|
.CreateLogger();
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
-//
|
|
|
|
|
|
|
+#region 出入境费用明细 专用记录器
|
|
|
|
|
|
|
|
-#region 鍑哄叆澧冭垂鐢ㄦ槑缁� 涓撶敤璁板綍鍣�
|
|
|
|
|
-
|
|
|
|
|
-// 鎸囧畾纾佺洏缁濆�硅矾寰勶紙绀轰緥锛欴鐩樼殑AppLogs鏂囦欢澶癸級
|
|
|
|
|
|
|
+// 出入境费用明细 专用记录器
|
|
|
var logDirectory = @"D:\OASystem\Logs\EnterExitCost";
|
|
var logDirectory = @"D:\OASystem\Logs\EnterExitCost";
|
|
|
|
|
|
|
|
-// 鑷�鍔ㄥ垱寤虹洰褰曪紙濡傛灉涓嶅瓨鍦�锛�
|
|
|
|
|
|
|
+// 自动创建目录(如果不存在)
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
Directory.CreateDirectory(logDirectory);
|
|
Directory.CreateDirectory(logDirectory);
|
|
|
- Log.Information($"鏃ュ織鐩�褰曞凡鍒涘缓/纭�璁ゅ瓨鍦�: {logDirectory}");
|
|
|
|
|
|
|
+ Log.Information($"日志目录已创建/确认存在: {logDirectory}");
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
|
{
|
|
{
|
|
|
- Log.Fatal($"鏃犳硶鍒涘缓鏃ュ織鐩�褰� {logDirectory}: {ex.Message}");
|
|
|
|
|
|
|
+ Log.Fatal($"无法创建日志目录{logDirectory}: {ex.Message}");
|
|
|
throw;
|
|
throw;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -399,50 +378,61 @@ var eec_TextLogger = new LoggerConfiguration()
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 鍥㈢粍姝ラ�ゆ搷浣� 涓撶敤璁板綍鍣�
|
|
|
|
|
|
|
+#region 分组步骤操作 专用记录器
|
|
|
|
|
|
|
|
-// 鎸囧畾纾佺洏缁濆�硅矾寰勶紙绀轰緥锛欴鐩樼殑AppLogs鏂囦欢澶癸級
|
|
|
|
|
|
|
+// 指定磁盘绝对路径(示例:D盘的AppLogs文件夹)
|
|
|
var groupLogDir = @"D:\OASystem\Logs\GroupStepOP";
|
|
var groupLogDir = @"D:\OASystem\Logs\GroupStepOP";
|
|
|
|
|
|
|
|
-// 鑷�鍔ㄥ垱寤虹洰褰曪紙濡傛灉涓嶅瓨鍦�锛�
|
|
|
|
|
|
|
+// 自动创建目录(如果不存在)
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
|
|
+ // 创建目录,若已存在则不执行任何操作
|
|
|
Directory.CreateDirectory(groupLogDir);
|
|
Directory.CreateDirectory(groupLogDir);
|
|
|
- Log.Information($"鏃ュ織鐩�褰曞凡鍒涘缓/纭�璁ゅ瓨鍦�: {groupLogDir}");
|
|
|
|
|
|
|
+ // 记录日志:目录已创建/确认存在
|
|
|
|
|
+ Log.Information($"日志目录已创建/确认存在: {groupLogDir}");
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
|
{
|
|
{
|
|
|
- Log.Fatal($"鏃犳硶鍒涘缓鏃ュ織鐩�褰� {groupLogDir}: {ex.Message}");
|
|
|
|
|
|
|
+ // 记录致命错误:无法创建日志目录
|
|
|
|
|
+ Log.Fatal($"无法创建日志目录 {groupLogDir}: {ex.Message}");
|
|
|
|
|
+ // 抛出异常终止程序
|
|
|
throw;
|
|
throw;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 初始化分组步骤操作专用日志器
|
|
|
var groupStepOP_TextLogger = new LoggerConfiguration()
|
|
var groupStepOP_TextLogger = new LoggerConfiguration()
|
|
|
- .MinimumLevel.Information()
|
|
|
|
|
- .WriteTo.File(Path.Combine(groupLogDir, "text-records-.txt"), rollingInterval: RollingInterval.Month)
|
|
|
|
|
- .CreateLogger();
|
|
|
|
|
|
|
+ .MinimumLevel.Information() // 最低日志级别:Information
|
|
|
|
|
+ .WriteTo.File( // 输出到文件
|
|
|
|
|
+ Path.Combine(groupLogDir, "text-records-.txt"), // 日志文件路径+名称
|
|
|
|
|
+ rollingInterval: RollingInterval.Month) // 滚动规则:按月生成新文件
|
|
|
|
|
+ .CreateLogger(); // 创建日志实例
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 浠诲姟鍒嗛厤鎿嶄綔 涓撶敤璁板綍鍣�
|
|
|
|
|
|
|
+#region 任务分配操作 专用记录器
|
|
|
|
|
|
|
|
-// 鎸囧畾纾佺洏缁濆�硅矾寰勶紙绀轰緥锛欴鐩樼殑AppLogs鏂囦欢澶癸級
|
|
|
|
|
|
|
+// 指定磁盘绝对路径(示例:D盘的AppLogs文件夹)
|
|
|
var taskLogDir = @"D:\OASystem\Logs\TaskAllocation";
|
|
var taskLogDir = @"D:\OASystem\Logs\TaskAllocation";
|
|
|
|
|
|
|
|
-// 鑷�鍔ㄥ垱寤虹洰褰曪紙濡傛灉涓嶅瓨鍦�锛�
|
|
|
|
|
|
|
+// 自动创建目录(如果不存在)
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
Directory.CreateDirectory(taskLogDir);
|
|
Directory.CreateDirectory(taskLogDir);
|
|
|
- Log.Information($"鏃ュ織鐩�褰曞凡鍒涘缓/纭�璁ゅ瓨鍦�: {taskLogDir}");
|
|
|
|
|
|
|
+ Log.Information($"日志目录已创建/确认存在: {taskLogDir}");
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
|
{
|
|
{
|
|
|
- Log.Fatal($"鏃犳硶鍒涘缓鏃ュ織鐩�褰� {taskLogDir}: {ex.Message}");
|
|
|
|
|
|
|
+ Log.Fatal($"无法创建日志目录 {taskLogDir}: {ex.Message}");
|
|
|
throw;
|
|
throw;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 创建任务分配专用日志实例(按月滚动归档)
|
|
|
var task_TextLogger = new LoggerConfiguration()
|
|
var task_TextLogger = new LoggerConfiguration()
|
|
|
.MinimumLevel.Information()
|
|
.MinimumLevel.Information()
|
|
|
- .WriteTo.File(Path.Combine(taskLogDir, "text-records-.txt"), rollingInterval: RollingInterval.Month)
|
|
|
|
|
|
|
+ .WriteTo.File(
|
|
|
|
|
+ Path.Combine(taskLogDir, "text-records-.txt"),
|
|
|
|
|
+ rollingInterval: RollingInterval.Month
|
|
|
|
|
+ )
|
|
|
.CreateLogger();
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
@@ -455,16 +445,21 @@ builder.Services.AddSingleton<IGroupTextFileLogger>(new GroupTextFileLogger(grou
|
|
|
builder.Services.AddSingleton<ITaskTextFileLogger>(new TaskTextFileLogger(task_TextLogger));
|
|
builder.Services.AddSingleton<ITaskTextFileLogger>(new TaskTextFileLogger(task_TextLogger));
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 寮曞叆娉ㄥ唽Autofac Module
|
|
|
|
|
|
|
+#region 注入注册 Autofac 模块
|
|
|
|
|
+// 使用 Autofac 作为 DI 容器工厂,替换默认容器
|
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
|
|
|
|
+
|
|
|
|
|
+// 配置 Autofac 容器注册
|
|
|
var hostBuilder = builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
|
|
var hostBuilder = builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
|
|
|
{
|
|
{
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
|
|
+ // 注册自定义 Autofac 注册模块(批量注入服务)
|
|
|
builder.RegisterModule(new AutofacRegister());
|
|
builder.RegisterModule(new AutofacRegister());
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
|
{
|
|
{
|
|
|
|
|
+ // 捕获注册异常,拼接异常信息与内部异常,便于排查错误
|
|
|
throw new Exception(ex.Message + "\n" + ex.InnerException);
|
|
throw new Exception(ex.Message + "\n" + ex.InnerException);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
@@ -481,29 +476,32 @@ builder.Services.AddScoped<IMapper, Mapper>();
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region DeepSeek AI 鏈嶅姟
|
|
|
|
|
|
|
+#region DeepSeek AI 服务
|
|
|
|
|
|
|
|
-// 閰嶇疆HTTP瀹㈡埛绔�锛圖eepSeek 闀胯€楁椂璋冪敤锛岄粯璁� 10 鍒嗛挓锛�
|
|
|
|
|
|
|
+// 配置HTTP客户端:DeepSeek 为长耗时调用,设置超时时间 10 分钟
|
|
|
builder.Services.AddHttpClient<IDeepSeekService, DeepSeekService>(client =>
|
|
builder.Services.AddHttpClient<IDeepSeekService, DeepSeekService>(client =>
|
|
|
client.Timeout = TimeSpan.FromMinutes(10));
|
|
client.Timeout = TimeSpan.FromMinutes(10));
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region Doubao API 鏈嶅姟
|
|
|
|
|
|
|
+#region 豆包API服务
|
|
|
|
|
+
|
|
|
var doubaoSetting = builder.Configuration.GetSection("DouBao").Get<OASystem.API.OAMethodLib.DoubaoAPI.DoubaoSetting>();
|
|
var doubaoSetting = builder.Configuration.GetSection("DouBao").Get<OASystem.API.OAMethodLib.DoubaoAPI.DoubaoSetting>();
|
|
|
builder.Services.AddSingleton(doubaoSetting);
|
|
builder.Services.AddSingleton(doubaoSetting);
|
|
|
builder.Services.AddHttpClient("Doubao", c => c.BaseAddress = new Uri(doubaoSetting.BaseAddress));
|
|
builder.Services.AddHttpClient("Doubao", c => c.BaseAddress = new Uri(doubaoSetting.BaseAddress));
|
|
|
builder.Services.AddScoped<OASystem.API.OAMethodLib.DoubaoAPI.IDoubaoService, OASystem.API.OAMethodLib.DoubaoAPI.DoubaoService>();
|
|
builder.Services.AddScoped<OASystem.API.OAMethodLib.DoubaoAPI.IDoubaoService, OASystem.API.OAMethodLib.DoubaoAPI.DoubaoService>();
|
|
|
|
|
+
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 鑱氬悎API 鏈嶅姟
|
|
|
|
|
|
|
+#region 聚合API服务
|
|
|
|
|
+
|
|
|
builder.Services.AddControllersWithViews();
|
|
builder.Services.AddControllersWithViews();
|
|
|
builder.Services.AddSingleton<IJuHeApiService, JuHeApiService>();
|
|
builder.Services.AddSingleton<IJuHeApiService, JuHeApiService>();
|
|
|
builder.Services.AddHttpClient("PublicJuHeApi", c => c.BaseAddress = new Uri("http://web.juhe.cn"));
|
|
builder.Services.AddHttpClient("PublicJuHeApi", c => c.BaseAddress = new Uri("http://web.juhe.cn"));
|
|
|
builder.Services.AddHttpClient("PublicJuHeTranslateApi", c => c.BaseAddress = new Uri("http://apis.juhe.cn"));
|
|
builder.Services.AddHttpClient("PublicJuHeTranslateApi", c => c.BaseAddress = new Uri("http://apis.juhe.cn"));
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 浼佷笟寰�淇�API 鏈嶅姟
|
|
|
|
|
|
|
+#region 企业微信 API 服务
|
|
|
|
|
|
|
|
builder.Services.AddControllersWithViews();
|
|
builder.Services.AddControllersWithViews();
|
|
|
builder.Services.AddSingleton<IQiYeWeChatApiService, QiYeWeChatApiService>();
|
|
builder.Services.AddSingleton<IQiYeWeChatApiService, QiYeWeChatApiService>();
|
|
@@ -511,17 +509,17 @@ builder.Services.AddHttpClient("PublicQiYeWeChatApi", c => c.BaseAddress = new U
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 娣峰厓API
|
|
|
|
|
|
|
+#region 混元API
|
|
|
|
|
|
|
|
-// 浠庨厤缃�涓�璇诲彇鑵捐��浜戝瘑閽ヤ俊鎭�锛堣�风‘淇漚ppsettings.json涓�鏈夊�瑰簲閰嶇疆锛�
|
|
|
|
|
|
|
+// 从配置文件读取腾讯云密钥信息(对应 appsettings.json 中的配置节点)
|
|
|
var secretId = builder.Configuration["TencentCloud:SecretId"];
|
|
var secretId = builder.Configuration["TencentCloud:SecretId"];
|
|
|
var secretKey = builder.Configuration["TencentCloud:SecretKey"];
|
|
var secretKey = builder.Configuration["TencentCloud:SecretKey"];
|
|
|
var region = builder.Configuration["TencentCloud:Region"] ?? "ap-guangzhou";
|
|
var region = builder.Configuration["TencentCloud:Region"] ?? "ap-guangzhou";
|
|
|
|
|
|
|
|
-// 閰嶇疆HttpClientFactory锛圫DK鍐呴儴浼氱敤鍒帮級
|
|
|
|
|
|
|
+// 注册 HttpClient 工厂(SDK 内部依赖使用)
|
|
|
builder.Services.AddHttpClient();
|
|
builder.Services.AddHttpClient();
|
|
|
|
|
|
|
|
-// 娉ㄥ唽鑵捐��浜慔unyuan Client涓篠ingleton锛堟帹鑽愶級
|
|
|
|
|
|
|
+// 注册腾讯云混元客户端(单例模式,官方推荐)
|
|
|
builder.Services.AddSingleton(provider =>
|
|
builder.Services.AddSingleton(provider =>
|
|
|
{
|
|
{
|
|
|
Credential cred = new Credential
|
|
Credential cred = new Credential
|
|
@@ -534,37 +532,29 @@ builder.Services.AddSingleton(provider =>
|
|
|
HttpProfile httpProfile = new HttpProfile
|
|
HttpProfile httpProfile = new HttpProfile
|
|
|
{
|
|
{
|
|
|
Endpoint = "hunyuan.tencentcloudapi.com",
|
|
Endpoint = "hunyuan.tencentcloudapi.com",
|
|
|
- Timeout = 60 * 10, // 鍗曚綅绉�
|
|
|
|
|
|
|
+ Timeout = 60 * 10, // 超时时间:10分钟
|
|
|
};
|
|
};
|
|
|
clientProfile.HttpProfile = httpProfile;
|
|
clientProfile.HttpProfile = httpProfile;
|
|
|
|
|
|
|
|
return new HunyuanClient(cred, region, clientProfile);
|
|
return new HunyuanClient(cred, region, clientProfile);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-// 娉ㄥ唽鑷�瀹氫箟鏈嶅姟鎺ュ彛鍙婂叾瀹炵幇涓篠coped鐢熷懡鍛ㄦ湡
|
|
|
|
|
|
|
+// 注册自定义混元业务接口(作用域生命周期)
|
|
|
builder.Services.AddScoped<IHunyuanService, HunyuanService>();
|
|
builder.Services.AddScoped<IHunyuanService, HunyuanService>();
|
|
|
|
|
|
|
|
-// 娉ㄥ唽娣峰厓鏈嶅姟
|
|
|
|
|
-//builder.Services.AddHttpClient<IHunyuanService, HunyuanService>(client =>
|
|
|
|
|
-//{
|
|
|
|
|
-// client.BaseAddress = new Uri("https://hunyuan.ap-chengdu.tencentcloudapi.com/");
|
|
|
|
|
-// client.Timeout = TimeSpan.FromSeconds(60);
|
|
|
|
|
-//});
|
|
|
|
|
-
|
|
|
|
|
-//builder.Services.Configure<HunyuanApiSettings>(builder.Configuration.GetSection("HunyuanApiSettings"));
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 鏈夐亾API 鏈嶅姟
|
|
|
|
|
|
|
+#region 有道 API 服务
|
|
|
//builder.Services.AddControllersWithViews();
|
|
//builder.Services.AddControllersWithViews();
|
|
|
//builder.Services.AddSingleton<IYouDaoApiService, YouDaoApiService>();
|
|
//builder.Services.AddSingleton<IYouDaoApiService, YouDaoApiService>();
|
|
|
//builder.Services.AddHttpClient("PublicYouDaoApi", c => c.BaseAddress = new Uri("https://openapi.youdao.com"));
|
|
//builder.Services.AddHttpClient("PublicYouDaoApi", c => c.BaseAddress = new Uri("https://openapi.youdao.com"));
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 楂樺痉鍦板浘API 鏈嶅姟
|
|
|
|
|
|
|
+#region 高德地图 API 服务
|
|
|
builder.Services.AddHttpClient<GeocodeService>();
|
|
builder.Services.AddHttpClient<GeocodeService>();
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region 閫氱敤鎼滅储鏈嶅姟
|
|
|
|
|
|
|
+#region 通用搜索服务
|
|
|
builder.Services.AddScoped(typeof(DynamicSearchService<>));
|
|
builder.Services.AddScoped(typeof(DynamicSearchService<>));
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
@@ -600,7 +590,7 @@ builder.Services.TryAddSingleton(typeof(CommonService));
|
|
|
builder.Services.AddScoped<HotmailService>();
|
|
builder.Services.AddScoped<HotmailService>();
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region Microsoft Graph 閭�绠辨湇鍔�
|
|
|
|
|
|
|
+#region Microsoft Graph 邮件服务
|
|
|
|
|
|
|
|
builder.Services.Configure<MicrosoftGraphMailboxOptions>(
|
|
builder.Services.Configure<MicrosoftGraphMailboxOptions>(
|
|
|
builder.Configuration.GetSection(MicrosoftGraphMailboxOptions.SectionName));
|
|
builder.Configuration.GetSection(MicrosoftGraphMailboxOptions.SectionName));
|
|
@@ -616,57 +606,49 @@ builder.Services.AddSingleton<IMicrosoftGraphMailboxService, MicrosoftGraphMailb
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
-//// 1. 寮傚父澶勭悊鍣ㄥ簲璇ュ湪鏈€鏃╃殑浣嶇疆锛堥櫎浜嗘棩蹇楃瓑锛�
|
|
|
|
|
-//app.UseExceptionHandler(new ExceptionHandlerOptions
|
|
|
|
|
-//{
|
|
|
|
|
-// ExceptionHandlingPath = "/Home/Error",
|
|
|
|
|
-// AllowStatusCode404Response = true
|
|
|
|
|
-//});
|
|
|
|
|
-
|
|
|
|
|
-//鑷�瀹氫箟寮傚父涓�闂翠欢
|
|
|
|
|
-//app.UseMiddleware<ExceptionHandlingMiddleware>();
|
|
|
|
|
-
|
|
|
|
|
-//serilog鏃ュ織 璇锋眰涓�闂寸�¢亾
|
|
|
|
|
|
|
+// Serilog日志 请求中间件
|
|
|
app.UseSerilogRequestLogging(options =>
|
|
app.UseSerilogRequestLogging(options =>
|
|
|
{
|
|
{
|
|
|
- //options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} from {ClientIP} (UA: {UserAgent}, Referer: {Referer}) - {StatusCode} in {Elapsed} ms";
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 自定义日志输出模板
|
|
|
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} from {ClientIP} (UA: {UserAgent}) - {StatusCode} in {Elapsed} ms";
|
|
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} from {ClientIP} (UA: {UserAgent}) - {StatusCode} in {Elapsed} ms";
|
|
|
|
|
|
|
|
- // 鑷�瀹氫箟鏃ュ織绾у埆
|
|
|
|
|
|
|
+ // 自定义日志级别
|
|
|
options.GetLevel = (httpContext, elapsed, ex) =>
|
|
options.GetLevel = (httpContext, elapsed, ex) =>
|
|
|
{
|
|
{
|
|
|
|
|
+ // 存在异常 → 错误级别
|
|
|
if (ex != null) return LogEventLevel.Error;
|
|
if (ex != null) return LogEventLevel.Error;
|
|
|
|
|
+ // 500+ 状态码 → 错误级别
|
|
|
if (httpContext.Response.StatusCode > 499) return LogEventLevel.Error;
|
|
if (httpContext.Response.StatusCode > 499) return LogEventLevel.Error;
|
|
|
|
|
|
|
|
- // 瀵瑰仴搴锋�€鏌ョ瓑绔�鐐逛娇鐢ㄦ洿浣庣骇鍒�
|
|
|
|
|
|
|
+ // 健康检查接口使用更低级别(Debug)
|
|
|
if (httpContext.Request.Path.StartsWithSegments("/health"))
|
|
if (httpContext.Request.Path.StartsWithSegments("/health"))
|
|
|
return LogEventLevel.Debug;
|
|
return LogEventLevel.Debug;
|
|
|
|
|
|
|
|
|
|
+ // 默认信息级别
|
|
|
return LogEventLevel.Information;
|
|
return LogEventLevel.Information;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // 涓板瘜鏃ュ織涓婁笅鏂�
|
|
|
|
|
|
|
+ // 丰富日志上下文(添加自定义字段)
|
|
|
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
|
|
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
|
|
|
{
|
|
{
|
|
|
- // 鑾峰彇瀹㈡埛绔疘P锛堝�勭悊浠g悊鎯呭喌锛�
|
|
|
|
|
|
|
+ // 获取客户端IP(处理代理场景)
|
|
|
var ipAddress = CommonFun.GetClientIpAddress(httpContext);
|
|
var ipAddress = CommonFun.GetClientIpAddress(httpContext);
|
|
|
|
|
+ // 解析客户端操作系统
|
|
|
var userAgent = CommonFun.DetectOS(httpContext.Request.Headers.UserAgent.ToString());
|
|
var userAgent = CommonFun.DetectOS(httpContext.Request.Headers.UserAgent.ToString());
|
|
|
|
|
|
|
|
- // 娣诲姞IP鍜屽叾浠栨湁鐢ㄤ俊鎭�鍒版棩蹇椾笂涓嬫枃
|
|
|
|
|
|
|
+ // 添加IP及其他有用信息到日志上下文
|
|
|
diagnosticContext.Set("ClientIP", ipAddress);
|
|
diagnosticContext.Set("ClientIP", ipAddress);
|
|
|
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
|
|
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
|
|
|
diagnosticContext.Set("UserAgent", userAgent);
|
|
diagnosticContext.Set("UserAgent", userAgent);
|
|
|
diagnosticContext.Set("Referer", httpContext.Request.Headers.Referer.ToString());
|
|
diagnosticContext.Set("Referer", httpContext.Request.Headers.Referer.ToString());
|
|
|
|
|
|
|
|
- // 瀵逛簬API璇锋眰娣诲姞棰濆�栦俊鎭�
|
|
|
|
|
|
|
+ // 对API请求额外添加请求头信息
|
|
|
if (httpContext.Request.Path.StartsWithSegments("/api"))
|
|
if (httpContext.Request.Path.StartsWithSegments("/api"))
|
|
|
{
|
|
{
|
|
|
diagnosticContext.Set("RequestContentType", httpContext.Request.ContentType);
|
|
diagnosticContext.Set("RequestContentType", httpContext.Request.ContentType);
|
|
|
diagnosticContext.Set("RequestContentLength", httpContext.Request.ContentLength ?? 0);
|
|
diagnosticContext.Set("RequestContentLength", httpContext.Request.ContentLength ?? 0);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
AutofacIocManager.Instance.Container = app.UseHostFiltering().ApplicationServices.GetAutofacRoot();//AutofacIocManager
|
|
AutofacIocManager.Instance.Container = app.UseHostFiltering().ApplicationServices.GetAutofacRoot();//AutofacIocManager
|
|
@@ -685,60 +667,75 @@ app.UseCors("Cors"); //Cors
|
|
|
|
|
|
|
|
//app.UseMiddleware<FixedPromptMiddleware>();
|
|
//app.UseMiddleware<FixedPromptMiddleware>();
|
|
|
|
|
|
|
|
-// 瀹氫箟鍏佽�窤PI鐨勮�块棶鏃堕棿娈�
|
|
|
|
|
|
|
+// 定义允许 API 访问的时间范围
|
|
|
//var startTime = DateTime.Parse(_config["ApiAccessTime:StartTime"]);
|
|
//var startTime = DateTime.Parse(_config["ApiAccessTime:StartTime"]);
|
|
|
//var endTime = DateTime.Parse(_config["ApiAccessTime:EndTime"]);
|
|
//var endTime = DateTime.Parse(_config["ApiAccessTime:EndTime"]);
|
|
|
//app.UseMiddleware<TimeRestrictionMiddleware>(startTime, endTime);
|
|
//app.UseMiddleware<TimeRestrictionMiddleware>(startTime, endTime);
|
|
|
|
|
|
|
|
-//鎸囧畾API鎿嶄綔璁板綍淇℃伅
|
|
|
|
|
|
|
+// 指定 API 操作记录信息
|
|
|
app.UseMiddleware<RecordAPIOperationMiddleware>();
|
|
app.UseMiddleware<RecordAPIOperationMiddleware>();
|
|
|
|
|
|
|
|
-app.UseAuthentication(); // 璁よ瘉
|
|
|
|
|
|
|
+app.UseAuthentication(); // 认证授权中间件
|
|
|
|
|
|
|
|
app.UseMiddleware<RateLimitMiddleware>();
|
|
app.UseMiddleware<RateLimitMiddleware>();
|
|
|
|
|
|
|
|
-app.UseAuthorization(); // 鎺堟潈
|
|
|
|
|
|
|
+app.UseAuthorization(); // 授权
|
|
|
|
|
|
|
|
app.UseWhen(context =>
|
|
app.UseWhen(context =>
|
|
|
context.Request.Path.StartsWithSegments("/api/MarketCustomerResources/QueryNewClientData"),
|
|
context.Request.Path.StartsWithSegments("/api/MarketCustomerResources/QueryNewClientData"),
|
|
|
branch => branch.UseResponseCompression());
|
|
branch => branch.UseResponseCompression());
|
|
|
|
|
|
|
|
-// 鎺堟潈璺�寰�
|
|
|
|
|
|
|
+// 授权路由
|
|
|
//app.MapGet("generatetoken", c => c.Response.WriteAsync(JWTBearer.GenerateToken(c)));
|
|
//app.MapGet("generatetoken", c => c.Response.WriteAsync(JWTBearer.GenerateToken(c)));
|
|
|
|
|
|
|
|
-#region 鍚�鐢╯waggerUI
|
|
|
|
|
|
|
+#region 启用SwaggerUI
|
|
|
|
|
+
|
|
|
|
|
+// 从配置读取开关,动态启用Swagger文档
|
|
|
if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
|
{
|
|
{
|
|
|
app.UseSwagger();
|
|
app.UseSwagger();
|
|
|
app.UseSwaggerUI(c =>
|
|
app.UseSwaggerUI(c =>
|
|
|
{
|
|
{
|
|
|
|
|
+ // 默认接口文档版本
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ver0.1");
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ver0.1");
|
|
|
|
|
+
|
|
|
|
|
+ // 遍历分组配置,动态加载多分组接口文档(上传文件分组)
|
|
|
foreach (var item in groups)
|
|
foreach (var item in groups)
|
|
|
{
|
|
{
|
|
|
c.SwaggerEndpoint($"/swagger/{item.Item1}/swagger.json", item.Item2);
|
|
c.SwaggerEndpoint($"/swagger/{item.Item1}/swagger.json", item.Item2);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 设置根路径访问Swagger(直接域名打开即文档)
|
|
|
c.RoutePrefix = string.Empty;
|
|
c.RoutePrefix = string.Empty;
|
|
|
|
|
+
|
|
|
|
|
+ // 默认不展开接口列表
|
|
|
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
|
|
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
|
|
|
|
|
+
|
|
|
|
|
+ // 隐藏模型结构,界面更简洁
|
|
|
c.DefaultModelsExpandDepth(-1);
|
|
c.DefaultModelsExpandDepth(-1);
|
|
|
|
|
|
|
|
- //c.EnableFilter();// 娣诲姞鎼滅储鍔熻兘
|
|
|
|
|
- //c.EnableDeepLinking(); // 鍚�鐢ㄦ繁搴﹂摼鎺�
|
|
|
|
|
|
|
+ // 可选功能(已注释)
|
|
|
|
|
+ //c.EnableFilter(); // 启用搜索功能
|
|
|
|
|
+ //c.EnableDeepLinking(); // 启用深度链接
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
-#region Quartz
|
|
|
|
|
|
|
+#region Quartz 定时任务
|
|
|
|
|
|
|
|
-//鑾峰彇瀹瑰櫒涓�鐨凲uartzFactory
|
|
|
|
|
|
|
+// 容器中获取 Quartz 工厂实例
|
|
|
var quartz = app.Services.GetRequiredService<QuartzFactory>();
|
|
var quartz = app.Services.GetRequiredService<QuartzFactory>();
|
|
|
|
|
+
|
|
|
|
|
+// 应用启动时启动 Quartz 定时任务
|
|
|
app.Lifetime.ApplicationStarted.Register(async () =>
|
|
app.Lifetime.ApplicationStarted.Register(async () =>
|
|
|
{
|
|
{
|
|
|
await quartz.Start();
|
|
await quartz.Start();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+// 应用停止时优雅关闭 Quartz 定时任务
|
|
|
app.Lifetime.ApplicationStopped.Register(() =>
|
|
app.Lifetime.ApplicationStopped.Register(() =>
|
|
|
{
|
|
{
|
|
|
- //Quzrtz鍏抽棴鏂规硶
|
|
|
|
|
//quartz.Stop();
|
|
//quartz.Stop();
|
|
|
});
|
|
});
|
|
|
|
|
|