mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 05:13:25 +08:00
feat: 动态API支持直接执行已有的应用逻辑
This commit is contained in:
parent
d42be4d7c9
commit
dfbb150f9b
@ -18,11 +18,13 @@ namespace OpenAuth.App
|
|||||||
{
|
{
|
||||||
private readonly ISqlSugarClient _client;
|
private readonly ISqlSugarClient _client;
|
||||||
private readonly IAuth _auth;
|
private readonly IAuth _auth;
|
||||||
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
public DynamicApiApp(ISqlSugarClient client, IAuth auth)
|
public DynamicApiApp(ISqlSugarClient client, IAuth auth, IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_auth = auth;
|
_auth = auth;
|
||||||
|
_serviceProvider = serviceProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -67,7 +69,7 @@ namespace OpenAuth.App
|
|||||||
|
|
||||||
// 分页查询
|
// 分页查询
|
||||||
var list = await queryable
|
var list = await queryable
|
||||||
.OrderBy($"{ columns[0].DbColumnName } DESC")
|
.OrderBy($"{columns[0].DbColumnName} DESC")
|
||||||
.Skip((req.page - 1) * req.limit)
|
.Skip((req.page - 1) * req.limit)
|
||||||
.Take(req.limit)
|
.Take(req.limit)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
@ -337,6 +339,27 @@ namespace OpenAuth.App
|
|||||||
{
|
{
|
||||||
return _client.DbMaintenance.GetColumnInfosByTableName(tableName);
|
return _client.DbMaintenance.GetColumnInfosByTableName(tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调用OpenAuth.App的各种应用
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="req">调用参数</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
21
OpenAuth.App/DynamicApiApp/Request/InvokeDynamicReq.cs
Normal file
21
OpenAuth.App/DynamicApiApp/Request/InvokeDynamicReq.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
namespace OpenAuth.App.Request
|
||||||
|
{
|
||||||
|
public class InvokeDynamicReq
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 服务名称,如:OpenAuth.App.MoudleApp
|
||||||
|
/// </summary>
|
||||||
|
public string ServiceName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 方法名称,如:Add
|
||||||
|
/// </summary>
|
||||||
|
public string MethodName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 参数,如:{ "Id": 1, "Name": "test" }
|
||||||
|
/// </summary>
|
||||||
|
public object Parameters { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -73,5 +73,15 @@ namespace OpenAuth.App.Test
|
|||||||
var obj = await app.Delete(new DelDynamicReq() { TableName = "noentity", Ids = new string[] { "10" } });
|
var obj = await app.Delete(new DelDynamicReq() { TableName = "noentity", Ids = new string[] { "10" } });
|
||||||
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
|
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestInvoke()
|
||||||
|
{
|
||||||
|
var app = _autofacServiceProvider.GetService<DynamicApiApp>();
|
||||||
|
|
||||||
|
var obj = app.Invoke(new InvokeDynamicReq { ServiceName = "UserManagerApp", MethodName = "GetParent",
|
||||||
|
Parameters = new { userid = "0ceff0f8-f848-440c-bc26-d8605ac858cd" } });
|
||||||
|
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ namespace OpenAuth.WebApi.Controllers
|
|||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 动态API控制器
|
/// 动态API控制器
|
||||||
/// 用于处理任意表的CRUD操作
|
/// 用于处理任意表的CRUD操作及直接调用OpenAuth.App的各种应用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Route("api/dynamic/[action]")]
|
[Route("api/dynamic/[action]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
@ -25,6 +25,8 @@ namespace OpenAuth.WebApi.Controllers
|
|||||||
_app = app;
|
_app = app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取表数据列表
|
/// 获取表数据列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -145,5 +147,30 @@ namespace OpenAuth.WebApi.Controllers
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 直接调用OpenAuth.App的各种应用
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="req">调用参数</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user