2025-03-30 02:19:14 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-03-30 16:50:46 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
using Infrastructure;
|
|
|
|
|
using OpenAuth.App.Interface;
|
|
|
|
|
using OpenAuth.App.Response;
|
|
|
|
|
using SqlSugar;
|
2025-03-30 16:50:46 +08:00
|
|
|
|
using OpenAuth.App.Request;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 动态API应用层
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 用于直接操作数据库表,不依赖实体
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 18:01:22 +08:00
|
|
|
|
public class DynamicApiApp
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly ISqlSugarClient _client;
|
|
|
|
|
private readonly IAuth _auth;
|
2025-03-30 18:01:22 +08:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
|
2025-03-30 18:01:22 +08:00
|
|
|
|
public DynamicApiApp(ISqlSugarClient client, IAuth auth, IServiceProvider serviceProvider)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
_client = client;
|
|
|
|
|
_auth = auth;
|
2025-03-30 18:01:22 +08:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 获取表数据列表
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <param name="req">查询参数</param>
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <returns></returns>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
public async Task<TableData> GetList(QueryDynamicListReq req)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
var result = new TableData();
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 验证表名是否存在
|
|
|
|
|
if (!TableExists(req.TableName))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
result.code = 500;
|
2025-03-30 16:50:46 +08:00
|
|
|
|
result.msg = $"表不存在:{req.TableName}";
|
2025-03-30 02:19:14 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建动态查询
|
2025-03-30 16:50:46 +08:00
|
|
|
|
var queryable = _client.Queryable<dynamic>().AS(req.TableName);
|
|
|
|
|
|
|
|
|
|
// 获取表结构
|
|
|
|
|
var columns = GetTableColumns(req.TableName);
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 02:19:14 +08:00
|
|
|
|
// 如果有搜索关键字,尝试在常见字段中搜索
|
2025-03-30 16:50:46 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(req.key))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
//todo: 尝试在Name、Title等常见字段中搜索
|
2025-03-30 18:01:22 +08:00
|
|
|
|
var nameColumn = columns.FirstOrDefault(c =>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
c.DbColumnName.Equals("Name", StringComparison.OrdinalIgnoreCase));
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
if (nameColumn != null)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
queryable = queryable.Where($"{nameColumn.DbColumnName} like @key", new { key = $"%{req.key}%" });
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取总记录数
|
|
|
|
|
var total = await queryable.CountAsync();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 02:19:14 +08:00
|
|
|
|
// 分页查询
|
2025-03-30 16:50:46 +08:00
|
|
|
|
var list = await queryable
|
2025-03-30 18:01:22 +08:00
|
|
|
|
.OrderBy($"{columns[0].DbColumnName} DESC")
|
2025-03-30 16:50:46 +08:00
|
|
|
|
.Skip((req.page - 1) * req.limit)
|
|
|
|
|
.Take(req.limit)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
result.data = list;
|
|
|
|
|
result.count = total;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.code = 500;
|
|
|
|
|
result.msg = ex.InnerException?.Message ?? ex.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 获取表数据详情
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <param name="tableName">表名称</param>
|
|
|
|
|
/// <param name="id">记录ID</param>
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <returns></returns>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
public async Task<Response<object>> Get(QueryDynamicEntityReq req)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
var result = new Response<object>();
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 验证表名是否存在
|
|
|
|
|
if (!TableExists(req.TableName))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
2025-03-30 16:50:46 +08:00
|
|
|
|
result.Message = $"表不存在:{req.TableName}";
|
2025-03-30 02:19:14 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 获取数据
|
|
|
|
|
var entity = await _client.Queryable<dynamic>()
|
|
|
|
|
.AS(req.TableName)
|
|
|
|
|
.Where("Id = @id", new { id = req.Id })
|
|
|
|
|
.FirstAsync();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
|
|
|
|
result.Message = "未找到记录";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 02:19:14 +08:00
|
|
|
|
result.Result = entity;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
|
|
|
|
result.Message = ex.InnerException?.Message ?? ex.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 添加表数据
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <param name="tableName">表名称</param>
|
|
|
|
|
/// <param name="obj">数据对象
|
|
|
|
|
/// <para>如果数据里面没有Id,自动会添加ID</para>
|
|
|
|
|
/// </param>
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <returns></returns>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
public async Task<Response<object>> Add(AddOrUpdateDynamicEntityReq req)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
var result = new Response<object>();
|
2025-03-30 02:19:14 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 验证表名是否存在
|
|
|
|
|
if (!TableExists(req.TableName))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
2025-03-30 16:50:46 +08:00
|
|
|
|
result.Message = $"表不存在:{req.TableName}";
|
2025-03-30 02:19:14 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 将对象转换为字典
|
|
|
|
|
var dict = req.Obj.ToDictionary();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 设置ID
|
|
|
|
|
if (!dict.ContainsKey("Id"))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
dict["Id"] = Guid.NewGuid().ToString();
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
2025-03-30 16:50:46 +08:00
|
|
|
|
else if (string.IsNullOrEmpty(dict["Id"]?.ToString()))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
dict["Id"] = Guid.NewGuid().ToString();
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
2025-03-30 16:50:46 +08:00
|
|
|
|
else
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
//如果Id不为空,需要判断Id是否存在
|
|
|
|
|
var entity = await _client.Queryable<dynamic>()
|
|
|
|
|
.AS(req.TableName)
|
|
|
|
|
.Where("Id = @id", new { id = dict["Id"] })
|
|
|
|
|
.FirstAsync();
|
|
|
|
|
if (entity != null)
|
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
|
|
|
|
result.Message = "Id已存在";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
}
|
2025-03-30 02:19:14 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
//如果有CreateTime字段,自动设置
|
|
|
|
|
if (dict.ContainsKey("CreateTime"))
|
|
|
|
|
{
|
|
|
|
|
dict["CreateTime"] = DateTime.Now;
|
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 添加数据
|
|
|
|
|
await _client.Insertable(dict)
|
|
|
|
|
.AS(req.TableName)
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 02:19:14 +08:00
|
|
|
|
result.Message = "添加成功";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
|
|
|
|
result.Message = ex.InnerException?.Message ?? ex.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 更新表数据
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <param name="tableName">表名称</param>
|
|
|
|
|
/// <param name="obj">数据对象</param>
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <returns></returns>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
public async Task<Infrastructure.Response> Update(AddOrUpdateDynamicEntityReq req)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
var result = new Infrastructure.Response();
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 验证表名是否存在
|
|
|
|
|
if (!TableExists(req.TableName))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
2025-03-30 16:50:46 +08:00
|
|
|
|
result.Message = $"表不存在:{req.TableName}";
|
2025-03-30 02:19:14 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 将对象转换为字典
|
|
|
|
|
var dict = req.Obj.ToDictionary();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 检查ID是否存在
|
|
|
|
|
if (!dict.ContainsKey("Id"))
|
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
|
|
|
|
result.Message = "更新数据必须包含Id字段";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 17:12:18 +08:00
|
|
|
|
// 如果有UpdateTime字段,自动设置
|
|
|
|
|
if (dict.ContainsKey("UpdateTime"))
|
2025-03-30 16:50:46 +08:00
|
|
|
|
{
|
|
|
|
|
dict["UpdateTime"] = DateTime.Now;
|
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 02:19:14 +08:00
|
|
|
|
// 如果有用户信息,设置更新用户
|
|
|
|
|
var currentUser = _auth.GetCurrentUser();
|
2025-03-30 16:50:46 +08:00
|
|
|
|
if (dict.ContainsKey("UpdateUserId") && currentUser != null)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 设置更新用户信息
|
|
|
|
|
dict["UpdateUserId"] = currentUser.User.Id;
|
|
|
|
|
dict["UpdateUserName"] = currentUser.User.Name;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 更新数据
|
|
|
|
|
await _client.Updateable(dict)
|
|
|
|
|
.AS(req.TableName)
|
2025-03-30 17:12:18 +08:00
|
|
|
|
.WhereColumns("Id") // 使用Id作为更新条件
|
2025-03-30 16:50:46 +08:00
|
|
|
|
.ExecuteCommandAsync();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 02:19:14 +08:00
|
|
|
|
result.Message = "更新成功";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
|
|
|
|
result.Message = ex.InnerException?.Message ?? ex.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 批量删除表数据
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <param name="tableName">表名称</param>
|
|
|
|
|
/// <param name="ids">记录ID数组</param>
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <returns></returns>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
public async Task<Infrastructure.Response> Delete(DelDynamicReq req)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
var result = new Infrastructure.Response();
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 验证表名是否存在
|
|
|
|
|
if (!TableExists(req.TableName))
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
2025-03-30 16:50:46 +08:00
|
|
|
|
result.Message = $"表不存在:{req.TableName}";
|
2025-03-30 02:19:14 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 批量删除数据
|
|
|
|
|
await _client.Deleteable<dynamic>()
|
|
|
|
|
.AS(req.TableName)
|
|
|
|
|
.Where("Id in (@ids)", new { ids = req.Ids })
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
result.Message = "批量删除成功";
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.Code = 500;
|
|
|
|
|
result.Message = ex.InnerException?.Message ?? ex.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 检查表是否存在
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <param name="tableName">表名</param>
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <returns></returns>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
private bool TableExists(string tableName)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 获取数据库类型
|
|
|
|
|
var dbType = _client.CurrentConnectionConfig.DbType;
|
|
|
|
|
string sql = string.Empty;
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
switch (dbType)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
case DbType.SqlServer:
|
|
|
|
|
sql = $"SELECT COUNT(1) FROM sys.tables WHERE name = '{tableName}'";
|
|
|
|
|
break;
|
|
|
|
|
case DbType.MySql:
|
|
|
|
|
sql = $"SELECT COUNT(1) FROM information_schema.tables WHERE table_name = '{tableName}' AND table_schema = DATABASE()";
|
|
|
|
|
break;
|
|
|
|
|
case DbType.PostgreSQL:
|
|
|
|
|
sql = $"SELECT COUNT(1) FROM pg_tables WHERE tablename = '{tableName.ToLower()}'";
|
|
|
|
|
break;
|
|
|
|
|
case DbType.Oracle:
|
|
|
|
|
sql = $"SELECT COUNT(1) FROM user_tables WHERE table_name = '{tableName.ToUpper()}'";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new NotSupportedException($"不支持的数据库类型:{dbType}");
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
var count = _client.Ado.GetInt(sql);
|
|
|
|
|
return count > 0;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 获取表字段信息
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <param name="tableName">表名</param>
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <returns></returns>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
private List<DbColumnInfo> GetTableColumns(string tableName)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
return _client.DbMaintenance.GetColumnInfosByTableName(tableName);
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
|
|
|
|
/// <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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
}
|
2025-03-30 02:19:14 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 对象扩展方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ObjectExtensions
|
|
|
|
|
{
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// <summary>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// 将对象转换为字典
|
2025-03-30 02:19:14 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="obj">对象</param>
|
2025-03-30 16:50:46 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static Dictionary<string, object> ToDictionary(this object obj)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
var dict = new Dictionary<string, object>();
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
if (obj == null) return dict;
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 如果已经是字典类型,直接返回
|
|
|
|
|
if (obj is Dictionary<string, object> dictionary)
|
2025-03-30 02:19:14 +08:00
|
|
|
|
{
|
2025-03-30 16:50:46 +08:00
|
|
|
|
return dictionary;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
// 获取所有属性
|
|
|
|
|
foreach (var prop in obj.GetType().GetProperties())
|
|
|
|
|
{
|
|
|
|
|
var value = prop.GetValue(obj);
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
dict[prop.Name] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-30 18:01:22 +08:00
|
|
|
|
|
2025-03-30 16:50:46 +08:00
|
|
|
|
return dict;
|
2025-03-30 02:19:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|