Skip to content

Commit bf27d40

Browse files
committed
bug: 修复过滤条件查询表达式转换时,不支持类似long?和long进行比较的问题
1 parent 3355a0c commit bf27d40

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

src/OSharp/Filter/FilterHelper.cs

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// <last-date>2018-07-15 10:22</last-date>
88
// -----------------------------------------------------------------------
99

10+
using System.Text.Json;
11+
1012
namespace OSharp.Filter;
1113

1214
/// <summary>
@@ -364,39 +366,56 @@ private static bool CheckFilterRule(Type type, FilterRule rule)
364366

365367
private static Expression ChangeTypeToExpression(FilterRule rule, Type conversionType)
366368
{
367-
//if (item.Method == QueryMethod.StdIn)
368-
//{
369-
// Array array = (item.Value as Array);
370-
// List<Expression> expressionList = new List<Expression>();
371-
// if (array != null)
372-
// {
373-
// expressionList.AddRange(array.Cast<object>().Select((t, i) =>
374-
// ChangeType(array.GetValue(i), conversionType)).Select(newValue => Expression.Constant(newValue, conversionType)));
375-
// }
376-
// return Expression.NewArrayInit(conversionType, expressionList);
377-
//}
378-
if (rule.Value?.ToString() == UserFlagAttribute.Token)
369+
if (rule.Value == null)
370+
{
371+
return Expression.Constant(null, conversionType);
372+
}
373+
374+
var valueType = rule.Value.GetType();
375+
var value = rule.Value;
376+
377+
// 处理 JsonElement 类型
378+
if (valueType.Name == "JsonElement")
379379
{
380-
// todo: 将UserFlag之类的功能提升为接口进行服务注册,好方便实现自定义XXXFlag
381-
if (rule.Operate != FilterOperate.Equal)
380+
var jsonElement = (JsonElement)value;
381+
value = jsonElement.ValueKind switch
382382
{
383-
throw new OsharpException($"当前用户“{rule.Value}”只能用在“{FilterOperate.Equal.ToDescription()}”操作中");
384-
}
385-
ClaimsPrincipal user = ServiceLocator.Instance.GetCurrentUser();
386-
if (user == null || !user.Identity.IsAuthenticated)
383+
JsonValueKind.String => jsonElement.GetString(),
384+
JsonValueKind.Number => jsonElement.TryGetInt64(out var l) ? l : jsonElement.GetDouble(),
385+
JsonValueKind.True => true,
386+
JsonValueKind.False => false,
387+
JsonValueKind.Null => null,
388+
_ => value
389+
};
390+
if (value != null)
387391
{
388-
throw new OsharpException("需要获取当前用户编号,但当前用户为空,可能未登录或已过期");
392+
valueType = value.GetType();
389393
}
390-
object value = user.Identity.GetClaimValueFirstOrDefault(ClaimTypes.NameIdentifier);
391-
value = value.CastTo(conversionType);
392-
return Expression.Constant(value, conversionType);
393394
}
394-
else
395+
396+
if (value == null)
395397
{
396-
object value = rule.Value.CastTo(conversionType);
397-
return Expression.Constant(value, conversionType);
398+
return Expression.Constant(null, conversionType);
398399
}
400+
401+
// 获取目标类型(如果是可空类型,获取其基础类型)
402+
var targetType = conversionType.IsNullableType()
403+
? Nullable.GetUnderlyingType(conversionType) ?? conversionType
404+
: conversionType;
405+
406+
// 转换值到目标类型
407+
if (targetType.IsEnum)
408+
{
409+
value = valueType == typeof(string) ? Enum.Parse(targetType, value.ToString()!) : Enum.ToObject(targetType, value);
410+
}
411+
else if (valueType != targetType)
412+
{
413+
value = targetType == typeof(Guid) ? Guid.Parse(value.ToString()!) : Convert.ChangeType(value, targetType);
414+
}
415+
416+
// 返回与conversionType匹配的常量表达式
417+
return Expression.Constant(value, conversionType);
399418
}
400419

401420
#endregion
402-
}
421+
}

0 commit comments

Comments
 (0)