mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-04-05 17:38:01 +08:00
feat: 动态API支持直接执行已有的应用逻辑
This commit is contained in:
parent
d42be4d7c9
commit
dfbb150f9b
@ -14,15 +14,17 @@ namespace OpenAuth.App
|
|||||||
/// 动态API应用层
|
/// 动态API应用层
|
||||||
/// 用于直接操作数据库表,不依赖实体
|
/// 用于直接操作数据库表,不依赖实体
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DynamicApiApp
|
public class DynamicApiApp
|
||||||
{
|
{
|
||||||
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>
|
||||||
@ -48,14 +50,14 @@ namespace OpenAuth.App
|
|||||||
|
|
||||||
// 获取表结构
|
// 获取表结构
|
||||||
var columns = GetTableColumns(req.TableName);
|
var columns = GetTableColumns(req.TableName);
|
||||||
|
|
||||||
// 如果有搜索关键字,尝试在常见字段中搜索
|
// 如果有搜索关键字,尝试在常见字段中搜索
|
||||||
if (!string.IsNullOrEmpty(req.key))
|
if (!string.IsNullOrEmpty(req.key))
|
||||||
{
|
{
|
||||||
//todo: 尝试在Name、Title等常见字段中搜索
|
//todo: 尝试在Name、Title等常见字段中搜索
|
||||||
var nameColumn = columns.FirstOrDefault(c =>
|
var nameColumn = columns.FirstOrDefault(c =>
|
||||||
c.DbColumnName.Equals("Name", StringComparison.OrdinalIgnoreCase));
|
c.DbColumnName.Equals("Name", StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
if (nameColumn != null)
|
if (nameColumn != null)
|
||||||
{
|
{
|
||||||
queryable = queryable.Where($"{nameColumn.DbColumnName} like @key", new { key = $"%{req.key}%" });
|
queryable = queryable.Where($"{nameColumn.DbColumnName} like @key", new { key = $"%{req.key}%" });
|
||||||
@ -64,10 +66,10 @@ namespace OpenAuth.App
|
|||||||
|
|
||||||
// 获取总记录数
|
// 获取总记录数
|
||||||
var total = await queryable.CountAsync();
|
var total = await queryable.CountAsync();
|
||||||
|
|
||||||
// 分页查询
|
// 分页查询
|
||||||
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();
|
||||||
@ -108,14 +110,14 @@ namespace OpenAuth.App
|
|||||||
.AS(req.TableName)
|
.AS(req.TableName)
|
||||||
.Where("Id = @id", new { id = req.Id })
|
.Where("Id = @id", new { id = req.Id })
|
||||||
.FirstAsync();
|
.FirstAsync();
|
||||||
|
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
{
|
{
|
||||||
result.Code = 500;
|
result.Code = 500;
|
||||||
result.Message = "未找到记录";
|
result.Message = "未找到记录";
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Result = entity;
|
result.Result = entity;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -150,7 +152,7 @@ namespace OpenAuth.App
|
|||||||
|
|
||||||
// 将对象转换为字典
|
// 将对象转换为字典
|
||||||
var dict = req.Obj.ToDictionary();
|
var dict = req.Obj.ToDictionary();
|
||||||
|
|
||||||
// 设置ID
|
// 设置ID
|
||||||
if (!dict.ContainsKey("Id"))
|
if (!dict.ContainsKey("Id"))
|
||||||
{
|
{
|
||||||
@ -173,19 +175,19 @@ namespace OpenAuth.App
|
|||||||
result.Message = "Id已存在";
|
result.Message = "Id已存在";
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//如果有CreateTime字段,自动设置
|
//如果有CreateTime字段,自动设置
|
||||||
if (dict.ContainsKey("CreateTime"))
|
if (dict.ContainsKey("CreateTime"))
|
||||||
{
|
{
|
||||||
dict["CreateTime"] = DateTime.Now;
|
dict["CreateTime"] = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加数据
|
// 添加数据
|
||||||
await _client.Insertable(dict)
|
await _client.Insertable(dict)
|
||||||
.AS(req.TableName)
|
.AS(req.TableName)
|
||||||
.ExecuteCommandAsync();
|
.ExecuteCommandAsync();
|
||||||
|
|
||||||
result.Message = "添加成功";
|
result.Message = "添加成功";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -218,7 +220,7 @@ namespace OpenAuth.App
|
|||||||
|
|
||||||
// 将对象转换为字典
|
// 将对象转换为字典
|
||||||
var dict = req.Obj.ToDictionary();
|
var dict = req.Obj.ToDictionary();
|
||||||
|
|
||||||
// 检查ID是否存在
|
// 检查ID是否存在
|
||||||
if (!dict.ContainsKey("Id"))
|
if (!dict.ContainsKey("Id"))
|
||||||
{
|
{
|
||||||
@ -226,13 +228,13 @@ namespace OpenAuth.App
|
|||||||
result.Message = "更新数据必须包含Id字段";
|
result.Message = "更新数据必须包含Id字段";
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果有UpdateTime字段,自动设置
|
// 如果有UpdateTime字段,自动设置
|
||||||
if (dict.ContainsKey("UpdateTime"))
|
if (dict.ContainsKey("UpdateTime"))
|
||||||
{
|
{
|
||||||
dict["UpdateTime"] = DateTime.Now;
|
dict["UpdateTime"] = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果有用户信息,设置更新用户
|
// 如果有用户信息,设置更新用户
|
||||||
var currentUser = _auth.GetCurrentUser();
|
var currentUser = _auth.GetCurrentUser();
|
||||||
if (dict.ContainsKey("UpdateUserId") && currentUser != null)
|
if (dict.ContainsKey("UpdateUserId") && currentUser != null)
|
||||||
@ -247,7 +249,7 @@ namespace OpenAuth.App
|
|||||||
.AS(req.TableName)
|
.AS(req.TableName)
|
||||||
.WhereColumns("Id") // 使用Id作为更新条件
|
.WhereColumns("Id") // 使用Id作为更新条件
|
||||||
.ExecuteCommandAsync();
|
.ExecuteCommandAsync();
|
||||||
|
|
||||||
result.Message = "更新成功";
|
result.Message = "更新成功";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -283,7 +285,7 @@ namespace OpenAuth.App
|
|||||||
.AS(req.TableName)
|
.AS(req.TableName)
|
||||||
.Where("Id in (@ids)", new { ids = req.Ids })
|
.Where("Id in (@ids)", new { ids = req.Ids })
|
||||||
.ExecuteCommandAsync();
|
.ExecuteCommandAsync();
|
||||||
|
|
||||||
result.Message = "批量删除成功";
|
result.Message = "批量删除成功";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -305,7 +307,7 @@ namespace OpenAuth.App
|
|||||||
// 获取数据库类型
|
// 获取数据库类型
|
||||||
var dbType = _client.CurrentConnectionConfig.DbType;
|
var dbType = _client.CurrentConnectionConfig.DbType;
|
||||||
string sql = string.Empty;
|
string sql = string.Empty;
|
||||||
|
|
||||||
switch (dbType)
|
switch (dbType)
|
||||||
{
|
{
|
||||||
case DbType.SqlServer:
|
case DbType.SqlServer:
|
||||||
@ -323,7 +325,7 @@ namespace OpenAuth.App
|
|||||||
default:
|
default:
|
||||||
throw new NotSupportedException($"不支持的数据库类型:{dbType}");
|
throw new NotSupportedException($"不支持的数据库类型:{dbType}");
|
||||||
}
|
}
|
||||||
|
|
||||||
var count = _client.Ado.GetInt(sql);
|
var count = _client.Ado.GetInt(sql);
|
||||||
return count > 0;
|
return count > 0;
|
||||||
}
|
}
|
||||||
@ -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>
|
||||||
@ -352,15 +375,15 @@ namespace OpenAuth.App
|
|||||||
public static Dictionary<string, object> ToDictionary(this object obj)
|
public static Dictionary<string, object> ToDictionary(this object obj)
|
||||||
{
|
{
|
||||||
var dict = new Dictionary<string, object>();
|
var dict = new Dictionary<string, object>();
|
||||||
|
|
||||||
if (obj == null) return dict;
|
if (obj == null) return dict;
|
||||||
|
|
||||||
// 如果已经是字典类型,直接返回
|
// 如果已经是字典类型,直接返回
|
||||||
if (obj is Dictionary<string, object> dictionary)
|
if (obj is Dictionary<string, object> dictionary)
|
||||||
{
|
{
|
||||||
return dictionary;
|
return dictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有属性
|
// 获取所有属性
|
||||||
foreach (var prop in obj.GetType().GetProperties())
|
foreach (var prop in obj.GetType().GetProperties())
|
||||||
{
|
{
|
||||||
@ -370,7 +393,7 @@ namespace OpenAuth.App
|
|||||||
dict[prop.Name] = value;
|
dict[prop.Name] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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