From 1bb8683aef25143048e572b65734de4772bfc712 Mon Sep 17 00:00:00 2001 From: wxr <464027401@qq.com> Date: 星期五, 11 十二月 2020 10:48:17 +0800 Subject: [PATCH] Merge branch 'CJL-NEW' --- HDL_ON/DAL/Server/HttpUtil.cs | 78 ++++++++++++++++++++++++++++++++++---- 1 files changed, 69 insertions(+), 9 deletions(-) diff --git a/HDL_ON/DAL/Server/HttpUtil.cs b/HDL_ON/DAL/Server/HttpUtil.cs index 1cd630f..8335bf9 100644 --- a/HDL_ON/DAL/Server/HttpUtil.cs +++ b/HDL_ON/DAL/Server/HttpUtil.cs @@ -12,7 +12,7 @@ public class HttpUtil { - #region HttpUtil 鍏ㄥ眬甯搁噺 + #region **********鍏ㄥ眬甯搁噺********** ///// <summary> ///// API_HTTPS ///// </summary> @@ -36,6 +36,7 @@ #endregion + #region **********缃戠粶璇锋眰灏佽********** /// <summary> /// 鍥哄畾鍩熷悕,姝e紡鐜 /// </summary> @@ -145,7 +146,7 @@ /// <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 @@ -167,7 +168,6 @@ 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)) { @@ -207,7 +207,6 @@ { try { - ResponsePackNew revertObj = new ResponsePackNew() { }; revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePackNew>(response.Content); //*****涓�浜涘垽绌哄鐞�***************** @@ -222,8 +221,6 @@ } //*****涓�浜涘垽绌哄鐞�***************** - ////缁熶竴杞垚澶у啓 - //revertObj.StateCode = revertObj.StateCode.ToUpper (); return revertObj; } catch (Exception ex) @@ -247,6 +244,69 @@ #endregion + } + + /// <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> @@ -275,7 +335,6 @@ ////**************娴嬭瘯************** 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"); @@ -454,7 +513,9 @@ } + #endregion + #region **********绛惧悕鏍¢獙********** /// <summary> /// /// </summary> @@ -552,6 +613,7 @@ } } + #endregion } /// <summary> @@ -587,7 +649,5 @@ } - - } -- Gitblit v1.8.0