using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace Serein.Library.Network
{
///
/// api请求处理模块
///
public class ApiHandleConfig
{
private readonly IJsonPortal jsonPortal;
private readonly DelegateDetails delegateDetails;
///
/// Post请求处理方法中,入参参数类型
///
public enum PostArgType
{
///
/// 不做处理
///
None,
///
/// 使用Url参数
///
IsUrlData,
///
/// 使用整体的Boby参数
///
IsBobyData,
}
///
/// 添加处理配置
///
///
///
public ApiHandleConfig(IJsonPortal jsonPortal, MethodInfo methodInfo)
{
delegateDetails = new DelegateDetails(methodInfo);
var parameterInfos = methodInfo.GetParameters();
ParameterType = parameterInfos.Select(t => t.ParameterType).ToArray();
ParameterName = parameterInfos.Select(t => t.Name.ToLower()).ToArray();
PostArgTypes = parameterInfos.Select(p =>
{
bool isUrlData = p.GetCustomAttribute(typeof(UrlAttribute)) != null;
bool isBobyData = p.GetCustomAttribute(typeof(BobyAttribute)) != null;
if (isBobyData)
{
return PostArgType.IsBobyData;
}
else if (isUrlData)
{
return PostArgType.IsUrlData;
}
else
{
return PostArgType.None;
}
}).ToArray();
}
private readonly PostArgType[] PostArgTypes;
private readonly string[] ParameterName;
private readonly Type[] ParameterType;
///
/// 处理Get请求
///
///
public object[] GetArgsOfGet(Dictionary routeData)
{
object[] args = new object[ParameterType.Length];
for (int i = 0; i < ParameterType.Length; i++)
{
var type = ParameterType[i];
var argName = ParameterName[i];
if (routeData.TryGetValue(argName, out var argValue))
{
if (type == typeof(string))
{
args[i] = argValue;
}
else // if (type.IsValueType)
{
args[i] = jsonPortal.Deserialize(argValue, type); // JsonConvert.DeserializeObject(argValue, type);
}
}
else
{
args[i] = type.IsValueType ? Activator.CreateInstance(type) : null;
}
}
return args;
}
public object[] GetArgsOfPost(Dictionary routeData, IJsonToken jsonObject)
{
object[] args = new object[ParameterType.Length];
for (int i = 0; i < ParameterType.Length; i++)
{
var type = ParameterType[i];
var argName = ParameterName[i];
if (PostArgTypes[i] == PostArgType.IsUrlData)
{
if (routeData.TryGetValue(argName, out var argValue))
{
if (type == typeof(string))
{
args[i] = argValue;
}
else // if (type.IsValueType)
{
args[i] = jsonPortal.Deserialize(argValue, type);
}
}
else
{
args[i] = type.IsValueType ? Activator.CreateInstance(type) : null;
}
}
else if (jsonObject != null && PostArgTypes[i] == PostArgType.IsBobyData)
{
args[i] = jsonObject.ToObject(type);
}
else if (jsonObject != null)
{
if(jsonObject.TryGetProperty(argName, out var jsonToken))
{
args[i] = jsonToken.ToObject(type);
}
else
{
args[i] = type.IsValueType ? Activator.CreateInstance(type) : null;
}
}
}
return args;
}
///
/// 处理请求
///
///
///
///
public async Task