using Newtonsoft.Json;
|
using System.Text;
|
using NewPdaSqlServer.util;
|
using System.Security.Cryptography;
|
|
|
|
namespace NewPdaSqlServer.util
|
{
|
/// SignUnit 签名属性常量类
|
public class SignConst
|
{
|
public SignConst()
|
{
|
//this.appKey = "变量_AppKey";
|
//this.operateCompanyCode = "变量_operateCompanyCode";
|
//this.ownerCompanyCode = "变量_ownerCompanyCode";
|
//this.appSecret = "变量_appSecret";
|
this.appKey = "5f0f8dadc4edc70197a73f7ef506aa9b";
|
this.operateCompanyCode = "85621331";
|
this.ownerCompanyCode = "85621331";
|
this.appSecret = "fa7c117c02fd4967849a612963c034ca";
|
this.version = "1.0";
|
}
|
|
public string appKey { get; set; }
|
public string version { get; set; }
|
public string operateCompanyCode { get; set; }
|
public string ownerCompanyCode { get; set; }
|
public string appSecret { get; set; }
|
}
|
}
|
|
namespace NewPdaSqlServer.util
|
{
|
/// API相关参数定义的全局类
|
public partial class ApiGlobal
|
{
|
private static readonly object _lock = new object();
|
private static bool _initialized = false;
|
|
static ApiGlobal()
|
{
|
Init();
|
}
|
|
public static void Init()
|
{
|
if (!_initialized)
|
{
|
lock (_lock)
|
{
|
if (!_initialized)
|
{
|
signConst = new SignConst();
|
_initialized = true;
|
}
|
}
|
}
|
}
|
|
public static SignConst signConst { get; set; }
|
}
|
}
|
|
|
namespace NewPdaSqlServer.util
|
{
|
/// SignUtils 签名工具类
|
public class SignUtils
|
{
|
public static MD5 md5 = MD5.Create();
|
|
public static string buildCurrentSign(IDictionary<string, string> parameters, String appSecret)
|
{
|
try
|
{
|
string secret = ":" + appSecret;
|
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal);
|
IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
|
StringBuilder query = new StringBuilder();
|
while (dem.MoveNext())
|
{
|
string key = dem.Current.Key;
|
string value = dem.Current.Value;
|
if (key == "sign")
|
{
|
continue;
|
}
|
if (!string.IsNullOrEmpty(key))
|
{
|
query.Append(value).Append(":");
|
}
|
}
|
string strvalue = query.ToString().TrimEnd(':') + secret;
|
return MD5Encrypt32(strvalue);
|
}
|
catch (Exception)
|
{
|
throw new Exception("签名异常!");
|
}
|
}
|
|
public static string MD5Encrypt32(string text)
|
{
|
string pwd = "";
|
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode的选择
|
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
|
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
|
for (int i = 0; i < s.Length; i++)
|
{
|
//将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
|
pwd = pwd + s[i].ToString("x2");
|
}
|
return pwd.ToString().ToLower();
|
}
|
|
public static long GetTimestamp(DateTime d)
|
{
|
TimeSpan ts = d.ToUniversalTime() - new DateTime(1970, 1, 1);
|
return (long)ts.TotalSeconds; //精确到秒
|
}
|
}
|
}
|
|
|
namespace NewPdaSqlServer.util
|
{
|
/// ApiCommonParam API请求对应的Common参数类
|
public class ApiCommonParam
|
{
|
public ApiCommonParam() { }
|
|
public ApiCommonParam(string appKey, string version, string operateCompanyCode, string ownerCompanyCode, long timestamps)
|
{
|
this.appKey = appKey;
|
this.version = version;
|
this.operateCompanyCode = operateCompanyCode;
|
this.ownerCompanyCode = ownerCompanyCode;
|
this.timestamps = timestamps;
|
}
|
|
public string appKey { get; set; }//appKey
|
public string version { get; set; }//接口版本
|
public string ownerCompanyCode { get; set; }//数据所属公司编码
|
public string operateCompanyCode { get; set; }//操作者所属公司编码
|
public string sign { get; set; }//签名
|
public long timestamps { get; set; }//请求的时间戳
|
public object reserver { get; set; }//扩展字段
|
|
/// 构造对象
|
public static ApiCommonParam NewApiCommon()
|
{
|
// 确保初始化完成
|
ApiGlobal.Init();
|
|
// 构造对象(原有逻辑保持不变)
|
ApiCommonParam param = new ApiCommonParam(
|
ApiGlobal.signConst.appKey,
|
ApiGlobal.signConst.version,
|
ApiGlobal.signConst.operateCompanyCode,
|
ApiGlobal.signConst.ownerCompanyCode,
|
SignUtils.GetTimestamp(DateTime.Now));
|
//计算签名&赋值
|
var jsonParam = JsonConvert.SerializeObject(param);
|
//JavaScriptSerializer json = new JavaScriptSerializer();
|
//string jsonParam = json.Serialize(param);
|
var paramDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonParam);
|
param.sign = SignUtils.buildCurrentSign(paramDict, ApiGlobal.signConst.appSecret);
|
return param;
|
}
|
}
|
}
|