using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Serein.Library.Web; namespace Serein.Library.Network { /// /// api请求处理模块 /// public class ApiHandleConfig { private readonly DelegateDetails delegateDetails; /// /// Post请求处理方法中,入参参数类型 /// public enum PostArgType { /// /// 不做处理 /// None, /// /// 使用Url参数 /// IsUrlData, /// /// 使用整体的Boby参数 /// IsBobyData, } /// /// 添加处理配置 /// /// public ApiHandleConfig(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] = JsonConvert.DeserializeObject(argValue, type); } } else { args[i] = type.IsValueType ? Activator.CreateInstance(type) : null; } } return args; } public object[] GetArgsOfPost(Dictionary routeData, JObject 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] = JsonConvert.DeserializeObject(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) { var jsonValue = jsonObject.GetValue(argName); if (jsonValue is null) { // 值类型返回默认值,引用类型返回null args[i] = type.IsValueType ? Activator.CreateInstance(type) : null; } else { args[i] = jsonValue.ToObject(type); } } } return args; } /// /// 处理请求 /// /// /// /// public async Task HandleAsync(object instance, object[] args) { object result = null; try { result = await delegateDetails.InvokeAsync(instance, args); } catch (Exception ex) { result = null; await Console.Out.WriteLineAsync(ex.Message); } return result; } } }