183 lines
6.3 KiB
C#
183 lines
6.3 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Newtonsoft.Json;
|
||
|
||
namespace Aop.Api.Util
|
||
{
|
||
/// <summary>
|
||
/// AOP系统工具类。
|
||
/// </summary>
|
||
public abstract class AopUtils
|
||
{
|
||
/// <summary>
|
||
/// AES加密
|
||
/// </summary>
|
||
/// <param name="encryptKey"></param>
|
||
/// <param name="bizContent"></param>
|
||
/// <param name="charset"></param>
|
||
/// <returns></returns>
|
||
public static string AesEncrypt(string encryptKey, string bizContent, string charset)
|
||
{
|
||
return AlipayEncrypt.AesEncrypt(encryptKey, bizContent, charset);
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 对指定参数进行签名
|
||
/// </summary>
|
||
/// <param name="parameters">参数集合</param>
|
||
/// <param name="privateKey">商户私钥</param>
|
||
/// <param name="charset">字符集编码</param>
|
||
/// <param name="signType">签名算法类型,RSA2或RSA、SM2</param>
|
||
/// <param name="keyFromFile">是否从私钥证书文件中加载私钥
|
||
/// 如果该参数为true,privateKey为私钥证书文件路径;
|
||
/// 如果该参数为false,privateKey为私钥内容字符串
|
||
/// </param>
|
||
/// <returns>签名字符串</returns>
|
||
public static string SignAopRequest(IDictionary<string, string> parameters, string privateKey, string charset, bool keyFromFile, string signType)
|
||
{
|
||
return AlipaySignature.RSASign(parameters, privateKey, charset, signType, keyFromFile);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清除字典中值为空的项。
|
||
/// </summary>
|
||
/// <param name="dict">待清除的字典</param>
|
||
/// <returns>清除后的字典</returns>
|
||
public static IDictionary<string, T> CleanupDictionary<T>(IDictionary<string, T> dict)
|
||
{
|
||
IDictionary<string, T> newDict = new Dictionary<string, T>(dict.Count);
|
||
IEnumerator<KeyValuePair<string, T>> dem = dict.GetEnumerator();
|
||
|
||
while (dem.MoveNext())
|
||
{
|
||
string name = dem.Current.Key;
|
||
T value = dem.Current.Value;
|
||
if (value != null)
|
||
{
|
||
newDict.Add(name, value);
|
||
}
|
||
}
|
||
|
||
return newDict;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
|
||
/// </summary>
|
||
/// <param name="fileData">文件字节流</param>
|
||
/// <returns>JPG, GIF, PNG or null</returns>
|
||
public static string GetFileSuffix(byte[] fileData)
|
||
{
|
||
if (fileData == null || fileData.Length < 10)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
if (fileData[0] == 'G' && fileData[1] == 'I' && fileData[2] == 'F')
|
||
{
|
||
return "GIF";
|
||
}
|
||
else if (fileData[1] == 'P' && fileData[2] == 'N' && fileData[3] == 'G')
|
||
{
|
||
return "PNG";
|
||
}
|
||
else if (fileData[6] == 'J' && fileData[7] == 'F' && fileData[8] == 'I' && fileData[9] == 'F')
|
||
{
|
||
return "JPG";
|
||
}
|
||
else if (fileData[0] == 'B' && fileData[1] == 'M')
|
||
{
|
||
return "BMP";
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。
|
||
/// </summary>
|
||
/// <param name="fileData">文件字节流</param>
|
||
/// <returns>媒体类型</returns>
|
||
public static string GetMimeType(byte[] fileData)
|
||
{
|
||
string suffix = GetFileSuffix(fileData);
|
||
string mimeType;
|
||
|
||
switch (suffix)
|
||
{
|
||
case "JPG": mimeType = "image/jpeg"; break;
|
||
case "GIF": mimeType = "image/gif"; break;
|
||
case "PNG": mimeType = "image/png"; break;
|
||
case "BMP": mimeType = "image/bmp"; break;
|
||
default: mimeType = "application/octet-stream"; break;
|
||
}
|
||
|
||
return mimeType;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据文件后缀名获取文件的媒体类型。
|
||
/// </summary>
|
||
/// <param name="fileName">带后缀的文件名或文件全名</param>
|
||
/// <returns>媒体类型</returns>
|
||
public static string GetMimeType(string fileName)
|
||
{
|
||
string mimeType;
|
||
fileName = fileName.ToLower();
|
||
|
||
if (fileName.EndsWith(".bmp", StringComparison.CurrentCulture))
|
||
{
|
||
mimeType = "image/bmp";
|
||
}
|
||
else if (fileName.EndsWith(".gif", StringComparison.CurrentCulture))
|
||
{
|
||
mimeType = "image/gif";
|
||
}
|
||
else if (fileName.EndsWith(".jpg", StringComparison.CurrentCulture) || fileName.EndsWith(".jpeg", StringComparison.CurrentCulture))
|
||
{
|
||
mimeType = "image/jpeg";
|
||
}
|
||
else if (fileName.EndsWith(".png", StringComparison.CurrentCulture))
|
||
{
|
||
mimeType = "image/png";
|
||
}
|
||
else
|
||
{
|
||
mimeType = "application/octet-stream";
|
||
}
|
||
|
||
return mimeType;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据API名称获取响应根节点名称。
|
||
/// </summary>
|
||
/// <param name="api">API名称</param>
|
||
/// <returns></returns>
|
||
public static string GetRootElement(string api)
|
||
{
|
||
int pos = api.IndexOf(".", StringComparison.Ordinal);
|
||
if (pos != -1 && api.Length > pos)
|
||
{
|
||
api = api.Substring(pos + 1).Replace('.', '_');
|
||
}
|
||
return api + "_response";
|
||
}
|
||
|
||
public static IDictionary ParseJson(string body)
|
||
{
|
||
return JsonConvert.DeserializeObject<IDictionary>(body);
|
||
}
|
||
|
||
[Obsolete("请替换为未废弃的有完整参数列表的重载版本,明确指定各参数的值")]
|
||
public static string SignAopRequest(IDictionary<string, string> parameters, string privateKeyPem, string charset, string signType)
|
||
{
|
||
return AlipaySignature.RSASign(parameters, privateKeyPem, charset, signType);
|
||
}
|
||
}
|
||
}
|