From dfbb150f9b71ed02e282a3b5dbd05aae57e5bd67 Mon Sep 17 00:00:00 2001 From: wintel Date: Sun, 30 Mar 2025 18:01:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A8=E6=80=81API=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=89=A7=E8=A1=8C=E5=B7=B2=E6=9C=89=E7=9A=84?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/DynamicApiApp/DynamicApiApp.cs | 71 ++++++++++++------- .../DynamicApiApp/Request/InvokeDynamicReq.cs | 21 ++++++ OpenAuth.App/Test/TestDynamicApiApp.cs | 10 +++ .../Controllers/DynamicApiController.cs | 29 +++++++- 4 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 OpenAuth.App/DynamicApiApp/Request/InvokeDynamicReq.cs diff --git a/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs b/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs index 197f348f..ce525e72 100644 --- a/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs +++ b/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs @@ -14,15 +14,17 @@ namespace OpenAuth.App /// 动态API应用层 /// 用于直接操作数据库表,不依赖实体 /// - public class DynamicApiApp + public class DynamicApiApp { private readonly ISqlSugarClient _client; private readonly IAuth _auth; + private readonly IServiceProvider _serviceProvider; - public DynamicApiApp(ISqlSugarClient client, IAuth auth) + public DynamicApiApp(ISqlSugarClient client, IAuth auth, IServiceProvider serviceProvider) { _client = client; _auth = auth; + _serviceProvider = serviceProvider; } /// @@ -48,14 +50,14 @@ namespace OpenAuth.App // 获取表结构 var columns = GetTableColumns(req.TableName); - + // 如果有搜索关键字,尝试在常见字段中搜索 if (!string.IsNullOrEmpty(req.key)) { //todo: 尝试在Name、Title等常见字段中搜索 - var nameColumn = columns.FirstOrDefault(c => + var nameColumn = columns.FirstOrDefault(c => c.DbColumnName.Equals("Name", StringComparison.OrdinalIgnoreCase)); - + if (nameColumn != null) { queryable = queryable.Where($"{nameColumn.DbColumnName} like @key", new { key = $"%{req.key}%" }); @@ -64,10 +66,10 @@ namespace OpenAuth.App // 获取总记录数 var total = await queryable.CountAsync(); - + // 分页查询 var list = await queryable - .OrderBy($"{ columns[0].DbColumnName } DESC") + .OrderBy($"{columns[0].DbColumnName} DESC") .Skip((req.page - 1) * req.limit) .Take(req.limit) .ToListAsync(); @@ -108,14 +110,14 @@ namespace OpenAuth.App .AS(req.TableName) .Where("Id = @id", new { id = req.Id }) .FirstAsync(); - + if (entity == null) { result.Code = 500; result.Message = "未找到记录"; return result; } - + result.Result = entity; } catch (Exception ex) @@ -150,7 +152,7 @@ namespace OpenAuth.App // 将对象转换为字典 var dict = req.Obj.ToDictionary(); - + // 设置ID if (!dict.ContainsKey("Id")) { @@ -173,19 +175,19 @@ namespace OpenAuth.App result.Message = "Id已存在"; return result; } - } + } //如果有CreateTime字段,自动设置 if (dict.ContainsKey("CreateTime")) { dict["CreateTime"] = DateTime.Now; } - + // 添加数据 await _client.Insertable(dict) .AS(req.TableName) .ExecuteCommandAsync(); - + result.Message = "添加成功"; } catch (Exception ex) @@ -218,7 +220,7 @@ namespace OpenAuth.App // 将对象转换为字典 var dict = req.Obj.ToDictionary(); - + // 检查ID是否存在 if (!dict.ContainsKey("Id")) { @@ -226,13 +228,13 @@ namespace OpenAuth.App result.Message = "更新数据必须包含Id字段"; return result; } - + // 如果有UpdateTime字段,自动设置 if (dict.ContainsKey("UpdateTime")) { dict["UpdateTime"] = DateTime.Now; } - + // 如果有用户信息,设置更新用户 var currentUser = _auth.GetCurrentUser(); if (dict.ContainsKey("UpdateUserId") && currentUser != null) @@ -247,7 +249,7 @@ namespace OpenAuth.App .AS(req.TableName) .WhereColumns("Id") // 使用Id作为更新条件 .ExecuteCommandAsync(); - + result.Message = "更新成功"; } catch (Exception ex) @@ -283,7 +285,7 @@ namespace OpenAuth.App .AS(req.TableName) .Where("Id in (@ids)", new { ids = req.Ids }) .ExecuteCommandAsync(); - + result.Message = "批量删除成功"; } catch (Exception ex) @@ -305,7 +307,7 @@ namespace OpenAuth.App // 获取数据库类型 var dbType = _client.CurrentConnectionConfig.DbType; string sql = string.Empty; - + switch (dbType) { case DbType.SqlServer: @@ -323,7 +325,7 @@ namespace OpenAuth.App default: throw new NotSupportedException($"不支持的数据库类型:{dbType}"); } - + var count = _client.Ado.GetInt(sql); return count > 0; } @@ -337,6 +339,27 @@ namespace OpenAuth.App { return _client.DbMaintenance.GetColumnInfosByTableName(tableName); } + + /// + /// 调用OpenAuth.App的各种应用 + /// + /// 调用参数 + /// + public TableData Invoke(InvokeDynamicReq req) + { + // 获取服务实例 + var serviceType = Type.GetType($"OpenAuth.App.{req.ServiceName}"); + var service = _serviceProvider.GetService(serviceType); + + // 获取并调用方法 + var method = serviceType.GetMethod(req.MethodName); + var result = method.Invoke(service, new[] { req.Parameters }); + return new TableData + { + data = result, + }; + } + } /// @@ -352,15 +375,15 @@ namespace OpenAuth.App public static Dictionary ToDictionary(this object obj) { var dict = new Dictionary(); - + if (obj == null) return dict; - + // 如果已经是字典类型,直接返回 if (obj is Dictionary dictionary) { return dictionary; } - + // 获取所有属性 foreach (var prop in obj.GetType().GetProperties()) { @@ -370,7 +393,7 @@ namespace OpenAuth.App dict[prop.Name] = value; } } - + return dict; } } diff --git a/OpenAuth.App/DynamicApiApp/Request/InvokeDynamicReq.cs b/OpenAuth.App/DynamicApiApp/Request/InvokeDynamicReq.cs new file mode 100644 index 00000000..8c570e15 --- /dev/null +++ b/OpenAuth.App/DynamicApiApp/Request/InvokeDynamicReq.cs @@ -0,0 +1,21 @@ + +namespace OpenAuth.App.Request +{ + public class InvokeDynamicReq + { + /// + /// 服务名称,如:OpenAuth.App.MoudleApp + /// + public string ServiceName { get; set; } + + /// + /// 方法名称,如:Add + /// + public string MethodName { get; set; } + + /// + /// 参数,如:{ "Id": 1, "Name": "test" } + /// + public object Parameters { get; set; } + } +} diff --git a/OpenAuth.App/Test/TestDynamicApiApp.cs b/OpenAuth.App/Test/TestDynamicApiApp.cs index 46ca46ef..4fd51eb0 100644 --- a/OpenAuth.App/Test/TestDynamicApiApp.cs +++ b/OpenAuth.App/Test/TestDynamicApiApp.cs @@ -73,5 +73,15 @@ namespace OpenAuth.App.Test var obj = await app.Delete(new DelDynamicReq() { TableName = "noentity", Ids = new string[] { "10" } }); Console.WriteLine(JsonHelper.Instance.Serialize(obj)); } + + [Test] + public void TestInvoke() + { + var app = _autofacServiceProvider.GetService(); + + var obj = app.Invoke(new InvokeDynamicReq { ServiceName = "UserManagerApp", MethodName = "GetParent", + Parameters = new { userid = "0ceff0f8-f848-440c-bc26-d8605ac858cd" } }); + Console.WriteLine(JsonHelper.Instance.Serialize(obj)); + } } } diff --git a/OpenAuth.WebApi/Controllers/DynamicApiController.cs b/OpenAuth.WebApi/Controllers/DynamicApiController.cs index 88d9d453..f04b84a9 100644 --- a/OpenAuth.WebApi/Controllers/DynamicApiController.cs +++ b/OpenAuth.WebApi/Controllers/DynamicApiController.cs @@ -11,7 +11,7 @@ namespace OpenAuth.WebApi.Controllers { /// /// 动态API控制器 - /// 用于处理任意表的CRUD操作 + /// 用于处理任意表的CRUD操作及直接调用OpenAuth.App的各种应用 /// [Route("api/dynamic/[action]")] [ApiController] @@ -25,6 +25,8 @@ namespace OpenAuth.WebApi.Controllers _app = app; } + + /// /// 获取表数据列表 /// @@ -145,5 +147,30 @@ namespace OpenAuth.WebApi.Controllers return result; } + + /// + /// 直接调用OpenAuth.App的各种应用 + /// + /// 调用参数 + /// + [HttpPost] + [AllowAnonymous] + public TableData Invoke([FromBody] InvokeDynamicReq req) + { + var result = new TableData(); + try + { + result = _app.Invoke(req); + } + catch (Exception ex) + { + result.code = 500; + result.msg = ex.InnerException?.Message ?? ex.Message; + } + + return result; + } } + + } \ No newline at end of file