南骏 池
4 天以前 4f8d1da89bc7cf399e6d90d6230ad6c28114443a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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;
        }
    }
}