fix 将参数调整为json序列化字符串

This commit is contained in:
wintel 2025-03-30 19:58:52 +08:00
parent b9bd4a66b8
commit 2d768d3cc7
4 changed files with 35 additions and 78 deletions

View File

@ -151,7 +151,7 @@ namespace OpenAuth.App
}
// 将对象转换为字典
var dict = req.Obj.ToDictionary();
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.Obj);
// 设置ID
if (!dict.ContainsKey("Id"))
@ -180,7 +180,7 @@ namespace OpenAuth.App
//如果有CreateTime字段自动设置
if (dict.ContainsKey("CreateTime"))
{
dict["CreateTime"] = DateTime.Now;
dict["CreateTime"] = DateTime.Now.ToString();
}
// 添加数据
@ -219,7 +219,7 @@ namespace OpenAuth.App
}
// 将对象转换为字典
var dict = req.Obj.ToDictionary();
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.Obj);
// 检查ID是否存在
if (!dict.ContainsKey("Id"))
@ -232,7 +232,7 @@ namespace OpenAuth.App
// 如果有UpdateTime字段自动设置
if (dict.ContainsKey("UpdateTime"))
{
dict["UpdateTime"] = DateTime.Now;
dict["UpdateTime"] = DateTime.Now.ToString();
}
// 如果有用户信息,设置更新用户
@ -372,7 +372,7 @@ namespace OpenAuth.App
// 调用方法
// 将对象转换为字典
var dict = req.Parameters.ToDictionary();
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.Parameters);
// 获取方法参数信息
var parameters = method.GetParameters();
@ -401,40 +401,5 @@ namespace OpenAuth.App
}
}
/// <summary>
/// 对象扩展方法
/// </summary>
public static class ObjectExtensions
{
/// <summary>
/// 将对象转换为字典
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static Dictionary<string, object> ToDictionary(this object obj)
{
var dict = new Dictionary<string, object>();
if (obj == null) return dict;
// 如果已经是字典类型,直接返回
if (obj is Dictionary<string, object> dictionary)
{
return dictionary;
}
// 获取所有属性
foreach (var prop in obj.GetType().GetProperties())
{
var value = prop.GetValue(obj);
if (value != null)
{
dict[prop.Name] = value;
}
}
return dict;
}
}
}

View File

@ -4,6 +4,6 @@ namespace OpenAuth.App.Request
{
public string TableName { get; set; }
public object Obj { get; set; }
public string Obj { get; set; }
}
}

View File

@ -16,6 +16,6 @@ namespace OpenAuth.App.Request
/// <summary>
/// 参数,如:{ "Id": 1, "Name": "test" }
/// </summary>
public object Parameters { get; set; }
public string Parameters { get; set; }
}
}

View File

@ -48,50 +48,42 @@ namespace OpenAuth.App.Test
}
[Test]
public async Task TestAdd()
{
var app = _autofacServiceProvider.GetService<DynamicApiApp>();
var obj = await app.Add(new AddOrUpdateDynamicEntityReq { TableName = "noentity", Obj = new { Id = "10", P1 = DateTime.Now.ToString() } });
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
}
[Test]
public async Task TestUpdate()
{
var app = _autofacServiceProvider.GetService<DynamicApiApp>();
var obj = await app.Update(new AddOrUpdateDynamicEntityReq { TableName = "noentity", Obj = new { Id = "1", P1 = DateTime.Now.ToString() } });
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
}
[Test]
public async Task TestDelete()
{
var app = _autofacServiceProvider.GetService<DynamicApiApp>();
var obj = await app.Delete(new DelDynamicReq() { TableName = "noentity", Ids = new string[] { "10" } });
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
}
[Test]
public void TestInvoke()
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" } });
Parameters = "{\"userid\":\"0ceff0f8-f848-440c-bc26-d8605ac858cd\"}" });
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
}
[Test]
public void TestInvoke2()
public async Task TestInvoke2()
{
var app = _autofacServiceProvider.GetService<DynamicApiApp>();
var obj = app.Invoke(new InvokeDynamicReq { ServiceName = "UserManagerApp", MethodName = "Load",
Parameters = new { page = 1, limit = 10, key = "" } });
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
}
var obj = app.Invoke(new InvokeDynamicReq {
ServiceName = "UserManagerApp",
MethodName = "Load",
Parameters = "{\"request\":{\"page\":1,\"limit\":10,\"key\":\"\"}}"
});
// 如果返回的是Task需要等待它完成并获取结果
if (obj is Task task)
{
await task;
// 使用反射获取Task的Result属性
var resultProperty = task.GetType().GetProperty("Result");
if (resultProperty != null)
{
var result = resultProperty.GetValue(task);
Console.WriteLine(JsonHelper.Instance.Serialize(result));
}
}
else
{
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
}
}
}
}