/// <summary>
/// 除去数组中的空值和签名参数并以字母a到z的顺序排序
/// </summary>
/// <param name="dicParams">过滤前的参数组</param>
/// <returns>过滤并排序后(a-z)的参数组</returns>
public static Dictionary<string, string> FilterParams(IDictionary<string, string> dicParams, string[] signKey = null, string filter = "", bool filterNull = true)
{
Dictionary<string, string> dicSignParams = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> temp in dicParams)
{
if (filterNull)
{
if (!string.IsNullOrEmpty(temp.Value))
{
if (signKey == null && !filter.Contains(temp.Key))
{
dicSignParams.Add(temp.Key, temp.Value);
continue;
}
if (signKey != null && signKey.Contains(temp.Key) && !filter.Contains(temp.Key))
{
dicSignParams.Add(temp.Key, temp.Value);
continue;
}
}
}
else
{
if (signKey == null && !filter.Contains(temp.Key))
{
dicSignParams.Add(temp.Key, temp.Value);
continue;
}
if (signKey != null && signKey.Contains(temp.Key) && !filter.Contains(temp.Key))
{
dicSignParams.Add(temp.Key, temp.Value);
continue;
}
}
}
return dicSignParams;
}
/// <summary>
/// 过滤参数,不排序
/// </summary>
/// <param name="dicParams">原字典数据</param>
/// <param name="signKeys">key</param>
/// <returns></returns>
public static Dictionary<string, string> FilterParams(Dictionary<string, string> dicParams, string[] signKeys = null, bool isFlitNull = true)
{
if (signKeys == null)
return dicParams;
var newParams = new Dictionary<string, string>();
foreach (var signKey in signKeys)
{
foreach (var item in dicParams)
{
if (!isFlitNull)
{
if (signKey == item.Key)
newParams.Add(item.Key, item.Value);
continue;
}
if (!string.IsNullOrEmpty(item.Value))
{
if (signKey == item.Key)
newParams.Add(item.Key, item.Value);
}
}
}
return newParams;
}
/// <summary>
/// Dictionary去除fitlers的字段
/// </summary>
/// <param name="dicParams"></param>
/// <param name="fitlers"></param>
/// <returns></returns>
public static Dictionary<string, string> FilterParams(Dictionary<string, string> dicParams, string filters)
{
var returnDic = new Dictionary<string, string>(dicParams);
var arr = filters.Split(',');
foreach (var item in arr)
{
if (returnDic.ContainsKey(item))
{
returnDic.Remove(item);
}
}
return returnDic;
}
/// <summary>
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
/// </summary>
/// <param name="sArray">需要拼接的数组</param>
/// <returns>拼接完成以后的字符串</returns>
public static string CreateLinkString(IDictionary<string, string> dicArray, bool concatKey = true, string mark = "&", Encoding encode = null)
{
StringBuilder prestr = new StringBuilder();
foreach (KeyValuePair<string, string> temp in dicArray)
{
string value = temp.Value;
if (encode != null)
value = HttpUtility.UrlEncode(temp.Value, encode);
if (concatKey)
prestr.Append(string.Format("{0}={1}{2}", temp.Key, value, mark));
else
prestr.Append(string.Format("{0}{1}", value, mark));
}
//去掉最后一个拼接符
prestr.Remove(prestr.Length - mark.Length, mark.Length);
return prestr.ToString();
}
/// <summary>
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符 或者 参数=[参数值] 拼接成字符串
/// </summary>
/// <param name="sArray">需要拼接的数组</param>
/// <returns>拼接完成以后的字符串</returns>
public static string CreateLinkStringWithBrackets(Dictionary<string, string> dicArray, bool concatKey = true, string mark = "&", Encoding encode = null)
{
StringBuilder prestr = new StringBuilder();
foreach (KeyValuePair<string, string> temp in dicArray)
{
string value = temp.Value;
if (encode != null)
value = HttpUtility.UrlEncode(temp.Value, encode);
if (concatKey)
prestr.Append(string.Format("{0}={1}{2}", temp.Key, value, mark));
else
prestr.Append(string.Format("{0}=[{1}]", temp.Key, value));
}
//去掉最后一个拼接符
prestr.Remove(prestr.Length - mark.Length, mark.Length);
return prestr.ToString();
}
public static Tuple<T, bool> GetDictionaryParams<T>(string payName) where T : IDictionary<string, string>, new()
{
var hasParams = false;
T t = new T();
try
{
var request = HttpContext.Current.Request.QueryString.Count > 0
? HttpContext.Current.Request.QueryString
: HttpContext.Current.Request.Form;
var allKeys = request.AllKeys;
foreach (var key in allKeys)
{
if (key == null)
continue;
t.Add(key, request[key]);
}
if (t.Count == 0)
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 未接收到任何通知参数");
else
{
hasParams = true;
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 通知参数:{JsonConvert.SerializeObject(t)}");
}
}
catch (Exception ex)
{
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 接收回调通知参数时发生异常,异常原因[{ex.Message}>>>>>{ex.StackTrace}]");
}
return new Tuple<T, bool>(t, hasParams);
}
public static string ConvertToXml(IDictionary<string, string> dictionary)
{
var sb = new StringBuilder("<xml>");
foreach (var item in dictionary)
{
sb.AppendFormat($"<{item.Key}>{item.Value}</{item.Key}>");
}
return sb.Append("</xml>").ToString();
}
/// <summary>
/// 将json数据转为字典
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="payName"></param>
/// <returns></returns>
public static Tuple<T, bool> GetJsonDictionaryParams<T>(string payName) where T : IDictionary<string, object>, new()
{
var hasParams = false;
T t = new T();
try
{
var request = HttpContext.Current.Request.QueryString.Count > 0
? HttpContext.Current.Request.QueryString
: HttpContext.Current.Request.Form;
var allKeys = request.AllKeys;
foreach (var key in allKeys)
{
if (key == null)
continue;
t.Add(key, request[key]);
}
if (t.Count == 0)
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 未接收到任何通知参数");
else
{
hasParams = true;
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 通知参数:{JsonConvert.SerializeObject(t)}");
}
}
catch (Exception ex)
{
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 接收回调通知参数时发生异常,异常原因[{ex.Message}>>>>>{ex.StackTrace}]");
}
return new Tuple<T, bool>(t, hasParams);
}
#region 将Url参数转为字典
public static Dictionary<String, String> GetUrlParam(String strHref)
{
Dictionary<string, string> dicInfo = new Dictionary<string, string>();
String[] arrParams = strHref.Split('&');
for (int i = 0; i < arrParams.Length; i++)
{
String[] arrSingle = arrParams[i].Split('=');
if (arrSingle.Length > 1)
{
String keyInfo = arrSingle[0].Trim();
String valueInfo = HttpUtility.UrlDecode(arrSingle[1].Trim());
if (!dicInfo.ContainsKey(keyInfo))
{
dicInfo.Add(keyInfo, valueInfo);
}
}
}
return dicInfo;
}
public static SortedDictionary<String, String> GetUrlSortedParam(String strHref)
{
SortedDictionary<string, string> dicInfo = new SortedDictionary<string, string>();
String[] arrParams = strHref.Split('&');
for (int i = 0; i < arrParams.Length; i++)
{
String[] arrSingle = arrParams[i].Split('=');
if (arrSingle.Length > 1)
{
String keyInfo = arrSingle[0].Trim();
String valueInfo = HttpUtility.UrlDecode(arrSingle[1].Trim());
if (!dicInfo.ContainsKey(keyInfo))
{
dicInfo.Add(keyInfo, valueInfo);
}
}
}
return dicInfo;
}
#endregion
/// <summary>
/// base解码
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string UnBase64String(string value)
{
if (value == null || value == "")
{
return "";
}
byte[] bytes = Convert.FromBase64String(value);
return Encoding.UTF8.GetString(bytes);
}
public static Tuple<T, bool> GetDictionaryParams2<T>(string payName) where T : IDictionary<string, object>, new()
{
var hasParams = false;
T t = new T();
try
{
var request = HttpContext.Current.Request.QueryString.Count > 0
? HttpContext.Current.Request.QueryString
: HttpContext.Current.Request.Form;
var allKeys = request.AllKeys;
foreach (var key in allKeys)
{
if (key == null)
continue;
t.Add(key, request[key]);
}
if (t.Count == 0)
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 未接收到任何通知参数");
else
{
hasParams = true;
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 通知参数:{JsonConvert.SerializeObject(t)}");
}
}
catch (Exception ex)
{
Common.CustomLogger.WriterLog(Common.LogType.OnlinePay, $"{payName} 接收回调通知参数时发生异常,异常原因[{ex.Message}>>>>>{ex.StackTrace}]");
}
return new Tuple<T, bool>(t, hasParams);
}