From bb6ad792b598927a5459a5fb6f6c27fb1aa9e94e Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期四, 17 十二月 2020 14:06:36 +0800
Subject: [PATCH] 20201217-1

---
 HDL_ON/DAL/Server/HttpUtil.cs |  169 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 132 insertions(+), 37 deletions(-)

diff --git a/HDL_ON/DAL/Server/HttpUtil.cs b/HDL_ON/DAL/Server/HttpUtil.cs
index 5e52242..512e33b 100644
--- a/HDL_ON/DAL/Server/HttpUtil.cs
+++ b/HDL_ON/DAL/Server/HttpUtil.cs
@@ -6,14 +6,13 @@
 using System.Text;
 using HDL_ON.Entity;
 using RestSharp;
-using Shared;
 
 namespace HDL_ON.DAL.Server
 {
     public class HttpUtil
     {
 
-        #region HttpUtil 鍏ㄥ眬甯搁噺
+        #region **********鍏ㄥ眬甯搁噺**********
         ///// <summary>
         ///// API_HTTPS
         ///// </summary>
@@ -37,10 +36,11 @@
 
         #endregion
 
+        #region **********缃戠粶璇锋眰灏佽**********
         /// <summary>
-        /// 鍥哄畾鍩熷悕
+        /// 鍥哄畾鍩熷悕,姝e紡鐜
         /// </summary>
-        //public const string GlobalRequestHttpsHost = "https://center.hdlcontrol.com";
+        //public const string GlobalRequestHttpsHost = "https://nearest.hdlcontrol.com";
         public const string GlobalRequestHttpsHost = "https://test-gz.hdlcontrol.com";
 
         /// <summary>
@@ -95,7 +95,7 @@
         /// <returns></returns>
         public static ResponsePackNew RequestHttpsPostFroHome(string apiPath, string bodyParameterJson, int mTimeout = 10)
         {
-            string urlHead = DB_ResidenceData.residenceData.residecenInfo.regionUrl;
+            string urlHead = DB_ResidenceData.Instance.CurrentRegion.regionUrl;
             //var replaceToken = "";
             //if (DB_ResidenceData.residenceData.residecenInfo.IsOthreShare)
             //{
@@ -146,29 +146,28 @@
         /// <param name="replaceToken"></param>
         /// <param name="mTimeout"></param>
         /// <returns></returns>
-        public static ResponsePackNew RequestHttps(Method method, string apiPath, string bodyParameterJson = null, Dictionary<string, object> queryDictionary = null, Dictionary<string, object> urlSegmentDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 10)
+        static ResponsePackNew RequestHttpsBase(Method method, string apiPath, string bodyParameterJson = null, Dictionary<string, object> queryDictionary = null, Dictionary<string, object> urlSegmentDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 10)
         {
             #region HttpWebRequest
             try
             {
 
-                //if (string.IsNullOrEmpty(urlHead))
-                //{
-                //    urlHead = UserInfo.Current.RequestHttpsHost;
-                //}
-                //string requestFullUrl = urlHead + apiPath;
+                if (string.IsNullOrEmpty(urlHead))
+                {
+                    urlHead = OnAppConfig.Instance.RequestHttpsHost;
+                }
+                string requestFullUrl = urlHead + apiPath;
 
 
-                //**************娴嬭瘯**************
-                string requestFullUrl = GlobalRequestHttpsHost + apiPath;
-                //**************娴嬭瘯**************
+                ////**************娴嬭瘯**************
+                //string requestFullUrl = GlobalRequestHttpsHost + apiPath;
+                ////**************娴嬭瘯**************
 
                 RestClient client = new RestClient(requestFullUrl);
 
                 RestRequest request = new RestRequest(method);
                 request.Timeout = mTimeout * 1000;
                 request.AddHeader("content-type", "application/json");
-                //request.AddHeader ("cache-control", "no-cache");
 
                 if (string.IsNullOrEmpty(replaceToken))
                 {
@@ -208,7 +207,6 @@
                 {
                     try
                     {
-
                         ResponsePackNew revertObj = new ResponsePackNew() { };
                         revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content);
                         //*****涓�浜涘垽绌哄鐞�*****************
@@ -223,8 +221,6 @@
                         }
                         //*****涓�浜涘垽绌哄鐞�*****************
 
-                        ////缁熶竴杞垚澶у啓
-                        //revertObj.StateCode = revertObj.StateCode.ToUpper ();
                         return revertObj;
                     }
                     catch (Exception ex)
@@ -251,6 +247,69 @@
         }
 
         /// <summary>
+        /// 閫氱敤  璇锋眰鏈嶅姟鍣ㄦ柟娉�
+        /// 澧炲姞token杩囨湡澶勭悊
+        /// </summary>
+        /// <param name="method"></param>
+        /// <param name="apiPath"></param>
+        /// <param name="bodyParameterJson"></param>
+        /// <param name="queryDictionary"></param>
+        /// <param name="urlSegmentDictionary"></param>
+        /// <param name="urlHead"></param>
+        /// <param name="replaceToken"></param>
+        /// <param name="mTimeout"></param>
+        /// <returns></returns>
+        public static ResponsePackNew RequestHttps(Method method, string apiPath, string bodyParameterJson = null, Dictionary<string, object> queryDictionary = null, Dictionary<string, object> urlSegmentDictionary = null, string urlHead = "", string replaceToken = "", int mTimeout = 10)
+        {
+            var responsePackNew = RequestHttpsBase(method, apiPath, bodyParameterJson, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout);
+            //*****************Token杩囨湡澶勭悊*****************
+            if (responsePackNew.Code == StateCode.TOKEN_EXPIRED)
+            {
+                //鍒锋柊Token
+                if (RefreshToken()) {
+                    return RequestHttpsBase(method, apiPath, bodyParameterJson, queryDictionary, urlSegmentDictionary, urlHead, replaceToken, mTimeout);
+                }
+                else
+                {
+                    return responsePackNew;
+                }
+            }
+            //*****************Token杩囨湡澶勭悊*****************
+            return responsePackNew;
+        }
+
+
+        /// <summary>
+        /// 鍒锋柊Token
+        /// </summary>
+        /// <returns></returns>
+        static bool RefreshToken()
+        {
+            try
+            {
+                var requestJson = GetSignRequestJson(new RefreshTokenObj()
+                {
+                    refreshToken = UserInfo.Current.RefreshToken,
+                });
+                var revertObj = RequestHttpsBase(Method.POST, NewAPI.API_POST_Login, requestJson);
+                if (revertObj.Code == StateCode.SUCCESS)
+                {
+                    var revertData = Newtonsoft.Json.JsonConvert.DeserializeObject<UserLoginRes>(revertObj.Data.ToString());
+                    UserInfo.Current.LoginTokenString = revertData.headerPrefix + revertData.accessToken;
+                    UserInfo.Current.RefreshToken = revertData.refreshToken;
+                    UserInfo.Current.LastTime = DateTime.Now;
+                    UserInfo.Current.SaveUserInfo();
+                    return true;
+                }
+                return false;
+            }
+            catch
+            {
+                return false;
+            }
+        }
+
+        /// <summary>
         /// 涓嬭浇鏂囦欢  璇锋眰鏈嶅姟鍣ㄦ柟娉�
         /// </summary>
         /// <param name="apiPath"></param>
@@ -265,17 +324,17 @@
             #region RestRequest
             try
             {
-                //if (string.IsNullOrEmpty (urlHead)) {
-                //    urlHead = UserInfo.Current.RequestHttpsHost;
-                //}
-                //string requestFullUrl = urlHead + apiPath;
+                if (string.IsNullOrEmpty(urlHead))
+                {
+                    urlHead = OnAppConfig.Instance.RequestHttpsHost;
+                }
+                string requestFullUrl = urlHead + apiPath;
 
-                //**************娴嬭瘯**************
-                string requestFullUrl = GlobalRequestHttpsHost + apiPath;
-                //**************娴嬭瘯**************
+                ////**************娴嬭瘯**************
+                //string requestFullUrl = GlobalRequestHttpsHost + apiPath;
+                ////**************娴嬭瘯**************
 
                 RestClient client = new RestClient(requestFullUrl);
-                //client.Timeout = mTimeout * 1000;
                 RestRequest request = new RestRequest(Method.POST);
                 request.Timeout = mTimeout * 1000;
                 request.AddHeader("content-type", "application/json");
@@ -335,14 +394,15 @@
             try
             {
 
-                //if (string.IsNullOrEmpty (urlHead)) {
-                //    urlHead = UserInfo.Current.RequestHttpsHost;
-                //}
-                //string requestFullUrl = urlHead + apiPath;
+                if (string.IsNullOrEmpty(urlHead))
+                {
+                    urlHead = OnAppConfig.Instance.RequestHttpsHost;
+                }
+                string requestFullUrl = urlHead + apiPath;
 
-                //**************娴嬭瘯**************
-                string requestFullUrl = GlobalRequestHttpsHost + apiPath;
-                //**************娴嬭瘯**************
+                ////**************娴嬭瘯**************
+                //string requestFullUrl = GlobalRequestHttpsHost + apiPath;
+                ////**************娴嬭瘯**************
 
                 RestClient client = new RestClient(requestFullUrl);
                 //client.Timeout = mTimeout * 1000;
@@ -423,6 +483,40 @@
         }
 
         /// <summary>
+        /// 涓嬭浇鏂囦欢  璇锋眰鏈嶅姟鍣ㄦ柟娉�
+        /// </summary>
+        /// <param name="apiPath"></param>
+        /// <param name="bodyParameterJson"></param>
+        /// <param name="queryDictionary"></param>
+        /// <param name="urlHead"></param>
+        /// <param name="replaceToken"></param>
+        /// <param name="mTimeout"></param>
+        /// <returns></returns>
+        public static byte[] HttpsDownload(string requestFullUrl, int mTimeout = 30)
+        {
+            #region RestRequest
+            try
+            {
+                RestClient client = new RestClient(requestFullUrl);
+                RestRequest request = new RestRequest(Method.GET);
+                request.Timeout = mTimeout * 1000;
+
+                IRestResponse response = client.Execute(request);
+                return response.RawBytes;
+            }
+            catch (Exception ex)
+            {
+                HDL_ON.Utlis.WriteLine(ex.Message);
+                return null;
+            }
+            #endregion
+
+        }
+
+        #endregion
+
+        #region **********绛惧悕鏍¢獙**********
+        /// <summary>
         /// 
         /// </summary>
         const string APP_KEY = "HDL-HOME-APP-TEST";
@@ -438,7 +532,8 @@
         static string GetTimestamp()
         {
             System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 褰撳湴鏃跺尯
-            return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 鐩稿樊绉掓暟
+            return ((long)(DateTime.Now - startTime).TotalMilliseconds).ToString(); // 鐩稿樊绉掓暟
+            //return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 鐩稿樊绉掓暟
         }
 
         /// <summary>
@@ -504,7 +599,8 @@
                     //2.4 MD5杞崲+杞皬鍐�
                     var signstr = SignMD5Encrypt(str);
                     paramDictionary.Add("sign", signstr);
-                    return Newtonsoft.Json.JsonConvert.SerializeObject(paramDictionary);
+                    var signResult = Newtonsoft.Json.JsonConvert.SerializeObject(paramDictionary);
+                    return signResult;
                 }
                 else
                 {
@@ -517,6 +613,7 @@
             }
         }
 
+        #endregion
     }
 
     /// <summary>
@@ -552,7 +649,5 @@
 
 
     }
-
-
 
 }

--
Gitblit v1.8.0