Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改读取微信列表格式到列表 #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/PaySharp.Core/FastActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Linq.Expressions;

namespace PaySharp.Core
{
public static class FastActivator
{
public static Func<T, TResult> Generate<T, TResult>()
{
ConstructorInfo constructorInfo = typeof(TResult).GetConstructor(new Type[] { typeof(T), });
#if DEBUG
ParameterInfo[] parameters = constructorInfo.GetParameters();
ParameterExpression parameterExpression = Expression.Parameter(typeof(T), parameters[0].Name);
#else
ParameterExpression parameterExpression = Expression.Parameter(typeof(T));
#endif
Expression<Func<T, TResult>> expression = Expression.Lambda<Func<T, TResult>>(
Expression.New(
constructorInfo,
parameterExpression),
parameterExpression);
Func<T, TResult> functor = expression.Compile();
return functor;
}

}
}
115 changes: 103 additions & 12 deletions src/PaySharp.Wechatpay/ConvertUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
using PaySharp.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PaySharp.Wechatpay
{
internal static class ConvertUtil
{
public static List<T> ToList<T, TChildren>(GatewayData gatewayData, int index)

public static List<T> ToList<T, TChildren>(GatewayData gatewayData, int index) where T:new() where TChildren:new()
{
var flag = true;
var list = new List<T>();
int i = 0;
while (flag)
{
var type = typeof(T);
var obj = Activator.CreateInstance(type);
var obj = new T();
var properties = type.GetProperties();
var isFirstProperty = true;

Expand All @@ -28,16 +32,7 @@ public static List<T> ToList<T, TChildren>(GatewayData gatewayData, int index)
continue;
}

string key;
var renameAttribute = item.GetCustomAttributes(typeof(ReNameAttribute), true);
if (renameAttribute.Length > 0)
{
key = ((ReNameAttribute)renameAttribute[0]).Name;
}
else
{
key = item.Name.ToSnakeCase();
}
string key = GetRealName(item);
if (index > -1)
{
key += $"_{index}";
Expand Down Expand Up @@ -70,5 +65,101 @@ public static List<T> ToList<T, TChildren>(GatewayData gatewayData, int index)

return list;
}

/// <summary>
/// 获取字段json中的名字
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
private static string GetRealName(System.Reflection.PropertyInfo item)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

移除System.Reflection命名空间

{
string key;
var renameAttribute = item.GetCustomAttributes(typeof(ReNameAttribute), true);
if (renameAttribute.Length > 0)
{
key = ((ReNameAttribute)renameAttribute[0]).Name;
}
else
{
key = item.Name.ToSnakeCase();
}

return key;
}

/// <summary>
/// 将微信返回值特殊格式转化为列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gatewayData"></param>
/// <returns></returns>
public static List<T> ToList<T>(GatewayData gatewayData) where T : new()
{
var list = new List<T>();
var properties = typeof(T).GetProperties();
var keyfirst = properties[0];
var count = gatewayData.Keys.Where(p => Regex.IsMatch(p, $@"{GetRealName(keyfirst)}_\d")).Count();

for (var i = 0; i < count; i++)
{
var item = new T();
foreach(var field in properties)
{
string keyname = $"{GetRealName(field)}_{i}";
field.SetValue(item, gatewayData.GetStringValue(keyname));
}
list.Add(item);
}
return list;
}

/// <summary>
/// 将微信返回值特殊格式转化为列表(二级)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TChildren"></typeparam>
/// <param name="gatewayData"></param>
/// <returns></returns>
public static List<T> ToList<T, TChildren>(GatewayData gatewayData) where T : new() where TChildren : new()
{
var list = new List<T>();
var properties = typeof(T).GetProperties();
var keyfirst = properties[0];
var count = gatewayData.Keys.Where(p => Regex.IsMatch(p, $@"{GetRealName(keyfirst)}_\d")).Count();

for (var i = 0; i < count; i++)
{
var item = new T();
foreach (var field in properties)
{
if (field.PropertyType == typeof(List<TChildren>))
{
var sublist = new List<TChildren>();
var subProperties = typeof(TChildren).GetProperties();
var subFirstkey = subProperties[0];
var SubFirstName = GetRealName(subFirstkey);
var subCount = gatewayData.Keys.Where(p => Regex.IsMatch(p, $@"{SubFirstName}_{i}_\d")).Count();
for(var j = 0; j < subCount; j++)
{
var subItem = new TChildren();
foreach (var subfield in subProperties)
{
string keyname = $"{GetRealName(subfield)}_{i}_{j}";
subfield.SetValue(subItem, gatewayData.GetStringValue(keyname));
}
sublist.Add(subItem);
}
field.SetValue(item, sublist);
}
else
{
string keyname = $"{GetRealName(field)}_{i}";
field.SetValue(item, gatewayData.GetStringValue(keyname));
}
}
list.Add(item);
}
return list;
}
}
}
2 changes: 1 addition & 1 deletion src/PaySharp.Wechatpay/Response/QueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class QueryResponse : BaseResponse

internal override void Execute<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request)
{
Coupons = ConvertUtil.ToList<CouponResponse, object>(GatewayData, -1);
Coupons = ConvertUtil.ToList<CouponResponse>(GatewayData);
}

public class CouponResponse
Expand Down
2 changes: 1 addition & 1 deletion src/PaySharp.Wechatpay/Response/RefundQueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class RefundQueryResponse : BaseResponse

internal override void Execute<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request)
{
Refunds = ConvertUtil.ToList<RefundResponse, RefundCouponResponse>(GatewayData, -1);
Refunds = ConvertUtil.ToList<RefundResponse, RefundCouponResponse>(GatewayData);
}

public class RefundResponse
Expand Down
2 changes: 1 addition & 1 deletion src/PaySharp.Wechatpay/Response/RefundResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class RefundResponse : BaseResponse

internal override void Execute<TModel, TResponse>(Merchant merchant, Request<TModel, TResponse> request)
{
RefundCoupons = ConvertUtil.ToList<RefundCouponResponse, object>(GatewayData, -1);
RefundCoupons = ConvertUtil.ToList<RefundCouponResponse>(GatewayData);
}

public class RefundCouponResponse
Expand Down
2 changes: 1 addition & 1 deletion src/PaySharp.Wechatpay/WechatpayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected override async Task<bool> ValidateNotifyAsync()

if (string.IsNullOrEmpty(NotifyResponse.ReqInfo))
{
NotifyResponse.Coupons = ConvertUtil.ToList<CouponResponse, object>(GatewayData, -1);
NotifyResponse.Coupons = ConvertUtil.ToList<CouponResponse>(GatewayData);
if (NotifyResponse.Sign != SubmitProcess.BuildSign(GatewayData, _merchant.Key))
{
throw new GatewayException("签名不一致");
Expand Down