wxr
2023-08-04 20f70e3446df19bf5d0faaae9f7bd58fd0fc4bcc
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**发布环境 使用发布环境 发布的正式签名KEY**/
#define Distribution
 
///**测试环境 使用测试环境 使用测试的签名KEY**/
//#undef Distribution 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
//using HDL_ON.Entity;
using RestSharp;
using Shared.SimpleControl;
 
 
 
namespace Shared
{
    public class HttpUtil
    {
 
        ///// <summary>
        ///// API_HTTPS
        ///// </summary>
        //public const string API_HTTPS = "https://";
        /// <summary>
        /// RegionMark
        /// </summary>
        public const string RegionMark = "HDL";
        /// <summary>
        /// 请求超时时间
        /// </summary>
        public const int TIME_OUT = 10;
        /// <summary>
        /// 特殊接口请求超时时间
        /// </summary>
        public const int TIME_OUT_LONG = 20;
        /////// <summary>
        /////// Bearer 暂时设为空,从登陆成功的返回的headerPrefix参数动态获取
        /////// </summary>
        //public const string TOKEN_BEARER = "Bearer ";
 
 
 
 
 
#if Distribution
        ///**********正式环境**********
        /// <summary>
        /// 固定域名,正式环境
        /// 公共域名就近解析
        /// </summary>
        public const string GlobalRequestHttpsHost = "https://bahrain-gateway.hdlcontrol.com";
        /// <summary>
        /// 
        /// </summary>
        const string APP_KEY = "HDL-HOME-IND-APP";
        /// <summary>
        /// 
        /// </summary>
        const string SECRET_KEY = "yPL345bn68gHnvilG4tYbq3cTYkiHu";
 
#else
        ///**********测试环境**********
        /// <summary>
        /// 固定域名,测试境
        /// 公共域名就近解析
        /// </summary>
        public const string GlobalRequestHttpsHost = "https://china-gateway.hdlcontrol.com";
        /// <summary>
        /// 
        /// </summary>
        const string APP_KEY = "HDL-APP-TENANT";
        /// <summary>
        /// 
        /// </summary>
        const string SECRET_KEY = "CeL345bn28gHnvi9G4tYcq3cTYkiiC";
#endif
 
 
 
        #region **********签名校验**********
        ///// <summary>
        ///// 
        ///// </summary>
        //const string APP_KEY = "HDL-HOME-APP-TEST";
        ///// <summary>
        ///// 
        ///// </summary>
        //const string SECRET_KEY = "WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss";
 
 
 
        /// <summary>
        /// 获取当前时间戳值
        /// </summary>
        /// <returns></returns>
        static string GetTimestamp ()
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (new System.DateTime (1970, 1, 1)); // 当地时区
            return ((long)(DateTime.Now - startTime).TotalMilliseconds).ToString (); // 相差秒数
            //return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒数
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="signstr"></param>
        /// <returns></returns>
        static string SignMD5Encrypt (string s)
        {
            byte [] sign = MD5.Create ().ComputeHash (UTF8Encoding.UTF8.GetBytes (s));
            string signstr = string.Empty;
            foreach (byte item in sign) {
                signstr += item.ToString ("X2");
            }
            return signstr.ToLower ();
        }
 
        /// <summary>
        /// 判断当前值是否需要参与签名,保持跟云端一致
        /// 空字符串不参与
        /// 数组,集合,对象不参与
        /// </summary>
        /// <param name="valueStr"></param>
        /// <returns></returns>
        static bool IfValueNeedSign (string valueStr)
        {
            if (string.IsNullOrEmpty (valueStr) || valueStr.StartsWith ("{") || valueStr.StartsWith ("[")) {
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 2020-11-02
        /// 基础服务的接口都要校验sign
        /// 计算sign签名
        /// </summary>
        /// <returns></returns>
        public static string GetSignRequestJson (object requestObj)
        {
            try {
                //1. 将model实体转为Dictionary<string, object>
                var paramDictionary = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>> (Newtonsoft.Json.JsonConvert.SerializeObject (requestObj));
                //2. 计算sign
                if (paramDictionary != null) {
                    paramDictionary.Add ("appKey", APP_KEY);
                    paramDictionary.Add ("timestamp", GetTimestamp ());
                    //2.1 字典升序
                    paramDictionary = paramDictionary.OrderBy (o => o.Key).ToDictionary (o => o.Key, p => p.Value);
                    //2.2 拼接按URL键值对
                    string str = string.Empty;
                    foreach (KeyValuePair<string, object> item in paramDictionary) {
                        //Value为null不参加校验
                        if (item.Value != null) {
                            //检测当前参数是否需要参与校验
                            if (IfValueNeedSign (item.Value.ToString ())) {
                                //如果是bool类型,要转小写
                                if (item.Value is bool) {
                                    str += item.Key + "=" + item.Value.ToString ().ToLower () + "&";
                                } else {
                                    str += item.Key + "=" + item.Value.ToString () + "&";
                                }
                            }
                        }
                    }
 
                    //2.3 拼接SECRET_KEY
                    str = str.Substring (0, str.Length - 1) + SECRET_KEY;
                    //2.4 MD5转换+转小写
                    var signstr = SignMD5Encrypt (str);
                    paramDictionary.Add ("sign", signstr);
                    var signResult = Newtonsoft.Json.JsonConvert.SerializeObject (paramDictionary);
                    return signResult;
                } else {
                    return "";
                }
            } catch {
                return "";
            }
        }
 
        #endregion
    }
 
    /// <summary>
    /// 响应参数
    /// </summary>
    [Serializable]
    public class ResponsePackNew
    {
        /// <summary>
        /// 响应状态码
        /// </summary>
        public string Code;
 
        /// <summary>
        /// 响应内容
        /// </summary>
        public object Data;
 
        /// <summary>
        /// 响应错误信息
        /// </summary>
        public string message;
 
        /// <summary>
        /// 这个是请求错误时的扩展数据,以后所有的附带扩展数据都会放在这里面动态维护
        /// </summary>
        public object extra;
 
 
    }
 
}