using XFEExtension.NetCore.ServerInteractive.Models.UserModels; using XFEExtension.NetCore.ServerInteractive.Options; using XFEExtension.NetCore.ServerInteractive.Utilities.DataTable; using XFEExtension.NetCore.ServerInteractive.Utilities.Server; using XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreService; namespace XFEExtension.NetCore.ServerInteractive.Utilities.Extensions; /// /// XFE服务器核心构建器扩展 /// public static class XFEServerCoreBuilderExtensions { /// extension(XFEServerCoreBuilder xFEServerCoreBuilder) { /// /// 使用XFE数据表格管理器 /// /// 数据表格构建器 /// /// /// public XFEServerCoreBuilder AddDataTableManager(XFEDataTableManagerBuilder xFEDataTableManagerBuilder, Func> getUserFunction, Func> getEncryptedUserLoginModelFunction) { xFEServerCoreBuilder.AddParameter("TableManager", xFEDataTableManagerBuilder.Build(getUserFunction, getEncryptedUserLoginModelFunction)); // Register the service for each table operation route // XFEDataTableManagerService uses dynamic routing, so we use AddServiceWithRoute foreach (var execute in xFEDataTableManagerBuilder.ExecuteList) { var parts = execute.Split('_', 2); if (parts.Length != 2) throw new InvalidOperationException($"执行语句格式无效:'{execute}',期望格式为 '{{operation}}_{{tableName}}'"); var route = $"table/{parts[0]}/{parts[1]}"; xFEServerCoreBuilder.AddServiceWithRoute(route); } return xFEServerCoreBuilder; } /// /// 添加用户基本参数 /// /// 登录返回用户接口类型 /// /// /// /// /// /// /// public XFEServerCoreBuilder AddUserParameterBase(Func> getUserFunction, Func> getEncryptedUserLoginModelFunction, Action addEncryptedUserLoginModelFunction, Action removeEncryptedUserLoginModelFunction, Func getLoginKeepDays, Func loginResultConvertFunction) where T : class => xFEServerCoreBuilder.AddParameter("GetUserFunction", getUserFunction) .AddParameter("GetEncryptedUserLoginModelFunction", getEncryptedUserLoginModelFunction) .AddParameter("AddEncryptedUserLoginModelFunction", addEncryptedUserLoginModelFunction) .AddParameter("RemoveEncryptedUserLoginModelFunction", removeEncryptedUserLoginModelFunction) .AddParameter("GetLoginKeepDays", getLoginKeepDays) .AddParameter("LoginResultConvertFunction", loginResultConvertFunction); /// /// 使用XFE标准登录服务 /// /// 登录返回用户接口类型 /// public XFEServerCoreBuilder AddStandardLoginService() where T : class => xFEServerCoreBuilder.AddService>() .AddService>() .AddOriginalService(); /// /// 添加IP封禁服务 /// /// public XFEServerCoreBuilder AddIPBannerService() => xFEServerCoreBuilder.AddService(); /// /// 添加日期统计服务 /// /// public XFEServerCoreBuilder AddDailyCounterService() => xFEServerCoreBuilder.AddOriginalService(); /// /// 添加XFE异常处理服务 /// /// public XFEServerCoreBuilder AddXFEErrorProcessService() => xFEServerCoreBuilder.AddOriginalService(); /// /// 添加连接检查服务 /// /// public XFEServerCoreBuilder AddConnectService() => xFEServerCoreBuilder.AddService(); /// /// 添加服务器入口点校验 /// /// public XFEServerCoreBuilder AddEntryPointVerify() => xFEServerCoreBuilder.AddVerifyService(); /// /// 添加服务器日志请求 /// /// public XFEServerCoreBuilder AddServerLogService() => xFEServerCoreBuilder.AddService(); /// /// 使用XFE标准服务器核心 /// /// 登录返回用户接口类型 /// /// /// /// /// /// /// /// public XFEServerCoreBuilder UseXFEStandardServerCore(Func> getUserFunction, Func> getEncryptedUserLoginModelFunction, Action addEncryptedUserLoginModelFunction, Action removeEncryptedUserLoginModelFunction, Func getLoginKeepDays, Func loginResultConvertFunction, XFEDataTableManagerBuilder xFEDataTableManagerBuilder) where T : class => xFEServerCoreBuilder.AddUserParameterBase(getUserFunction, getEncryptedUserLoginModelFunction, addEncryptedUserLoginModelFunction, removeEncryptedUserLoginModelFunction, getLoginKeepDays, loginResultConvertFunction) .AddDataTableManager(xFEDataTableManagerBuilder, getUserFunction, getEncryptedUserLoginModelFunction) .AddEntryPointVerify() .AddDailyCounterService() .AddXFEErrorProcessService() .AddConnectService() .AddStandardLoginService() .AddServerLogService() .AddIPBannerService(); /// /// 使用XFE标准服务器核心(Options) /// public XFEServerCoreBuilder UseXFEStandardServerCore(XFEStandardServerCoreOptions? options) where T : class { if (options is null) return xFEServerCoreBuilder; // start with base builder var builder = xFEServerCoreBuilder; // If user-related functions are provided, add user parameters and login services var hasUserFunctions = options.GetUserFunction is not null && options.GetEncryptedUserLoginModelFunction is not null && options.AddEncryptedUserLoginModelFunction is not null && options.RemoveEncryptedUserLoginModelFunction is not null && options.LoginResultConvertFunction is not null; if (hasUserFunctions) { builder = builder.AddUserParameterBase(options.GetUserFunction!, options.GetEncryptedUserLoginModelFunction!, options.AddEncryptedUserLoginModelFunction!, options.RemoveEncryptedUserLoginModelFunction!, options.GetLoginKeepDays, options.LoginResultConvertFunction!); } // DataTableManager requires both user functions and a builder if (options.DataTableManagerBuilder is not null && options.GetUserFunction is not null && options.GetEncryptedUserLoginModelFunction is not null) { builder = builder.AddDataTableManager(options.DataTableManagerBuilder, options.GetUserFunction, options.GetEncryptedUserLoginModelFunction); } // Common services that don't strictly require user functions if (options.UseEntryPointVerifyService) { builder = builder.AddEntryPointVerify(); } if (options.UseDailyCounterService) { builder = builder.AddDailyCounterService(); } if (options.UseXFEErrorProcessService) { builder = builder.AddXFEErrorProcessService(); } if (options.UseConnectService) { builder = builder.AddConnectService(); } // Add services that depend on user functions if available if (hasUserFunctions) { builder = builder.AddStandardLoginService().AddServerLogService().AddIPBannerService(); } return builder; } /// /// 使用 XFE 标准服务器核心,提供 options lambda 初始化器 /// public XFEServerCoreBuilder UseXFEStandardServerCore(Action> optionsBuilder) where T : class { var options = new XFEStandardServerCoreOptions(); optionsBuilder.Invoke(options); return xFEServerCoreBuilder.UseXFEStandardServerCore(options); } } }